Date and Time in Java 8

  • How to get current date and/or time in Java ?  //Output: 2021-06-20  //Output: 23:22:17.939243900  //Output: 2021-06-20T23:13:37.193359900  //Output: 2021-06-20T23:14:21.295180600-04:00[America/New_York] 
  • How to convert a String to date and time in Java ?
public static LocalDateTime StringToDate(){
String date = "01-October-2021 23:59:00.123";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMMM-yyyy HH:mm:ss.SSS");
LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter);
return localDateTime;

For date strings with ‘T’ if you face the below exception:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-09-30T23:50:36.4259546' could not be parsed at index 10

add ‘T’ as shown in the below format:

  public static void main(String args[]){
        String now = "2021-09-30T23:50:36.4259546";
        System.out.println(" " + LocalDateTime.parse(now, 
//OUTPUT: 2021-09-30T23:50:36.425954600
  • How to create a Date Time instance with a previous date time value ?

Either you can parse the String as shown above. Or use the “of” factory method.

//of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)


  • How to specify milliseconds / microseconds / nanoseconds in DateTimeFormatter in Java ?
 Formatter SymbolFormatted examples.
Day of monthd/dd2/02
Hour in day(0-23)H/HH2/02 , 14/14
Hour in am/pm (1-12)h/hh4/04
TimeZonez / zzzz / VVEDT / Eastern Daylight Time /America/New_York
EraG/GGGGAD/ Anno Domini
DateTimeFormatter symbols / pattern.

  • Are Java8 Date/Time APIs thread safe ?

Yes. They are thread safe and that is one of main advantages of using Java8 Date/Time APIs against the APIs of previous java versions (Java7 and below).

  • How to print date/time in a specific format ?
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMMM-yyyy HH:mm:ss.n zzzz VV GGGG");

//Output: 20-June-2021 23:42:43.646265900 Eastern Daylight Time America/New_York Anno Domini
  • How to convert a Date Time to a different Time Zone ?

ZonedDateTime preserves the Time Zone Data. If you want to change the timezone to a different time zone, use “withZoneSameInstant” api.

System.setProperty("user.timezone", "UTC");
ZonedDateTime zonedDateTimeUTC =;

System.out.println(zonedDateTimeUTC);    //Output: 2021-06-27T03:44:06.031687100Z[UTC]

ZonedDateTime zonedDateTimeInEastern = zonedDateTimeUTC.withZoneSameInstant(ZoneId.of("US/Eastern"));

System.out.println(zonedDateTimeInEastern); //Output: 2021-06-26T23:44:06.031687100-04:00[US/Eastern]

List of all ZoneIds in java can be found here.

  • How to get epoch milliseconds from LocalDateTime ? And how to convert milliseconds to LocalDateTime ?
public static void main(String args[]){
    LocalDateTime ldt = LocalDateTime.of(2021, 8, 23, 11, 14, 10);

public static long toMilliSeconds(LocalDateTime ldt){
    return ldt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

public static LocalDateTime toLocalDateTime(long milliseconds){
    return Instant.ofEpochMilli(milliseconds).atZone(ZoneId.systemDefault()).toLocalDateTime();

// Output: 

%d bloggers like this: