Skip to content Skip to sidebar Skip to footer

Java Simpledateformat Pattern For Javascript Date

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

Solution 1:

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

Solution 2:

tl;dr

Parsing string.

ZonedDateTime.parse ( 
    "Wed Mar 30201600: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.

Stringinput="Wed Mar 30 2016 00:00:00 GMT-0400 (EDT)"; // EDTDateTimeFormatterf= DateTimeFormatter.ofPattern ( "EEE MMM dd uuuu HH:mm:ss 'GMT'x (z)" ).withLocale ( Locale.US );

ZonedDateTimezdt= 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.

Instantinstant= myJavaUtilDate.toInstant() ;

Adjust into the desired time zone.

ZoneIdz= ZoneId.of( "America/New_York" );
ZonedDateTimezdt= instant.atZone( z );

Generate a string using the same DateTimeFormatter object seen above.

DateTimeFormatterf= DateTimeFormatter.ofPattern ( "EEE MMM dd uuuu HH:mm:ss 'GMT'x (z)" ).withLocale ( Locale.US );
Stringoutput= 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.

Solution 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");
Datedate = newDate();
String dateStr = format.format(date);
TimeZone tz = TimeZone.getDefault();
int offset = tz.getRawOffset();

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

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

Post a Comment for "Java Simpledateformat Pattern For Javascript Date"