Algeroth Algeroth - 5 months ago 25
Java Question

SimpleDateFormat with TimeZone

I'm trying to format date from java.util.Date. I need this format: '2016-06-10T13:38:13.687+02:00'. How correctly convert this from standard Date format 'May 04 09:51:52 CDT 2009' ?

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z", Locale.getDefault());
sdf.format(new Date());


This code unfortunately return value without '+02:00'.

Answer

Just turn your z to upperCase

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.getDefault());
    sdf.format(new Date());

Result: 2016-06-10T13:53:22 +0200