首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Java处理日历TimeZones?

如何使用Java处理日历TimeZones?
EN

Stack Overflow用户
提问于 2008-10-23 15:15:48
回答 9查看 230.3K关注 0票数 96

我有一个来自我的应用程序的时间戳值。用户可以在任何给定的本地TimeZone中。

由于此日期用于假定给定的时间总是在GMT中的WebService,所以我需要将用户的参数从例如(EST)转换为(GMT)。关键在于:用户对他的TZ置若罔闻。他输入要发送给WS的创建日期,所以我需要的是:

用户输入:5/1/20086:12(东部时间)

WS的参数需要是:5/1/2008 6:12 (格林尼治时间)

我知道默认情况下,TimeStamps总是在GMT中,但是当发送参数时,即使我从TS (应该在GMT中)创建我的日历,除非用户在GMT中,否则时间总是关闭的。我遗漏了什么?

代码语言:javascript
复制
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

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2008-10-23 18:03:18

谢谢大家的回应。经过进一步的调查,我找到了正确的答案。正如Skip所提到的,我从我的应用程序中获得的TimeStamped正在被调整为用户的TimeZone。因此,如果用户输入6:12 PM (EST),我将得到2:12 (格林尼治标准时间)。我需要的是一种撤销转换的方法,这样用户输入的时间就是我发送到WebServer请求的时间。我是如何做到这一点的:

代码语言:javascript
复制
// 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 (格林尼治时间)

票数 29
EN

Stack Overflow用户

发布于 2008-10-23 16:17:38

代码语言:javascript
复制
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处理时间间隔为毫秒。

票数 62
EN

Stack Overflow用户

发布于 2009-02-02 12:41:36

您说日期是用于web服务的,所以我假设它在某个时候被序列化为一个字符串。

如果是这样的话,您应该看看setTimeZone法类的DateFormat。这会指示打印时间戳时将使用的时区。

一个简单的例子:

代码语言:javascript
复制
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());
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/230126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档