I need to convert a Java Date object to a string that is the same format as JavaScript Dates when they are converted to a string. On our server we have JavaScript dates that
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy '00:00:00' zZ");
Date date = arrayOfDates[i];
String dateStr = format.format(date);
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 )
)
You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.
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(!).
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 );
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
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?
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.
To format the date exactly like that, we may need to format date and timezone separately, e,g,:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss");
Date date = new Date();
String dateStr = format.format(date);
TimeZone tz = TimeZone.getDefault();
int offset = tz.getRawOffset();
String text = String.format("%s%02d%02d", offset >= 0 ? "+" : "-", offset / 3600000, (offset / 60000) % 60);
String timeZoneText = "GMT" + text + " (" + tz.getDisplayName(true, tz.SHORT, Locale.getDefault())+")";
System.out.println(dateStr + " " + timeZoneText);
Typically if a given date is valid then the date object created should return back the same date, month and year value. Below logic worked for most of the cases that I coded.
Example: 29-Feb-2018 does not exist, but when javascript accepts this input as Date("2018-02-29") it creates a date value of 1-Mar-2018.
Javascript Function:
function dateCheck(dd,mm,yyyy) { var tempDt = new Date(parseInt(yyyy), parseInt(mm)-1, parseInt(dd), 0, 0, 0, 0); if(tempDt.getDate()!=parseInt(dd) || tempDt.getFullYear()!=parseInt(yyyy) || tempDt.getMonth() + 1 !=parseInt(mm)) return false; else return true; }