如何使用 Java 处理日历时区? [英] How to handle calendar TimeZones using Java?

查看:27
本文介绍了如何使用 Java 处理日历时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自我的应用程序的时间戳值.用户可以位于任何给定的本地时区.

I have a Timestamp value that comes from my application. The user can be in any given local TimeZone.

由于此日期用于假定给定时间始终为 GMT 的 WebService,因此我需要将用户参数从 (EST) 转换为 (GMT).关键是:用户没有注意到他的 TZ.他输入了他想发送给 WS 的创建日期,所以我需要的是:

Since this date is used for a WebService that assumes the time given is always in GMT, I have a need to convert the user's parameter from say (EST) to (GMT). Here's the kicker: The user is oblivious to his TZ. He enters the creation date that he wants to send to the WS, so what I need is:

用户输入: 2008 年 5 月 1 日下午 6:12(美国东部标准时间)
WS 的参数需要是:2008 年 5 月 1 日下午 6:12 (GMT)

User enters: 5/1/2008 6:12 PM (EST)
The parameter to the WS needs to be: 5/1/2008 6:12 PM (GMT)

我知道默认情况下时间戳总是应该在 GMT 中,但是在发送参数时,即使我从 TS(应该在 GMT 中)创建了我的日历,除非用户是在格林威治标准时间.我错过了什么?

I know TimeStamps are always supposed to be in GMT by default, but when sending the parameter, even though I created my Calendar from the TS (which is supposed to be in GMT), the hours are always off unless the user is in GMT. What am I missing?

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;
}

使用之前的代码,这就是我得到的结果(便于阅读的简短格式):

With the previous Code, this is what I get as a result (Short Format for easy reading):

[2008 年 5 月 1 日晚上 11:12]

[May 1, 2008 11:12 PM]

推荐答案

感谢大家的回复.经过进一步调查,我得到了正确的答案.正如 Skip Head 所提到的,我从我的应用程序中获得的 TimeStamped 正在调整为用户的时区.因此,如果用户输入 6:12 PM (EST),我将得到 2:12 PM (GMT).我需要的是一种撤销转换的方法,这样用户输入的时间就是我发送到 WebServer 请求的时间.这是我如何实现的:

Thank you all for responding. After a further investigation I got to the right answer. As mentioned by Skip Head, the TimeStamped I was getting from my application was being adjusted to the user's TimeZone. So if the User entered 6:12 PM (EST) I would get 2:12 PM (GMT). What I needed was a way to undo the conversion so that the time entered by the user is the time I sent to the WebServer request. Here's how I accomplished this:

// 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)

The code's output is: (User entered 5/1/2008 6:12PM (EST)

当前用户的时区:EST
当前与 GMT 的偏移量(以小时为单位):-4(通常为 -5,DST 调整除外)
来自 ACP 的 TS:2008-05-01 14:12:00.0
使用 GMT 和 US_EN Locale 从 TS 转换的日历日期:5/1/08 6:12 PM (GMT)

Current User's TimeZone: EST
Current Offset from GMT (in hrs):-4 (Normally -5, except is DST adjusted)
TS from ACP: 2008-05-01 14:12:00.0
Calendar Date converted from TS using GMT and US_EN Locale: 5/1/08 6:12 PM (GMT)

这篇关于如何使用 Java 处理日历时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆