date in java code example

Example 1: java date time

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;  
public class CurrentDateTimeExample1 {  
  public static void main(String[] args) {  
   DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
   LocalDateTime now = LocalDateTime.now();
   System.out.println(dtf.format(now));
  }  
}

Example 2: how to create date class in java

// This creates a new Date instance with current system time. Internally It uses System.currentTimeMillis()
Date date = new Date();
// This creates a new Date instance with time that you specify in milliseconds since the epoch time
Date date = new Date(1672556400000L); // This time is Jan 1 2023 in GMT

Example 3: what is T in the date in java

The T is just a literal to separate the date from the time, and the Z means "zero hour offset" also known as "Zulu time" (UTC). If your strings always have a "Z" you can use: SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss. SSS'Z'", Locale.US); format. setTimeZone(TimeZone.07-Dec-2011

Tags:

Java Example