Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a ZonedDateTime to a String?

I want to convert a ZonedDateTime to a String in the format of ("dd/MM/yyyy - hh:mm"). I know this is possible in Joda-Time other types, just using their toString("dd/MM/yyyy - hh:mm")....But this doesn't work with ZonedDateTime.toString().

How can I format a ZonedDateTime to a String?


EDIT:

I tried to print the time in another timezone and the result appears to be the same always:

ZonedDateTime date = ZonedDateTime.now();
ZoneId la = ZoneId.of("America/Los_Angeles");
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la);

// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date));
// same result as the previous one
// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date2));

And I am not in the same timezone as Los Angeles.


EDIT 2:

Found how to change the timezones:

// Change this:
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la); // incorrect!
// To this:
ZonedDateTime date2 = date.withZoneSameInstant(la);
like image 971
Michel Feinstein Avatar asked Feb 23 '17 20:02

Michel Feinstein


People also ask

What is the format of ZonedDateTime?

Class ZonedDateTime. A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris . ZonedDateTime is an immutable representation of a date-time with a time-zone.

How do I convert ZonedDateTime from one zone to another?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

How do I get only date from ZonedDateTime?

You can convert ZonedDateTime to an instant, which you can use directly with Date. No, it will be the current Date on your zone system default.

Is ZonedDateTime a UTC?

A ZonedDateTime represents a date-time with a time offset and/or a time zone in the ISO-8601 calendar system. On its own, ZonedDateTime only supports specifying time offsets such as UTC or UTC+02:00 , plus the SYSTEM time zone ID.


2 Answers

You can use java.time.format.DateTimeFormatter. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Here there is an example

ZonedDateTime date = ZonedDateTime.now();  System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date)); 
like image 190
reos Avatar answered Oct 02 '22 19:10

reos


Many thanks for above. Here it is in scala where localDateTime.now always Zulu/UTC time.

import java.time.format.DateTimeFormatter import java.time.LocalDateTime import java.time.ZoneId  val ny = ZoneId.of("America/New_York") val utc = ZoneId.of("UTC") val dateTime = LocalDateTime.now.atZone(utc)  val nyTime = DateTimeFormatter.       ofPattern("yyyy-MMM-dd HH:mm z").       format(dateTime.withZoneSameInstant(ny)) 
like image 26
Tony Fraser Avatar answered Oct 02 '22 19:10

Tony Fraser