4

I have a date input with a timezone of EST.

I'm parsing it to a ZonedDateTime, but when formatting, it's being displayed as GMT-04:00, rather than EST as I'd expect.

Why is this happening - can I get it to display the timezone I expect?

Example

final String dateString = "16/07/2017 19:28:33 EST";
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss z");

System.out.println(MessageFormat.format("Input - {0}", dateString));
System.out.println(MessageFormat.format("Parsed - {0}", formatter.format(ZonedDateTime.parse(dateString, formatter))));

Output:

Input - 16/07/2017 19:28:33 EST
Parsed - 16/07/2017 19:28:33 GMT-04:00

My time zone is GMT. AdoptOpenJDK 14.0.2.12, en_GB locale.

For context, I'm trying to better understand this issue in recent versions of Amazon Corretto 11 - https://github.com/corretto/corretto-11/issues/147

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Jakg
  • 922
  • 12
  • 39

2 Answers2

3

tl;dr

You said:

it's being displayed as GMT-04:00, rather than EST as I'd expect.

Such details depend on whether Locale.UK is in effect or not.

  • 16/07/2017 19:28:33 GMT-04:00Locale.UK
  • 16/07/2017 19:28:33 EDTLocale.US
  • 16/07/2017 19:28:33 HAELocale.CANADA_FRENCH

➥ For textual date-time data exchange, (a) stick with the ISO 8601 standard formats, and (b) use an offset of zero whenever practical. Doing that makes moot Issue # 147.

Details

By the way, try hard to avoid parsing date-time text using the 2-4 letter pseudo-zones like EST, EDT, IST, CST. These are non-standard, non-unique(!), weak attempts at giving a hint as to the time zone and whether Daylight Saving Time (DST) is in effect.

Use only real time zones, in format of Continent/Region such as Europe/London.


You said:

rather than EST as I'd expect

Daylight Saving Time (DST) was in effect on that date-time in time zone of America/New_York, not Standard Time. So you should be expecting EDT rather than EST.


I suggest you not complicate matters by embedding calls within your println statements.

You should specify the Locale rather than rely implicitly on the JVM’s current default locale which can change at any moment during runtime. So notice how I appended .withLocale( locale ) to the formation of the DateTimeFormatter object. I specified Locale.UK because you mentioned en_GB in a Comment.

So let's modify your code to this:

final Locale locale = Locale.US ;  // Locale.UK versus Locale.US yield different outputs.
final String input = "16/07/2017 19:28:33 EST";
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/yyyy HH:mm:ss z" ).withLocale( locale ) ;
final ZonedDateTime zdt = ZonedDateTime.parse( input , formatter ) ;
final String zdtToString = zdt.toString() ;
final String zdtFormatted = zdt.format( formatter ) ;

… and:

// Dump to console.
System.out.println( "input: " + input ) ;
System.out.println( "zdt: " + zdt ) ;
System.out.println( "zdtToString: " + zdtToString ) ;
System.out.println( "zdtFormatted: " + zdtFormatted ) ;

See code run live at IdeOne.com on Java 12.

input: 16/07/2017 19:28:33 EST
zdt: 2017-07-16T19:28:33-04:00[America/New_York]
zdtToString: 2017-07-16T19:28:33-04:00[America/New_York]
zdtFormatted: 16/07/2017 19:28:33 GMT-04:00

If we change that locale from Locale.UK to Locale.US, we get a different result.

zdtFormatted: 16/07/2017 19:28:33 EDT
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

tl;dr

Never use a date-time parser/formatter type (class) without a Locale.

import java.text.MessageFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        final String dateString = "16/07/2017 19:28:33 EST";
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss z", Locale.ENGLISH);

        System.out.println(MessageFormat.format("Input - {0}", dateString));
        System.out.println(
                MessageFormat.format("Parsed - {0}", formatter.format(ZonedDateTime.parse(dateString, formatter))));
    }
}

Output:

Input - 16/07/2017 19:28:33 EST
Parsed - 16/07/2017 19:28:33 EDT

ONLINE DEMO

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110