谢谢大家的回复。经过进一步的调查,我得到了正确的答案。正如 Skip Head 所提到的,我从应用程序中获得的时间戳正在调整为用户的时区。因此,如果用户输入 6:12 PM (EST) 我会得到 2:12 PM (GMT)。我需要的是一种撤消转换的方法,这样用户输入的时间就是我发送到 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:12PM (EST)
当前用户的时区:EST
当前与 GMT 的偏移量(以小时为单位):-4(通常为 -5,DST 调整除外)
ACP 的 TS:2008-05-01 14:12:00.0
使用 GMT 和 US_EN 区域设置从 TS 转换的日历日期: 2008 年 5 月 1 日下午 6:12(格林威治标准时间)