bharathi bharathi - 1 year ago 157
Java Question

convert date from one format to another in java

I need to convert a date from one format to another in java. Here I am not converting the date from one format to another format. First I have to retrieve from XMLGregorianCalendar and then I have to convert it to yyyyDDDHHmmssSSS which should be same as the below explained by hard coding.


I have hard code a value in string and then I try to convert to date and then to back to original value(yyyyDDDHHmmssSSS) format, I am getting the expected value as below.

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyDDDHHmmssSSS");
String t ="20160690600530";
Date dateq = sdf1.parse(t);
System.out.println("After converting"+ dateq );

Output: (Expected one)

Wed Mar 09 06:00:53 EST 2016

Same way I am trying to convert a value from the XMLGregorianCalendar to yyyyDDDHHmmssSSS, but I am getting a different value from the above.

Pojo ddd = new Pojo();
XMLGregorianCalendar gregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2016-03-09T06:00:53.0Z");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyDDDHHmmssSSS");
Date date1 = ddd.getMemberEffectiveTimestamp().toGregorianCalendar().getTime();
System.out.println("sss-->"+ sdf1.format(date1));


date1--->Wed Mar 09 01:00:53 EST 2016

I need the "2016-03-09T06:00:53.0Z" to convert to 2016069010053000 value.
I am not sure how to achieve it.

Answer Source

Avoid old date-item classes

You are using outmoded old date-time classes bundled with the earliest versions of Java. They have proven to be poorly designed, confusing, and troublesome. Avoid them.


Java 8 and later comes with the java.time framework built-in. A vast improvement!

In java.time, an Instant is a moment on the timeline in UTC.

Your input string complies with the ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating string representations of their date-time values. So no need to specify a formatting pattern. The Instant class can directly parse such a string.

String input = "2016-03-09T06:00:53.0Z";
Instant instant = Instant.parse ( input );

An Instant is only intended as a basic building block. So for generating formatted strings we must convert to an OffsetDateTime object. You apparently want the same time zone of UTC, so we specify the constant ZoneOffset.UTC.

OffsetDateTime odt = OffsetDateTime.ofInstant ( instant , ZoneOffset.UTC );

We define a DateTimeFormatter to use your pattern. The reader should note the desired pattern uses the uppercase-D DDD pattern which means day-of-year (a count between 1 and 365/366).

DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyyDDDHHmmssSSS" );
String output = odt.format ( formatter );

Dump to console.

System.out.println ( "input: " + input + " = instant: " + instant + " = odt: " + odt + " = output: " + output );

When run.

input: 2016-03-09T06:00:53.0Z = instant: 2016-03-09T06:00:53Z = odt: 2016-03-09T06:00:53Z = output: 2016069060053000

Caution: I strongly recommend against generating such strings lacking any offset-from-UTC or time zone information. That is as unwise as transmitting a currency amount without noting which currency (USD, CAD, MXN, etc.).

Such serializations of date-time values to strings should stick to the sensible and intuitive formats defined by ISO 8601 whenever possible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download