Answers for "ZonedDateTime get time java"

1

java zoneddatetime

LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println("ZoneId.systemDefault(): " + ZoneId.systemDefault());

// convert LocalDateTime to ZonedDateTime, with default system zone id
ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault());

// convert LocalDateTime to ZonedDateTime, with specified zoneId
ZonedDateTime europeDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/Kaliningrad"));
System.out.println(europeDateTime);

// convert LocalDateTime to ZonedDateTime, with specified off set
ZonedDateTime offSetNegative5 = now.atOffset(ZoneOffset.of("-05:00")).toZonedDateTime();
System.out.println(offSetNegative5);

// display all zone ids
//ZoneId.getAvailableZoneIds().forEach(System.out::println);

// output
/*
2021-03-23T16:43:32.010069453
ZoneId.systemDefault(): Asia/Kuala_Lumpur
2021-03-23T10:43:32.010069453+02:00[Europe/Kaliningrad]
2021-03-23T16:43:32.010069453-05:00
*/
Posted by: Guest on July-05-2021
1

get date by timezone java

Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// Use Madrid's time zone to format the date in
df.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));

System.out.println("Date and time in Madrid: " + df.format(date));
Posted by: Guest on July-23-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language