最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Java SimpleDateFormat Pattern for JavaScript Date - Stack Overflow

programmeradmin3浏览0评论

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 are .toString()'d and have a format like:

Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)

And I have a Java Date object that I am trying to convert to a string of the same format. So far my SimpleDateFormat pattern is

EEE MMM dd yyyy '00:00:00' (The hours, minutes and secs will always be 0) but I can't figure out the proper pattern for the time zone (GMT-0400 (EDT))

Thanks for the help! Here is the Java code I'm using so far in case it helps:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy '00:00:00'");
Date date = arrayOfDates[i];
String dateStr = format.format(date);
// Current dateStr = "Wed Mar 30 2016 00:00:00"

EDIT

Vaibhav Jain's answer put me on the right track (Java SimpleDateFormat Pattern for JavaScript Date)

The final format I ended up using is:

EEE MMM dd yyyy '00:00:00' 'GMT'Z '('z')'

Thanks again, all!

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 are .toString()'d and have a format like:

Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)

And I have a Java Date object that I am trying to convert to a string of the same format. So far my SimpleDateFormat pattern is

EEE MMM dd yyyy '00:00:00' (The hours, minutes and secs will always be 0) but I can't figure out the proper pattern for the time zone (GMT-0400 (EDT))

Thanks for the help! Here is the Java code I'm using so far in case it helps:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy '00:00:00'");
Date date = arrayOfDates[i];
String dateStr = format.format(date);
// Current dateStr = "Wed Mar 30 2016 00:00:00"

EDIT

Vaibhav Jain's answer put me on the right track (Java SimpleDateFormat Pattern for JavaScript Date)

The final format I ended up using is:

EEE MMM dd yyyy '00:00:00' 'GMT'Z '('z')'

Thanks again, all!

Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Mar 2, 2016 at 16:17 julianwyzjulianwyz 3,1822 gold badges32 silver badges36 bronze badges 3
  • just to clarify arrayOfDates[i] has dates in the format of Wed Mar 30 2016 00:00:00 GMT-0400 (EDT) or is it in milliseconds? – depperm Commented Mar 2, 2016 at 16:19
  • @depperm arrayOfDates is of type java.util.Date[] and holds dates that need to be parsed into strings with the format Wed Mar 30 2016 00:00:00 GMT-0400 (EDT) (obviously not this exact date, it would be whatever the date of the arrayOfDates[i] is. Hope that helps clarify things. – julianwyz Commented Mar 2, 2016 at 16:25
  • 2 Note that in javascript (actually ECMAScript), Date.prototype.toString is entirely implementation dependent, so while there might be some monality between implementations, there is no certainty of conformance to any particular format. – RobG Commented Mar 3, 2016 at 3:22
Add a ment  | 

4 Answers 4

Reset to default 6
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy '00:00:00'  zZ");
Date date = arrayOfDates[i];
String dateStr = format.format(date);

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 bination 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..

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 ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

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;
    }
发布评论

评论列表(0)

  1. 暂无评论