Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeZone in Java Calendar

Tags:

java

I want to perform operations using the calendar.getinstance() Method. But I am confused that whether it is timezone independent or timezone dependent.

If it is timezone dependent please suggest me to how to make it timezone independent.

like image 343
RahulRaj Avatar asked Jun 03 '26 12:06

RahulRaj


1 Answers

You have four possiblities. From the JavaDoc:

  • Calender.getInstance() will return a Calender-object using the default time zone and locale.
  • Calender.getInstance(TimeZone zone) will return a Calendar-object using the specified time zone and default locale.
  • Calender.getInstance(Locale aLocale) will return a Calendar-object using the default time zone and specified locale.
  • Calender.getInstance(TimeZone zone, Locale aLocale) will return a Calendar-object using the specified time zone and locale.

And as the following quote from the Calendar JavaDoc states, the Calender-object is always TimeZone dependant. The purpose of the object is the transformation between the milliseconds since January 1, 1970 00:00:00.000 GMT (Gregorian) into YEAR, MONTH, DAY, HOUR and so on. The later information is always TimeZone dependant.

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

like image 175
Christopher Avatar answered Jun 06 '26 01:06

Christopher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!