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

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

问题描述

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



由于此日期用于WebService,假定时间总是在GMT,我需要转换用户的参数从(EST)到(GMT)。这里是踢球人:用户不知道他的TZ。他输入了他要发送给WS的创建日期,所以我需要的是:



用户输入: 5/1/2008 6:12 PM(EST)

WS的参数需要:5/1/2008 6:12 PM(GMT)



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

  Timestamp issuedDate =(Timestamp)getACPValue(inputs_,issuedDate); 
日历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]

解决方案

感谢大家的回应。经过进一步调查,我得到了正确的答案。正如Skip Head所提到的,TimeStamped我从我的应用程序得到的正在调整到用户的TimeZone。所以如果用户输入6:12 PM(EST),我会得到下午2:12(GMT)。我需要的是一种撤销转换的方法,以便用户输入的时间是我发送到WebServer请求的时间。这是我完成这个的方式:

  //获取用户的TimeZone 
TimeZone currentTimeZone = sc_.getTimeZone
Calendar currentDt = new GregorianCalendar(currentTimeZone,EN_US_LOCALE);
//从DST获取偏移量,将DST考虑在内
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 。毫秒));
// convert to hours
gmtOffset = gmtOffset /(60 * 60 * 1000);
System.out.println(Current User's TimeZone:+ currentTimeZone.getID());
System.out.println(GMT当前偏移量(小时):+ gmtOffset);
//从用户输入获取TS
Timestamp issuedDate =(Timestamp)getACPValue(inputs_,issuedDate);
System.out.println(TS from ACP:+ issuedDate);
//将TS设置为日历
日历issueDate = convertTimestampToJavaCalendar(issuedDate);
//调整GMT(注意偏移否定)
issueDate.add(Calendar.HOUR_OF_DAY,-gmtOffset);
System.out.println(使用GMT和US_EN语言环境从TS转换的日历日期:
+ DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)
.format(issueDate.getTime ()));

代码的输出是:(用户输入5/1/2008 6:12 PM p>

当前用户时间区域:EST

GMT当前偏移量(小时): - 4(通常为-5,除非调整DST)

来自ACP的TS:2008-05-01 14:12:00.0

使用GMT和US_EN从TS转换的日历日期区域设置:5/1/08 6:12 PM(GMT)


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

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:

User enters: 5/1/2008 6:12 PM (EST)
The parameter to the WS needs to be: 5/1/2008 6:12 PM (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):

[May 1, 2008 11:12 PM]

解决方案

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()));

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

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处理日历TimeZones?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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