Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default timezone in java.util.Date

Tags:

If i create a new Date() object. What will be the default timezone it will print.

I have my machine running in GMT. And i am creating a new Date() object. If i print why does it shows Thu Jul 05 08:21:05 PKT 2012. How does it takes the timezone as PKT ?

like image 666
user1182253 Avatar asked Jul 05 '12 03:07

user1182253


People also ask

Is Java Util date UTC?

java. util. Date has no specific time zone, although its value is most commonly thought of in relation to UTC.

What is the default time zone?

Unfortunately, there is no default time, so you have to change your time depending on the time zone used in the place.

Does Java Util date store time?

Conclusion. Class java. util. Date stores a date-time value as milliseconds since the epoch.

Does Java date include TimeZone?

Using Java 7 The Date class (which represents a specific instant in time) doesn't contain any time zone information.


1 Answers

The date itself doesn't have any time zone. Its toString() method uses the current default time zone to return a String representing this date:

Date date = new Date();  System.out.println(TimeZone.getDefault()); System.out.println(date);  TimeZone.setDefault(TimeZone.getTimeZone("UTC"));  System.out.println(TimeZone.getDefault()); System.out.println(date); 

Executing the above code on my machine leads to the following output:

sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]] Fri Jul 06 09:24:45 CEST 2012 sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null] Fri Jul 06 07:24:45 UTC 2012 
like image 100
JB Nizet Avatar answered Feb 14 '23 11:02

JB Nizet