栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Java处理日历TimeZones?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Java处理日历TimeZones?

我从应用程序中获取的时间戳已调整为用户的TimeZone。因此,如果用户输入的是美国东部标准时间下午6:12,我将获得格林尼治标准时间2:12。我需要的是一种撤消转换的方法,这样用户输入的时间就是我发送到WebServer请求的时间。这是我的完成方式:

// Get TimeZone of userTimeZone currentTimeZone = sc_.getTimeZone();Calendar currentDt = new GregorianCalendar(currentTimeZone, EN_US_LOCALE);// Get the Offset from GMT taking DST into accountint 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 hoursgmtOffset = 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 InputTimestamp issuedDate = (Timestamp) getACPValue(inputs_, "issuedDate");System.out.println("TS from ACP: " + issuedDate);// Set TS into CalendarCalendar 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 PM(美国东部标准时间)

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号