user2182258
user2182258

Reputation: 1

why local timeZone String is getting appended in the UTC date?

I want to get a Date Object in UTC Time Zone so i am converting it first to a String which is returing me correct UTC Date String but when i am again parsing it to Date object then Local Time Zone String(ie. IST) is getting appended in that Date instead of UTC.

        Date date = new Date();
        DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        String estTime = timeFormat.format(date);
        date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(estTime);

Upvotes: 0

Views: 148

Answers (1)

Jonathan Drapeau
Jonathan Drapeau

Reputation: 2610

A Date doesn't have a timezone, it represents a moment in time. It holds the time in milliseconds.

You can't have a Date with a format, it doesn't work that way. If you need to show the Date in a GUI, console or anywhere else, that's when you use a SimpleDateFormat like you did to change it to a String in the format you want.

Upvotes: 2

Related Questions