如何在java中将UTC日期格式字符串转换为日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18721046/
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
How to convert a UTC date format String to Date format in java?
提问by sTg
I have a requirement where the date is in UTC format like: Thu Jan 1 19:30:00 UTC+0530 1970. I want to convert in to normal date format dd-MM-yyyy HH:mm:ss.Below is the code that i tried.
我要求日期采用 UTC 格式,例如:Thu Jan 1 19:30:00 UTC+0530 1970。我想转换成正常的日期格式dd-MM-yyyy HH:mm:ss 。下面是我试过的代码。
DateFormat formatter = new SimpleDateFormat("E,MMM dd,yyyy h:mmaa");
String today = formatter.format("Thu Jan 1 19:30:00 UTC+0530 1970");
SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date d = f.parse(masterDetailsJsonObject.get("cols1").toString());
But it throws an exception saying unparseable date. Please guide. Thanks in advance.
但它抛出一个异常,说无法解析日期。请指导。提前致谢。
回答by Justin Vincent
You can try this
你可以试试这个
java.util.Date dt = new java.util.Date("Thu Jan 1 19:30:00 UTC+0530 1970");
String newDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(dt);
System.out.println(""+newDateFormat);
回答by Basil Bourque
Joda-Time
乔达时间
This kind of work is much easier with the third-party open-source date-time library, Joda-Time.
使用第三方开源日期时间库Joda-Time,这种工作要容易得多。
Note that unlike a java.util.Date, a Joda-Time DateTime knows its own time zone.
请注意,与 java.util.Date 不同,Joda-Time DateTime 知道自己的时区。
Here is some example code using Joda-Time 2.3.
这是一些使用 Joda-Time 2.3 的示例代码。
String input = "Thu Jan 1 19:30:00 UTC+0530 1970";
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEE MMM dd HH:mm:ss 'UTC'Z yyyy" );
// Adding "withOffsetParsed()" means "set new DateTime's time zone offset to match input string".
DateTime dateTime = formatter.withOffsetParsed().parseDateTime( input );
// Convert to UTC/GMT (no time zone offset).
DateTime dateTimeUtc = dateTime.toDateTime( DateTimeZone.UTC );
// Convert to India time zone. That is +05:30 (notice half-hour difference).
DateTime dateTimeIndia = dateTimeUtc.toDateTime( DateTimeZone.forID( "Asia/Kolkata" ) );
Dump to console…
转储到控制台...
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeIndia: " + dateTimeIndia );
When run…
运行时…
dateTime: 1970-01-01T19:30:00.000+05:30
dateTimeUtc: 1970-01-01T14:00:00.000Z
dateTimeIndia: 1970-01-01T19:30:00.000+05:30
Back To Date
回到日期
If you need a java.util.Date for other purposes, convert your DateTime.
如果您需要 java.util.Date 用于其他目的,请转换您的 DateTime。
java.util.Date date = dateTime.toDate();
Formatted String
格式化字符串
To represent your DateTime as a new String in a certain format, search StackOverflowfor "joda format". You'll find many questions and answers.
要将您的 DateTime 表示为特定格式的新字符串,请在StackOverflow中搜索“joda 格式”。你会发现很多问题和答案。
Joda-Time offers many features for generating strings, including default formatters for ISO 8601formats (seen above), Locale-sensitive formats that automatically change order of elements and even translate words to various languages, formats sensed from the user's computer's settings. If none of those meet your peculiar needs, you may define your own formats with the help of Joda-Time.
Joda-Time 提供了许多用于生成字符串的功能,包括ISO 8601格式的默认格式化程序(如上所示)、自动更改元素顺序甚至将单词翻译成各种语言的区域设置敏感格式、从用户计算机设置中感知的格式。如果这些都不能满足您的特殊需求,您可以在 Joda-Time 的帮助下定义自己的格式。
回答by wangwei
Locale.setDefault(Locale.US);
SimpleDateFormat sourceDateFormat = new SimpleDateFormat("E MMM d HH:mm:ss 'UTC'Z yyyy");
Date sourceDate = sourceDateFormat.parse("Thu Jan 1 19:30:00 UTC+0530 1970");
System.out.println(sourceDate);
SimpleDateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String targetString = targetFormat.format(sourceDate);
System.out.println(targetString);
Use "E MMM d HH:mm:ss 'UTC'Z yyyy
" as the source format. I don't like Java's Date API, especially for the TimeZone
case. Joda-Timeseems good.
使用“ E MMM d HH:mm:ss 'UTC'Z yyyy
”作为源格式。我不喜欢 Java 的 Date API,尤其是这种TimeZone
情况。Joda-Time似乎不错。