首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android :将实际日期和时间转换为millis和其他时区

Android :将实际日期和时间转换为millis和其他时区
EN

Stack Overflow用户
提问于 2012-05-03 22:54:10
回答 3查看 716关注 0票数 0

我必须将实际的日期和时间转换为millis和其他时区GMT+3 (我的时区是GMT-2)。我使用这段代码,但它返回给我时间,但进入我的timezone....why?

代码语言:javascript
复制
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
cal.get(Calendar.DAY_OF_MONTH);
cal.get(Calendar.MONTH);
cal.get(Calendar.YEAR);
cal.get(Calendar.HOUR_OF_DAY);
cal.get(Calendar.MINUTE);
cal.get(Calendar.SECOND);
cal.get(Calendar.MILLISECOND);
long timez = cal.getTime().getTime();
EN

回答 3

Stack Overflow用户

发布于 2012-05-03 22:59:17

您需要使用SimpleDateFormat。日历始终使用计算机上默认配置的时区。下面是关于如何使用SimpleDateFormat实现此功能的example

票数 0
EN

Stack Overflow用户

发布于 2012-05-03 23:03:04

代码语言:javascript
复制
Date date = new Date();
  DateFormat firstFormat = new SimpleDateFormat();
  DateFormat secondFormat = new SimpleDateFormat();
  TimeZone firstTime = TimeZone.getTimeZone(args[0]);
  TimeZone secondTime = TimeZone.getTimeZone(args[1]);
  firstFormat.setTimeZone(firstTime);
  secondFormat.setTimeZone(secondTime);
  System.out.println("-->"+args[0]+": " + firstFormat.format(date));
  System.out.println("-->"+args[1]+": " + secondFormat.format(date));
  }

其中arg和arg1是两个时区。请参阅此LINK

票数 0
EN

Stack Overflow用户

发布于 2012-05-03 22:59:48

getTime()方法同时返回。它与时区无关。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10433928

复制
相关文章

相似问题

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