我在应用程序API(DFC)下工作,它给我一个类似日期的对象(IDfTime)。 API会自动将日期转换为本地时区(EST)。我需要转换为UTC才能发送到oracle数据库。出于某种原因,我的代码增加了一个月。我不明白。谁能解释一下呢?
modifyDate
是app API返回的类似日期的对象。我已将其转换为字符串,它看起来像预期的(我的EST)。因此,使用Calendar和SimpleDateFormat将其转换为特定格式的字符串,我可以使用SQL更新语句中的UTC字符串将其发送到数据库。
注意:我正在将调试语句编写到由应用程序而不是stdout生成的批处理作业日志文件中。是的,有更好的方法,但这应该是一个快速的小部件。
//adjust modify date for UTC to send to database
String dfcDatePattern = "mm/dd/yyyy hh:mi:ss";
String javaDatePattern = "MM/dd/yyyy HH:mm:ss";
String oracleDatePattern = "MM/DD/YYYY HH24:MI:SS";
String strModifyDate = modifyDate.asString(dfcDatePattern);
if (m_debugMode) m_File.writeBytes("DFC Modify Date String: " + strModifyDate + NEWLINE);
TimeZone localTimeZone = TimeZone.getDefault();
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeZone(localTimeZone);
if (m_debugMode) m_File.writeBytes("DFC Year: " + modifyDate.getYear() + NEWLINE);
if (m_debugMode) m_File.writeBytes("DFC Month: " + modifyDate.getMonth() + NEWLINE);
if (m_debugMode) m_File.writeBytes("DFC Day: " + modifyDate.getDay() + NEWLINE);
if (m_debugMode) m_File.writeBytes("DFC Hour: " + modifyDate.getHour() + NEWLINE);
if (m_debugMode) m_File.writeBytes("DFC Minutes: " + modifyDate.getMinutes() + NEWLINE);
if (m_debugMode) m_File.writeBytes("DFC Seconds: " + modifyDate.getSeconds() + NEWLINE);
cal.set(modifyDate.getYear(), modifyDate.getMonth(), modifyDate.getDay(), modifyDate.getHour(), modifyDate.getMinutes(), modifyDate.getSeconds());
if (m_debugMode) m_File.writeBytes("Converted Modify Date: " + cal.toString() + NEWLINE);
SimpleDateFormat sdf = new SimpleDateFormat(javaDatePattern);
if (m_debugMode) m_File.writeBytes("Modify Date in Local Time Zone: " + sdf.format(cal.getTime()) + NEWLINE);
sdf.setTimeZone(utcTimeZone);
if (m_debugMode) m_File.writeBytes("Modify Date in UTC Time Zone: " + sdf.format(cal.getTime()) + NEWLINE);
updateQueryString = "execute exec_sql with query = 'update dm_sysobject_s set r_modify_date = TO_DATE(''" + sdf.format(cal.getTime()) + "'',''" + oracleDatePattern + "'') where r_object_id = ''" + objectIdString + "'' '";
输出:
DFC Modify Date String: 03/30/2016 11:44:30 [yep, looks good]
DFC Year: 2016
DFC Month: 3 [month looks good here]
DFC Day: 30
DFC Hour: 11 [EST hour]
DFC Minutes: 44
DFC Seconds: 30
[是的,每个部分看起来都很好]
Converted Modify Date: java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,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=2016,MONTH=3,WEEK_OF_YEAR=14,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=90,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=3,HOUR_OF_DAY=11,MINUTE=44,SECOND=30,MILLISECOND=399,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
[推翻这个以确定我是否能弄清楚发生了什么。我认为一切看起来都不错]
Modify Date in Local Time Zone: 04/30/2016 11:44:30
[不好!时间是正确的,但它增加了一个月!? UTC偏移量仅为4小时!]
Modify Date in UTC Time Zone: 04/30/2016 15:44:30
[不好!时间按预期正确调整,但仍然增加了一个月]
exec_sql to update r_modify_date: execute exec_sql with query = 'update dm_sysobject_s set r_modify_date = TO_DATE(''04/30/2016 15:44:30'',''MM/DD/YYYY HH24:MI:SS'') where r_object_id = ''09ae12f980002934'' '
[这就是我希望SQL看起来像;同一天,但时间调整为UTC偏移。但月份已经过去了。 SQL语法很好(由于API语法看起来很奇怪);它更新了DB中的记录;只是错误的月份。]
我想我可以通过增加四个小时来强制硬编码偏移量,但我认为以上是"正确"办法。为什么它不起作用?
答案 0 :(得分:0)
在java.util.GregorianCalendar中,Month基于零。要表明三月,你应该传递2而不是3。
{{1}}