-2

I have one date format in 2014-12-18T05:39:04.523Z I want to convert it into IST format. I tried for this one.

 SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat targetFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
    Date date;
    try {
         //2014-12-04T12:27:15
        date = originalFormat.parse("2014-12-18T05:39:04.523Z");
        System.out.println("Old Format :   " + originalFormat.format(date));

        System.out.println("New Format :   " + targetFormat.format(date));

    } catch (ParseException ex) {
        System.out.println("Exception *** " +ex);
    }

In output:

Old Format :   2014-12-18T05:39:04
New Format :   Thu Dec 18 05:39:04 IST 2014

Here instead of z, if I put Z, it is showing like this..

New Format :   Thu Dec 18 05:39:04 +0530 2014

But here I want to get 11:09 like this. How can I get the result I want?

Here day month date year all are perfect, but hours minutes seconds are not correct. I have to show 11 hours 09 minutes. Why? Because I am getting this date format one greenhouse Account. This belongs to US, so I want to convert into IST Format.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

1

When you parse the format, try small hh

When convert, try big HH

SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
SimpleDateFormat targetFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");

In Try-Catch

targetFormat.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(targetFormat.format(date));

p/s: Import the TimeZone first...

薛源少
  • 306
  • 1
  • 6
  • 18
0

try small hh in date format

SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
SimpleDateFormat targetFormat = new SimpleDateFormat("E MMM dd hh:mm:ss z yyyy");