// get time in milliseconds since 1970 Jan 1 long timestamp = System.currentTimeMillis();If you use pure longs, you don’t have to deal with time zones, leap years, months with variable number of days, regional differences, bulky objects, slow code and all the gotchas in the Date and Calendar classes. SQL (Standard Query Language) can’t screw up a long. It surely can a SQLDate. Think hard to keep the bulk of your application dealing with simple longs. Only use Date, Calendar or CalendarFormat when you need to input or output human-readable dates/times.
There are a ton of surprises and bugs awaiting the unwary users of these classes.
The java.util.Date class is crawling with gotchas. It is a disgrace. It is the lemon of Java and deserves a giant string of raspberries. In Java version 1.2, Date has largely been deprecated, and replaced by something even more complicated, Calendar/ GregorianCalendar.
The key to understanding Date is that it is not a date class, but a timestamp class. Inside each date is stored the number of milliseconds since 1970-01-01 UTC (Coordinated Universal Time/Temps Universel Coordonné) . It does not record the time zone or the timestamp in local time. You choose the time zone when you display the date with code like this:
With Date, you can’t have a date without a time. Keep in mind that when you display the same Date with different time zones you will sometimes get 1999-12-14, and sometimes 1999-12-25.
In contrast GregorianCalendar objects do contain a TimeZone reference along with the date and time. Like Dates, they are initialised with the current date and time. Unfortunately, if they are not sure about the user’s time zone, the Sun classes quietly revert to GMT, and in early versions of GregorianCalendar, seem to revert to PST (Pacific Standard Time) if you so much as breathe on them. DateFormats also have TimeZone that is similarly erratic. For code to work, your clients must have configured the TimeZone correctly in the OS (Operating System). You can’t help them configure it.
DateFormat.setTimeZone( TimeZone.getDefault() );To get UTC, without any daylight saving correction, you can use:
DateFormat.setTimeZone( TimeZone.getTimeZone("UTC") );You must configure your TimeZone property in your OS (for example, in the Windows Control Panel) for TimeZone. getDefault to work. The default time zone will be automatically configured in Java once you configure the correct time zone in your OS. However, selecting the right time zone is more than just selecting how many hours off GMT you are. You have to select the correct set of rules for when daylight savings changes. Java does everything internally in GMT and converts for display, so it is not enough just to manually reset your PC (Personal Computer) clock in the spring and fall. The configuring process can fail for exotic parts of the earth because the time zone tables either in the OS or Java are imperfect. Unfortunately, if you actually live in the GMT time zone, Java will think you want GMT standard time without any daylight savings changes. The best solution to this I know so far when your time zone is not properly supported is to roll your own TimeZone object with Alternatively, for applications, you can override the time zone on the java.exe command line with something like
java.exe -Duser.timezone=Europe/London HelloWorldMake sure the -D goes before the classname, or it will be interpreted as an arg to your HelloWorld class. You may have to do this if the JDK does not properly recognise the name of the time zone that your OS calls it. The names for time zones used in Java comes from a list maintained at NIH (Not Invented Here) by Arthur David Olson. Unfortunately, the common names such as EST can mean
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[188.8.131.52]
|Feedback||You are visitor number 60,400.|