How to convert a UTC date format String to Date format in java?

做~自己de王妃 提交于 2019-12-09 19:29:09

问题


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.

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.


回答1:


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);



回答2:


Joda-Time

This kind of work is much easier with the third-party open-source date-time library, Joda-Time.

Note that unlike a java.util.Date, a Joda-Time DateTime knows its own time zone.

Here is some example code using 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 date = dateTime.toDate();

Formatted String

To represent your DateTime as a new String in a certain format, search StackOverflow for "joda format". You'll find many questions and answers.

Joda-Time offers many features for generating strings, including default formatters for ISO 8601 formats (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.




回答3:


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-Time seems good.



来源:https://stackoverflow.com/questions/18721046/how-to-convert-a-utc-date-format-string-to-date-format-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!