tl;dr
Parsing string.
ZonedDateTime.parse (
"Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)" ,
DateTimeFormatter.ofPattern ( "EEE MMM dd uuuu HH:mm:ss 'GMT'Z (z)" ).withLocale ( Locale.US )
)
Generating string.
myJavaUtilDate.toInstant()
.atZone( ZoneId.of( "America/New_York" ) )
.format(
DateTimeFormatter.ofPattern ( "EEE MMM dd uuuu HH:mm:ss 'GMT'Z (z)" )
.withLocale ( Locale.US )
)
java.time
You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.
Parsing strings
Define a formatting pattern with DateTimeFormatter
class.
The modern pattern codes are similar to the legacy SimpleDateFormat
codes, but not exactly identical. Study the documentation. The subtleties of the zone/offset pattern codes eludes me, but some trial-and-error found a combination that seems to work. Test thoroughly, as I have not; use at your own risk.
String input = "Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)"; // EDT
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EEE MMM dd uuuu HH:mm:ss 'GMT'x (z)" ).withLocale ( Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse ( input , f );
Generate a String in standard ISO 8601 format.
System.out.println ( "zdt.toString(): " + zdt );
zdt.toString(): 2016-03-30T00:00-04:00[America/New_York]
By the way, if at all possible I suggest you use the standard ISO 8601 formats rather than the format shown in your Question. Much easier to parse and easier to read by humans across cultures.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or EDT
or IST
as they are not true time zones, not standardized, and not even unique(!).
Generating strings
Convert any java.util.Date
to its modern equivalent, Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
To convert, look to new methods added to the old classes.
Instant instant = myJavaUtilDate.toInstant() ;
Adjust into the desired time zone.
ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = instant.atZone( z );
Generate a string using the same DateTimeFormatter
object seen above.
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EEE MMM dd uuuu HH:mm:ss 'GMT'x (z)" ).withLocale ( Locale.US );
String output = zdt.format( f );
Live code
See this example code live in IdeOne.com.
String input = "Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)"; // EDT
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EEE MMM dd uuuu HH:mm:ss 'GMT'Z (z)" ).withLocale ( Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse ( input , f );
String output = zdt.format ( f );
System.out.println ( "input: " + input );
System.out.println ( "zdt.toString(): " + zdt );
System.out.println ( "output: " + output );
System.out.println ( "input equals output: " + input.equals ( output ) );
input: Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)
zdt.toString(): 2016-03-30T00:00-04:00[America/New_York]
output: Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)
input equals output: true
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.