Java and SOAP data client request as UTC time and save as EST time

I have a soap client request that I need to convert the date response into EST time.

On my screen, I selected: 11:45am and I want to save 11:45 am in the database.

But the SOAP request comes in as:

2012-11-24T16:45:00.000Z

In java code, the date prints as:

Sat Nov 24 11:45:00 EST 2012 ...

Yet we make another web-service call which eventually saves to the database (SQL Server): 2012-11-24 16:45

Calendar incomingWebServiceCalendarObject = fromWebService.getDateTime()

Calendar outgoingWebServiceCalendarObject = incomingWebServiceCalendarObject;
webServiceBean.setDateTime(outgoingWebServiceCalendarObject);

... How can I save as 2012-11-24 11:45?

Also, here is the gregorian calendar:

java.util.GregorianCalendar[time=1353775500000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2012,MONTH=10,WEEK_OF_YEAR=47,WEEK_OF_MONTH=4,DAY_OF_MONTH=24,DAY_OF_YEAR=329,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=45,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-18000000,DST_OFFSET=0]

...

I did the following and this appears to work, is this a proper approach, what is the code trying to accomplish based on my requirement?

final long offset = this.secondaryScheduleTime.getTimeInMillis() + TimeZone.getTimeZone("EST").getRawOffset();
final Date estTime = new Date(offset);
final Calendar c2 = Calendar.getInstance();
c2.setTime(estTime);

Your initial SOAP request is coming in as UTC ( .000Z is your timezone information), after which your Calendar object converts it to Eastern time ,zone=sun.util.calendar.ZoneInfo[id="America/New_York"... for display and then you are persisting the original SOAP information to your database.

What you don't show is how you are persisting your database and with which timestamp.

I can think of only 2 situations you have going on:

  • Your database is in UTC only and no timezone information is saved. This means that each call to/from your DB is always UTC and timezones are left up to your code.
  • Your database saves timezone info via the Timestamp datatype, but you are sending the UTC information from your SOAP request to your database, not your local Calendar object.
  • I think the solution you provided satisfies #2 on the list above, but again, without seeing any other details I think it will be hard to determine for sure.

    I would suggest dropping some break points and stepping through your code to see exactly when/where your date objects are being created, and with what information.

    链接地址: http://www.djcxy.com/p/948.html

    上一篇: 使用Java从MySql DATETIME字段中打印日期和时间

    下一篇: Java和SOAP数据客户端请求为UTC时间并保存为EST时间