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

23,157

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

Solution 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.

Share:
23,157
sTg
Author by

sTg

~~I believe in learning and you learn every second in your life~~

Updated on July 09, 2022

Comments

  • sTg
    sTg almost 2 years

    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.

  • luizfelipetx
    luizfelipetx about 8 years
    This works for me . my input is. Sun Mar 13 19:21:30 UTC 2016 works fine