2

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.

5
  • What language are you in? What have you tried? Commented Sep 10, 2013 at 14:21
  • I want to convert the same in Java. I tried simpledateformat class but i was getting exception unparseable date.. Commented Sep 10, 2013 at 14:23
  • Take a look at docs.oracle.com/javase/7/docs/api/java/text/…. Commented Sep 10, 2013 at 17:47
  • @ Andrew : I have added the code that i have tried from my end as per stackoverflow rules. Commented Sep 11, 2013 at 4:40
  • By the way, there is no such thing as a "normal" date format. Date-time formats vary widely across different cultures and languages. The closest thing to "normal" would be the official standard, ISO 8601 format like this: 2014-01-24T10:30:27+02:00 Commented Jan 25, 2014 at 4:37

3 Answers 3

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me . my input is. Sun Mar 13 19:21:30 UTC 2016 works fine
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.