我从应用程序中获取的时间戳已调整为用户的TimeZone。因此,如果用户输入的是美国东部标准时间下午6:12,我将获得格林尼治标准时间2:12。我需要的是一种撤消转换的方法,这样用户输入的时间就是我发送到WebServer请求的时间。这是我的完成方式:
// Get TimeZone of user
TimeZone currentTimeZone = sc_.getTimeZone();
Calendar currentDt = new GregorianCalendar(currentTimeZone, EN_US_LOCALE);
// Get the Offset from GMT taking DST into account
int gmtOffset = currentTimeZone.getOffset(
currentDt.get(Calendar.ERA),
currentDt.get(Calendar.YEAR),
currentDt.get(Calendar.MONTH),
currentDt.get(Calendar.DAY_OF_MONTH),
currentDt.get(Calendar.DAY_OF_WEEK),
currentDt.get(Calendar.MILLISECOND));
// convert to hours
gmtOffset = gmtOffset / (60*60*1000);
System.out.println("Current User's TimeZone: " + currentTimeZone.getID());
System.out.println("Current Offset from GMT (in hrs):" + gmtOffset);
// Get TS from User Input
Timestamp issuedDate = (Timestamp) getACPValue(inputs_, "issuedDate");
System.out.println("TS from ACP: " + issuedDate);
// Set TS into Calendar
Calendar issueDate = convertTimestampToJavaCalendar(issuedDate);
// Adjust for GMT (note the offset negation)
issueDate.add(Calendar.HOUR_OF_DAY, -gmtOffset);
System.out.println("Calendar Date converted from TS using GMT and US_EN Locale: "
+ DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
.format(issueDate.getTime()));
代码的输出为:(用户输入时间:5/1/2008 6:12 PM(美国东部标准时间)
当前用户的时区:EST 与GMT的当前偏移量(以小时为单位):-4(通常为-5,DST调整除外) ACP的TS:2008-05-01 14:12: 00.0日历日期使用GMT和US_EN语言环境从TS转换而来:08/5/1下午6:12(GMT)