Syntax error Java Program to convert java.util.Date to any local date in certain timezone

Java Program to convert java.util.Date to any local date in certain timezone



First, set the Date and ZoneId −

Date date = new Date();
ZoneId zone = ZoneId.systemDefault();

Now convert the java.util.date to localdate −

date.toInstant().atZone(zone).toLocalDate()
date.toInstant().atZone(zone).toLocalTime()
date.toInstant().atZone(zone).getHour()
date.toInstant().atZone(zone).getMinute()
date.toInstant().atZone(zone).getSecond()

Example

import java.time.ZoneId;
import java.util.Date;
public class Demo {
   public static void main(String[] args) {
      Date date = new Date();
      ZoneId zone = ZoneId.systemDefault();
      System.out.println("LocalDate = "+date.toInstant().atZone(zone).toLocalDate());
      System.out.println("LocalTime= "+date.toInstant().atZone(zone).toLocalTime());
      System.out.println("Hour = "+date.toInstant().atZone(zone).getHour());
      System.out.println("Minute = "+date.toInstant().atZone(zone).getMinute());
      System.out.println("Seconds = "+date.toInstant().atZone(zone).getSecond());
   }
}

Output

LocalDate = 2019-04-18
LocalTime= 23:25:09.708
Hour = 23
Minute = 25
Seconds = 9
Updated on: 2019-07-30T22:30:25+05:30

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements