Java SimpleDateFormat Pattern for JavaScript Date

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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!

回答1:

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


回答2:

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?

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.



回答3:

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);


回答4:

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;     }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!