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:00
⇐ Locale.UK
16/07/2017 19:28:33 EDT
⇐ Locale.US
16/07/2017 19:28:33 HAE
⇐ Locale.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