0

I have a GMT formatted String value in yyyy-MM-dd HH:mm:ss.SSS zzz

eg: 2013-07-29 06:35:40:622 GMT. I want to get it as a date object and convert it into IST time zone.

I did smthng like this... **

    String GMT = "2013-07-29 06:35:40:622 GMT";  
     DateFormat utcDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
     utcDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    try {
        utcDateFormat.parse(GMT);
    } catch (ParseException e) {
        e.printStackTrace();
    }**

bt gettng error as java.text.ParseException: Unparseable date: "2013-07-29 06:35:40:622 GMT"

Aswathy
  • 45
  • 1
  • 1
  • 4
  • 7
    Start by checking out [`SimpleDateFormat`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) and having a read of the other few hundred posts on the same subject... – MadProgrammer Aug 21 '13 at 06:14
  • 2
    possible duplicate of [Date and time conversion to some other Timezone in java](http://stackoverflow.com/questions/9429357/date-and-time-conversion-to-some-other-timezone-in-java) – rajesh Aug 21 '13 at 06:17
  • Try JodaTime. It is much easier to use. – KKKCoder Aug 21 '13 at 06:19
  • Try this might help you, SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); String dateInMyFormat sdf.format(new Date()); – LMK Aug 21 '13 at 06:19
  • What have you *tried*? – nanofarad Aug 21 '13 at 06:20
  • I think the `zzz` format doesn't recognize the `GMT` text – MadProgrammer Aug 21 '13 at 06:37
  • Got it ..It was my mistake. I gave it as "yyyy-MM-dd HH:mm:ss.SSS zzz" instead of "ss:SSS" Thanks for ur replies :) – Aswathy Aug 21 '13 at 06:40

3 Answers3

8

The main (problems) are that SimpleDateFormat doesn't recognize the text GMT, it's looking for a different format for the time zone.

The other problem is your format doesn't match your text...

Your String is in the form of

2013-07-29 06:35:40:622 GMT

And your format is in the form of

yyyy-MM-dd HH:mm:ss.SSS zzz
                   ^--------This is a naughty character...

The seconds and milliseconds are separated by different characters, this isn't going to help

Now, if we fix the expected format with the actual format, this won't fix the issue with the time zone...but, fourtantly for us, DateFormat provides a lenient method, this allows us to "bend" the format rules a bit...

So, with all that in hand, you can try something like...

String text = "2013-07-29 06:35:40:622 GMT";
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
TimeZone gmt = TimeZone.getTimeZone("GMT");
sdf.setTimeZone(gmt);
sdf.setLenient(false);

try {
    Date date = sdf.parse(text);
    System.out.println(date);

    System.out.println(sdf.format(date));
} catch (Exception e) {
    e.printStackTrace();
}

Which outputs...

Mon Jul 29 16:35:40 EST 2013
2013-07-29 06:35:40:622
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • After changing it to the actual format I got it correctly..but i didnt include dat linient method..nyway i was not aware of this method earlier.. thnks for this information.. – Aswathy Aug 21 '13 at 07:14
  • I'd be curious as to why this answer would deserver a downvote? In what ways does it not answer the question been asked - remember, it was answered 2013, so the never APIs weren't available, or is jus that you don't think the question should have been answered? Shame on me for wanting to help someone – MadProgrammer Aug 08 '18 at 10:37
-1

Try This.

Date d=new Date("2013-07-29 06:35:40:622");
Date newDate=new Date(d.getYear(),d.getMonth(),d.getDate());
Time t=new Time(d.getHours()+5, d.getMinutes()+30, d.getSeconds());
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
-1

Joda Time supports timezone conversion. http://www.joda.org/joda-time/userguide.html#Changing_TimeZone

Net Dawg
  • 518
  • 1
  • 6
  • 10