I was simply trying to convert the instance of XMLGregorianCalendar
(which I got from JAXWS) to GregorianCalendar
in specific TimeZone using below code.
The date is coming in EST and I want to convert it into GMT for further saving to DB
//soap response <ns4:TimeStamp>2016-06-18T04:43:54-04:00</ns4:TimeStamp>
//dtime is what i got from JAXB for the above date, so I wrote::
Date date = dTime.toGregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US, null).getTime();
System.out.println(date);
Output: Sat Jun 18 14:13:54 IST 2016
Since above is not working as expected so i tried DateFormat and its giving the expected result.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
GregorianCalendar gc = dTime.toGregorianCalendar();
System.out.println(df.format(gc.getTime()));
Output: 2016-06-18 08:43:54 +0000
What could be the issue here as toGregorianCalendar(...) is not giving the desired result?
Also I noticed the GregorianCalendar instance obtained above from toGregorianCalendar has fieldSet= false. Not sure if this is causing the issue.
java.util.GregorianCalendar[time=1468382241000,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2016,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=12,DAY_OF_YEAR=194,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=57,SECOND=21,MILLISECOND=0,ZONE_OFFSET=-14400000,DST_OFFSET=0]
Any help will be appreciated..!!