将 Java 字符串解析为 GMT 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13166683/
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
Parsing Java String into GMT Date
提问by c12
I'm trying to parse a String that represents a date using GMT, but it prints out in my timezone on my PC (pacific). When I run the below I get the below output. Any ideas on how to get the parse to parse and return a GMT date? If you look below I'm setting the timezone using format.setTimeZone(TimeZone.getTimeZone("GMT")); but its not producing the desired result.
我正在尝试使用 GMT 解析表示日期的字符串,但它会在我的 PC(太平洋)上的时区中打印出来。当我运行下面的我得到下面的输出。关于如何让解析解析并返回 GMT 日期的任何想法?如果您看下面,我正在使用 format.setTimeZone(TimeZone.getTimeZone("GMT")); 设置时区;但它没有产生预期的结果。
output from below code:
以下代码的输出:
Mon Oct 29 05:57:00 PDT 2012
2012 年 10 月 29 日星期一 05:57:00 PDT
package javaapplication1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class JavaApplication1 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.parse("2012-10-29T12:57:00-0000"));
}
}
回答by Yogendra Singh
You are using format.setTimeZone(TimeZone.getTimeZone("GMT"));
in the formatter, which is being used in formatting the string into date i.e.
您正在format.setTimeZone(TimeZone.getTimeZone("GMT"));
格式化程序中使用,该格式化程序用于将字符串格式化为日期,即
Date date = format.parse("2012-10-29T12:57:00-0000");
is parsed treating 2012-10-29T12:57:00-0000
was a GMT
value, but you are printing date
which uses local timezome in printing hence you are noticing the difference.
被解析处理2012-10-29T12:57:00-0000
是一个GMT
值,但您正在打印date
它在打印中使用本地时区,因此您会注意到差异。
If you want to print the date back in GMT
, please use:
如果您想打印回日期GMT
,请使用:
String formattedDate = format.format(date);
and print the formattedDate
. This will be GMT
.
并打印formattedDate
. 这将是GMT
。
System.out.println(formattedDate);
回答by c12
Complete working example
完整的工作示例
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main
{
public static void main(String[] args)
{
final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss z", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(new Date()));
// ^^^ note printing the results of sdf.format()
// not a raw `Date`
}
}
result : 31-10-2012 08:32:01 UTC
Note what I am actually printing out!
结果:31-10-2012 08:32:01 UTC
注意我实际打印的内容!
回答by koljaTM
System.out.println(format.parse("2012-10-29T12:57:00-0000"));
Parses the date as GMT and returns a Date object. Then it prints it out (using the default toString()-method). This just uses the settings of your computer. so you should use:
将日期解析为 GMT 并返回一个 Date 对象。然后将其打印出来(使用默认的 toString() 方法)。这仅使用您计算机的设置。所以你应该使用:
Date parsedDate=format.parse("2012-10-29T12:57:00-0000");
System.out.println(format.format(parsedDate));
回答by Diego Monteiro
Try this,
试试这个,
public static String converterGMTToLocal(String datahora_gmt)
{
try {
SimpleDateFormat fmt = new SimpleDateFormat("YYYY-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.setTime(fmt.parse(datahora_gmt));
cal.add(Calendar.HOUR, -3);
return fmt.format(cal.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
}
Change the values for your project...make the appropriate changes
更改项目的值...进行适当的更改