Java 如何在Android中设置用于字符串解析的时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22814263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:05:55  来源:igfitidea点击:

How to set the TimeZone for String parsing in Android

javaandroidtimezone

提问by Vlad

I try to parse a String and set a time zone, but I can't produce the desired result.

我尝试解析字符串并设置时区,但无法产生所需的结果。

String dtc = "2014-04-02T07:59:02.111Z";
SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = null;
try {
   date = readDate.parse(dtc);
   Log.d("myLog", "date "+date);
} catch (ParseException e) {
   Log.d("myLog", "dateExcep " + e);
}

SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm"); 
writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
String dateString = writeDate.format(date);

At the output of the variable "dateString" still gives the time 07:59:02 , and I want to make it +4 hours in advance that is 11:59:02

在变量“dateString”的输出中仍然给出时间 07:59:02 ,我想提前 4 小时,即 11:59:02

采纳答案by Meno Hochschild

You need to instruct the read-formatter to interprete the input as UTC (GMT - remember that Z stands for UTC in ISO-8601-format):

您需要指示读取格式化程序将输入解释为 UTC(GMT - 请记住 Z 代表 ISO-8601 格式的 UTC):

String dtc = "2014-04-02T07:59:02.111Z";
SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
readDate.setTimeZone(TimeZone.getTimeZone("GMT")); // missing line
Date date = readDate.parse(dtc);
SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm");
writeDate.setTimeZone(TimeZone.getTimeZone("GMT+04:00"));
String s = writeDate.format(date);

Then you will get:

然后你会得到:

02.04.2014, 11.59

02.04.2014, 11.59

回答by Basil Bourque

Joda-Time

乔达时间

Doing date-time work much easier and simpler with the Joda-Timelibrary than with the notoriously troublesome bundled java.util.Date & .Calendar classes.

与臭名昭著的捆绑 java.util.Date 和 .Calendar 类相比,使用Joda-Time库更容易和更简单地完成日期时间工作。

Time Zone

时区

Using proper time zone names, rather than a specific offset, is generally a wiser approach.

使用正确的时区名称而不是特定的偏移量通常是一种更明智的方法。

Example Code

示例代码

Here is some example code using Joda-Time 2.3.

这是一些使用 Joda-Time 2.3 的示例代码。

Notice:

注意:

  • Joda-Time has built-in parsers for strings in ISO 8601format. No need to instantiate parsing or formatting objects.
  • Joda-Time is both parsing the UTC string andadjusting it to another time zone all in one call to the DateTimeconstructor.
  • When converting to a UTC-based dateTime in the last line, we still have the same moment in the timeline of the Universe (same count of milliseconds since Unix epoch).
  • Joda-Time 内置了ISO 8601格式的字符串解析器。无需实例化解析或格式化对象。
  • Joda-Time 正在解析 UTC 字符串并将其调整到另一个时区,所有这些都在对DateTime构造函数的一次调用中。
  • 当在最后一行转换为基于 UTC 的 dateTime 时,我们在宇宙的时间线中仍然具有相同的时刻(自Unix epoch以来的毫秒数相同)。

Source…

来源…

String input = "2014-04-02T07:59:02.111Z";
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Dubai" );
DateTime dateTimeDubai = new DateTime( input, timeZone ); (a) Parse, (b) Adjust time zone.
DateTime dateTimeUtc = dateTimeDubai.withZone( DateTimeZone.UTC );

Dump to console…

转储到控制台...

System.out.println( "input: " + input );
System.out.println( "dateTimeDubai: " + dateTimeDubai );
System.out.println( "dateTimeUtc: " + dateTimeUtc );

When run…

运行时…

input: 2014-04-02T07:59:02.111Z
dateTimeDubai: 2014-04-02T11:59:02.111+04:00
dateTimeUtc: 2014-04-02T07:59:02.111Z

回答by shabnam singh

you can take whatever pattern you want like i have date+ time zone. so you can set according to you. here is my code.

你可以采用任何你想要的模式,就像我有日期+时区一样。所以你可以根据你的设置。这是我的代码。

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

here i have set the date format.

在这里我设置了日期格式。

String currentDate = dateFormat.format(currentLocalTime);
 String localTime = date.format(currentLocalTime);// to get the time zone i have used this.
  String newtime=currentDate+localTime;// and then i have apeend this now print log you will get the desire result.