Search code examples
javatimetimezonejodatimetimestamp-with-timezone

After DateTimeZone.convertLocalToUTC timezone still shows local


I use DateTimeZone.convertLocalToUTC to convert local time to UTC. The time is changed correctly, but after conversion, the timezone info still says the Original local timezone. Please refer below sample code

Date gmttime = new Date(tz.convertLocalToUTC(System.currentTimeMillis(),false));
System.out.println(gmttime.toString());

Output : Wed Oct 16 12:58:19 IST 2013

Please note the bold value, I expected it to be UTC . Please let me know if I am missing something.


Solution

  • #Date.toString() will print the date in the local timezone.

    Use SimpleDateFormat to print the Date formatted for a specific TimeZone:

    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("E MMM  dd HH:mm:ss:SS z");
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println(format.format(date));
    }