Get Current Date and Time in Java

Get Current Date and Time in Java

There are many ways to get current date and time in java. There are many classes that can be used to get current date and time in java.

  1. java.time.LocalDate class
  2. java.time.LocalTime class
  3. java.time.LocalDateTime class
  4. java.time.Clock class
  5. java.util.Date class
  6. java.sql.Date class
  7. java.util.Calendar class

Get Current Date: java.time.LocalDate

The LocalDate.now() method returns the instance of LocalDate class. If we print the instance of LocalDate class, it prints current date.

  1. System.out.println(java.time.LocalDate.now());

Output:

2017-01-23

Get Current Time: java.time.LocalTime

The LocalTime.now() method returns the instance of LocalTime class. If we print the instance of LocalTime class, it prints current time.

  1. System.out.println(java.time.LocalTime.now());

Output:

00:01:14.341

 

Get Current Date & Time: java.time.LocalDateTime

The LocalDateTime.now() method returns the instance of LocalDateTime class. If we print the instance of LocalDateTime class, it prints current date and time both.

  1. System.out.println(java.time.LocalDateTime.now());

Output:

2017-01-24T00:03:31.593

Get Current Date & Time: java.time.Clock

The Clock.systemUTC().instant() method returns current date and time both.

  1. System.out.println(java.time.Clock.systemUTC().instant());

Output:

2017-01-23T18:35:23.669Z

Get Current Date & Time: java.util.Date

By printing the instance of java.util.Date class, you can print current date and time in java. There are two ways to do so.

1st way:

  1. java.util.Date date=new java.util.Date();
  2. System.out.println(date);

2nd way:

  1. long millis=System.currentTimeMillis();
  2. java.util.Date date=new java.util.Date(millis);
  3. System.out.println(date);

Output:

Thu Mar 26 08:22:02 IST 2015

Get Current Date: java.sql.Date

By printing the instance of java.sql.Date class, you can print current date in java. It doesn’t print time. This date instance is generally used to save current date in database.

  1. long millis=System.currentTimeMillis();
  2. java.sql.Date date=new java.sql.Date(millis);
  3. System.out.println(date);

Output:

2015-03-26

Get Current Date & Time: java.util.Calendar

Calendar class can be used to get the instance of Date class. The getTime() method of Calendar class returns the instance of java.util.Date. The Calendar.getInstance() method returns the instance of Calendar class.

  1. Date date=java.util.Calendar.getInstance().getTime();
  2. System.out.println(date);

Output:

Thu Mar 26 08:22:02 IST 2015

Note: It is recommended to use Calendar class for getting current date and time in classical Date API. Since Java 8, you can use LocalDate, LocalTime or LocalDateTime classes.

Next TopicJava Convert String to int
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Get Current Date and Time in Java There are many ways to get current date and time in java. There are many classes that can be used to get current date and time in java. java.time.LocalDate class java.time.LocalTime class java.time.LocalDateTime class java.time.Clock class java.util.Date class java.sql.Date class java.util.Calendar class Get Current Date: java.time.LocalDate The LocalDate.now() […]

Tags:

....
...