Java datetime format conversion

date here is my problem:

String datetime = "2012-03-24 23:20:51";


I know this string is in UTC timezone. I need to convert this string to "yyy-mm-dd'T'HH: mm: ssZ" format.

I use the following code for this:

    SimpleDateFormat inFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date inDate = inFormatter.parse(datetime);
    SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    String output = outFormatter.format(inDate);


The problem is this code is running on a server with UTC + 1 timezone and the result it gave me is this:

output = "2012-03-24T21:20:51+0000"


It removes 2 hours from the start and puts the UTC timestamp (0000).

Could you help me solve this? Thank.


source to share

1 answer

If the output format is UTC + 1, you should use it in the outformatter instead of UTC.



Also, if you don't want +0000 at the end, remove the Z

SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");




All Articles