Date toString() in Java

toString(): This method is available in java.util.Date class of Java.

Syntax:

String java.util.Date.toString()

This method converts this Date object to a string of the form: dow mon dd hh:mm:ss zzz yyyy

where:

1. dow is the day of the week.

2. mon is the month.

3. dd is the day of the month (01 through 31), as two decimal digits.

4. hh is the hour of the day (00 through 23), as two decimal digits.

5. mm is the minute within the hour (00 through 59), as two decimal digits.

6. ss is the second within the minute (00 through 61, as two decimal digits.

7. zzz is the time zone.

8. yyyy is the year, as four decimal digits.

Parameters: NA

Returns: a string representation of this date.

Exceptions: NA

Approach

Java

import java.util.Date;

public class DatetoString {
    public static void main(String[] args) {

        Date date = new Date();

        System.out.println(date.toString());

    }
}

Output:

Sun Jan 30 18:26:09 IST 2022


No comments:

Post a Comment