January 3, 2023

Create a Date Object With Values in Java

In your application you may need to show current date and time or you may have to create a date object with values where you specify the year, month, day and even hour, minute and seconds. In this post we’ll see how to create Date object with values in Java.

Though it is easy to do that with new Date and Time API classes residing in java.time like LocalTime, LocalDate, LocalDateTime and ZonedDateTime but if you have to use java.util classes then you can also get a Date object with values using Date and GregorianCalendar classes.

Date object using GregorianCalendar

In GregorianCalendar class there are following three constructors for creating an instance with values for year, month, day of month, hour, minute, second.

  • GregorianCalendar(int year, int month, int dayOfMonth)- Constructs a GregorianCalendar with the given date set 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 given date and time set 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 given date and time set for the default time zone with the default locale.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class FormatDate {
  public static void main(String[] args) {
    // Creating GregorianCalendar with values
    GregorianCalendar calendar = new GregorianCalendar(2019, Calendar.FEBRUARY, 20, 18, 9, 22);
    // Getting Date object
    Date date = calendar.getTime();
    
    System.out.println("DateTime is- " + date);
    // Specifying pattern	
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
    // Format date 
    System.out.println("Time in 24 Hour format - " + sdf.format(date));
  }
}

Creating Date object using new Date and Time API

In the new Date and Time API class you can use the static method of() to pass values for date creation.

Using LocalDate

If you want to create only date with no time values then you can use java.time.LocalDate

LocalDate date = LocalDate.of(2019, Month.APRIL, 3);
System.out.println("Date - " + date); // Date – 2019-04-03
Using LocalTime

If you want to specify only time values then you can use java.time.LocalTime

LocalTime time = LocalTime.of(17, 8, 32);
System.out.println("Time - " + time); //Time – 17:08:32
Using LocalDateTime

If you want to specify both date and time values then you can use java.time.LocalDateTime

LocalDateTime dateTime = LocalDateTime.of(2018, Month.DECEMBER, 23, 13, 18, 23);
System.out.println("Date Time - " + dateTime);//2018-12-23T13:18:23
Using ZonedDateTime

Above mentioned classes doesn’t have time-zone information. If you want time-zone information then use ZonedDateTime and set the appropriate zone id.

ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.of(2018, Month.DECEMBER, 23, 13, 18, 23), ZoneId.of("Europe/Paris"));
System.out.println("Zoned time - " + zdt); //2018-12-23T13:18:23+01:00[Europe/Paris]

That's all for the topic Create a Date Object With Values in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment