Wednesday, January 17, 2024

Create Date Object in Java With Date and Time Values

There are scenarios when you may want to create a date object with specific values meaning you should be able to pass date values like year, month, day and time values like hour, minute, second too if required. In this post we’ll see how to create such a date object with values in Java.

In new data and time API from Java 8 you can create date object using one of the following classes as per your requirements.

  • LocalDate- to create Date object with date values.
  • LocalTime- to create Date object with time values.
  • LocalDateTime- To create Date object with both date and time values.
  • ZonedDateTime- To create Date object with both date and time with a time-zone.

If you still want to use one of the java.util classes then you can also get a Date object with values using GregorianCalendar class which is a concrete subclass of Calendar.

1. Create date object using LocalDate in Java

You can use of() static method of LocalDate class to obtain an instance of java.time.LocalDate from the passed year, month and day.

LocalDate date = LocalDate.of(2020, 6, 17);
System.out.println("Date- " + date); // 2020-06-17

You can also use enum java.time.Month to specify month.

LocalDate date = LocalDate.of(2020, Month.JUNE, 17);

2. Create date object using LocalTime

If you want only time values then use of() method of java.time.LocalTime
LocalTime time = LocalTime.of(17, 12, 17);
System.out.println("Time- " + time); // 17:12:17

3. Create datetime object using java.time.LocalDateTime

If you have to pass both date and time values then use java.time.LocalDateTime class.

LocalDateTime dateTime = LocalDateTime.of(2020, Month.JUNE, 17, 17, 12, 17);
System.out.println("Date- " + dateTime); // 2020-06-17T17:12:17

4. Date object using ZonedDateTime

Along with date and time if you want time-zone information then use ZonedDateTime. You need to set the required zone id, for that you can use ZoneId.of() method.

ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.of(2020, Month.JUNE, 17, 17, 12, 17), ZoneId.of("Asia/Tokyo"));
System.out.println("Date- " + zonedDateTime); // Date- 2020-06-17T17:12:17+09:00[Asia/Tokyo]

Creating Date object using Java GregorianCalendar

In GregorianCalendar class there are different constructors to create a java.util.Date object passing only year, month, day values or passing both date and time values. There is also a constructor to pass timezone to get time value in the passed time zone.

  • GregorianCalendar(int year, int month, int dayOfMonth)- Constructs a GregorianCalendar with the passed year, month, day in the default time zone with the default locale.
  • GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)- Constructs a GregorianCalendar with the passed year, month, day, hour, minute for the default time zone with the default locale.
  • GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)- Constructs a GregorianCalendar with the passed year, month, day, hour, minute, second for the default time zone with the default locale.
  • GregorianCalendar(TimeZone zone, Locale aLocale)- Constructs a GregorianCalendar based on the current time in the given time zone with the given locale.
Calendar cal = new GregorianCalendar(2020, Calendar.JUNE, 17, 17, 45, 17);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
System.out.println("Date- " + sdf.format(cal.getTime()));//17-06-2020 17:45:17 PM

With time-zone information, note that day time saving is not taken into consideration by GregorianCalendar.

Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"), Locale.US);
cal.set(2020, Calendar.JUNE, 17, 17, 45, 17);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a", Locale.US);
System.out.println("Time- " + sdf.format(cal.getTime()));

That's all for this topic Create Date Object in Java With Date and Time Values. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Java Program to Get Current Date and Time
  2. Difference Between Two Dates - Java Program
  3. How to find last modified date of a file in Java
  4. How to Format Date in Java Using SimpleDateFormat
  5. How to Convert a File to Byte Array

You may also like-

  1. Find The Maximum Element in Each Row of a Matrix - Java Program
  2. How to Reverse a Linked List in Java
  3. Connection Interface in Java-JDBC
  4. Var type in Java - Local Variable Type Inference
  5. registerShutdownHook() Method in Spring Framework
  6. Angular Custom Two-Way Data Binding
  7. Spring Web Reactive - Spring WebFlux Example Using Functional Programming
  8. Python Program to Display Prime Numbers