Pages

Tuesday, December 15, 2020

How To Get Current Date and Time in Java 8 and Older?

1. Overview

In this tutorial, We'll learn how to get the current date and time in java and new date time api in java 8.

There are different classes in java to get the current date and time and let us explore the example programs.

java.util.Date
Java 8 - java.time.Clock
java.util.Calendar
java.sql.Date
java.text.SimpleDateFormat
Java 8 - java.time.LocalDate
Java 8 - java.time.LocalTime
Java 8 - java.time.LocalDateTime
Java 8 - java.time.ZonedDateTime
Java 8 - java.time.format.DateTimeFormatter

How To Get Current Date and Time in Java 8 and Older?


2. Using java.util.Date


Use the util Date class constructor to get the current date and time values.

package com.javaprogramto.java8.dates.getdatetime;

import java.util.Date;

public class DateExample {

	public static void main(String[] args) {

		// using new Date()
		Date currentDateTime = new Date();
		System.out.println("Current date time using Date() : " + currentDateTime);

		// using System.currentTimeMillis()
		long milliSeconds = System.currentTimeMillis();
		currentDateTime = new Date(milliSeconds);
		System.out.println("Current date time using System.currentTimeMillis() : " + currentDateTime);
	}
}

Output:

Current date time using Date() : Tue Dec 15 20:47:44 IST 2020
Current date time using System.currentTimeMillis() : Tue Dec 15 20:47:44 IST 2020

3. Using java.util.Calendar


Use Calendar.getTime() method to get the Date instance.

package com.javaprogramto.java8.dates.getdatetime;

import java.util.Calendar;
import java.util.Date;

public class CalendarExample {

	public static void main(String[] args) {
		
		// Getting the calendar object
		Calendar cal = Calendar.getInstance();

		// Getting the util Date.
		Date currentDateTime = cal.getTime();
		System.out.println("Current Date Time : "+currentDateTime);
	}
}

Output:

Current Date Time : Tue Dec 15 20:51:02 IST 2020

4. Using java 8 - java.time.Clock


Use java 8 new method Instant() method from Clock class.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.Clock;
import java.time.Instant;

public class ClockExample {

	public static void main(String[] args) {
		Clock clockInUTC = Clock.systemUTC();
		Instant instnat = clockInUTC.instant();
		
		System.out.println("Java 8 - Current date time : "+instnat);
	}
}

Output:


5. Using java.sql.Date


This sql Date class returns only the Date part without time units. Pass the time in milliseconds to the sql Date() constructor and returns the date.

package com.javaprogramto.java8.dates.getdatetime;

import java.sql.Date;

public class SqlDateExample {

	public static void main(String[] args) {
		
		long timeInMillis = System.currentTimeMillis();
		
		Date sqlDate = new Date(timeInMillis);
		
		System.out.println("Current date from sql date : "+sqlDate);
	}
}

Output:
Current date from sql date : 2020-12-15

6. Using java.text.SimpleDateFormat


Use SimpleDateFormat class to get the date and time in our custom desired format.

Next, call format() method with the util Date and returns date in string format.

package com.javaprogramto.java8.dates.getdatetime;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatExample {

	public static void main(String[] args) {

		// current date custom format
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		
		// current date and time
		Date currentDate = new Date();
		
		// Util Date to String using format()
		String strDate = dateFormatter.format(currentDate);

		System.out.println("Current Date using SimpleDateFormat : "+strDate);
	}
}

Output:

Current Date using SimpleDateFormat : 2020-12-15 09:20

7. Using Java 8 - java.time.LocalDate


Use LocalDate.now() method to get the current date. This method does not return the time formats and stores the only date values.

Note: If you want to get only date part in java 8 then use LocalDate class.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalDate;

public class LocalDateExample {
	
	public static void main(String[] args) {
		
		// using LocalDate class
		LocalDate localDateCurrentDate = LocalDate.now();
		
		// print the current date
		System.out.println("LocalDate current date : "+localDateCurrentDate);
	}
}

Output:

LocalDate current date : 2020-12-15

8. Using Java 8 - java.time.LocalTime


In java 8, LocalTime class works opposite to the LocalDate class and LocalTime.now() method gives only time part with milli seconds by default.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalTime;

public class LocalTimeExample {

	public static void main(String[] args) {
		
		// Using LocalTime class now() method
		LocalTime localTimeCurrentTime = LocalTime.now();
		
		// using 
		System.out.println("LocalTime : "+localTimeCurrentTime);
	}
}
Output:
LocalTime : 21:28:59.495299

9. Using Java 8 - java.time.LocalDateTime


Another class from java 8 LocalDateTime class which gives both date and time part. Use now() method to get the current date and time.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalDateTime;

public class LocalDateTimeExample {

	public static void main(String[] args) {
		
		// using LocalDateTime class now() method
		LocalDateTime now = LocalDateTime.now();
		
		// printing
		System.out.println("Current date time from LocalDateTime : "+now);
	}
}

Output:
Current date time from LocalDateTime : 2020-12-15T21:33:16.944571

10. Using Java 8 - java.time.ZonedDateTime


Next java 8 class is ZonedDateTime and this works with timezone part along with the date time.

Use now() method to get the current date time from the current timezone.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.ZonedDateTime;

public class ZonedDateTimeExample {

	public static void main(String[] args) {

		// Using ZonedDateTime class now() method
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		
		// print current date time from ZonedDateTime class.
		System.out.println("ZonedDateTime : "+zonedDateTime);
	}
}

Output:
ZonedDateTime : 2020-12-15T21:36:12.881014+05:30[Asia/Kolkata]

11. Using Java 8 - java.time.format.DateTimeFormatter


Use DateTimeFormatter class to get the date in custom format in java 8 api.

Pass the date pattern to DateTimeFormatter.ofPattern() method and format() method to get the LocalDateTime or ZonedDateTime in string format.

package com.javaprogramto.java8.dates.getdatetime;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterExamples {

	public static void main(String[] args) {
		DateTimeFormatter formatterInJava8 = DateTimeFormatter.ofPattern("yyyy/MMM/dd HH:mm:ss");
		
		LocalDateTime LocalDateTimeCurrentTime = LocalDateTime.now();
		String date1 = formatterInJava8.format(LocalDateTimeCurrentTime);

		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		String date2 = formatterInJava8.format(zonedDateTime);
		
		System.out.println("Date in custom format : ");
		System.out.println("Current date time from LocalDateTime : "+date1);
		System.out.println("Current date time from ZonedDateTime : "+date2);
	}
}

Output:
Date in custom format : 
Current date time from LocalDateTime : 2020/Dec/15 21:41:17
Current date time from ZonedDateTime : 2020/Dec/15 21:41:17

12. Conclusion


In this article, we've seen various ways to get the date and time in java older versions and new java 8 api with examples.



No comments:

Post a Comment

Please do not add any spam links in the comments section.