我有一个来自我的应用程序的时间戳值。用户可以在任何给定的本地TimeZone中。
由于此日期用于假定给定的时间总是在GMT中的WebService,所以我需要将用户的参数从例如(EST)转换为(GMT)。关键在于:用户对他的TZ置若罔闻。他输入要发送给WS的创建日期,所以我需要的是:
用户输入:5/1/20086:12(东部时间)
WS的参数需要是:5/1/2008 6:12 (格林尼治时间)
我知道默认情况下,TimeStamps总是在GMT中,但是当发送参数时,即使我从TS (应该在GMT中)创建我的日历,除非用户在GMT中,否则时间总是关闭的。我遗漏了什么?
Timestamp issuedDate = (Timestamp) getACPValue(inputs_, "issuedDate");
Calendar issueDate = convertTimestampToJavaCalendar(issuedDate);
...
private static java.util.Calendar convertTimestampToJavaCalendar(Timestamp ts_) {
java.util.Calendar cal = java.util.Calendar.getInstance(
GMT_TIMEZONE, EN_US_LOCALE);
cal.setTimeInMillis(ts_.getTime());
return cal;
}在前面的代码中,这就是我得到的结果(简单阅读的简短格式):
2008年5月1日晚上11:12
发布于 2008-10-23 18:03:18
谢谢大家的回应。经过进一步的调查,我找到了正确的答案。正如Skip所提到的,我从我的应用程序中获得的TimeStamped正在被调整为用户的TimeZone。因此,如果用户输入6:12 PM (EST),我将得到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‘s (EST) )
当前用户的TimeZone:
来自格林尼治时间的电流偏移(小时):-4(通常为-5,DST调整除外)
机场核心计划TS : 2008-05-01 14:12:00.0
使用格林尼治时间和US_EN地区从TS转换的日历日期: 5/1/08 6:12 (格林尼治时间)
发布于 2008-10-23 16:17:38
public static Calendar convertToGmt(Calendar cal) {
Date date = cal.getTime();
TimeZone tz = cal.getTimeZone();
log.debug("input calendar has date [" + date + "]");
//Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
long msFromEpochGmt = date.getTime();
//gives you the current offset in ms from GMT at the current date
int offsetFromUTC = tz.getOffset(msFromEpochGmt);
log.debug("offset is " + offsetFromUTC);
//create a new calendar in GMT timezone, set to this date and add the offset
Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.setTime(date);
gmtCal.add(Calendar.MILLISECOND, offsetFromUTC);
log.debug("Created GMT cal with date [" + gmtCal.getTime() + "]");
return gmtCal;
}如果我传递当前时间(来自Calendar.getInstance()的“EDT 12:09:05”),输出如下:
调试-输入日历日期为清华10月23日12:09:05 调试-偏移量为-14400000 调试创建的GMT cal,日期为清华010月23日08:09:05
格林尼治时间12:09:05是东部时间8:09:05。
这里令人困惑的地方是,Calendar.getTime()在当前时区中返回一个Date,而且也没有方法来修改日历的时区并滚动底层日期。取决于web服务所采用的参数类型,您可能只想让WS处理时间间隔为毫秒。
发布于 2009-02-02 12:41:36
您说日期是用于web服务的,所以我假设它在某个时候被序列化为一个字符串。
如果是这样的话,您应该看看setTimeZone法类的DateFormat。这会指示打印时间戳时将使用的时区。
一个简单的例子:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar cal = Calendar.getInstance();
String timestamp = formatter.format(cal.getTime());https://stackoverflow.com/questions/230126
复制相似问题