Reputation: 26548
We have one of our application hosted on a application server installed on machine working in CST timings. Now in the application footer we are showing the Last Login time and date using java.util.date().toString();
Now from reference of this post I can say java.util.Date().toString() is using TimeZone.getDefault() method which, in turn, will default to the time zone of the operating system it is running on (i.e. the host).
So the application Should show the timings according to CST timzone but it is not happening only for this server, it is always showing GMT timzone.
Here is a attached screenshot showing time configuration and the results from ‘time’ command on the serve, as well as the application Admin UI, showing time in GMT which is visible in footer.
So I am looking for the possible causes as why java.util.date.toString()
is showing this unexpected behavior and what is the solution for that?
Upvotes: 0
Views: 1334
Reputation: 44071
As you have already recognized, the behaviour of java.util.Date.toString()
is based on TimeZone.getDefault().
- Use the user.timezone property value as the default time zone ID if it's available.
- Detect the platform time zone ID. The source of the platform time zone and ID mapping may vary with implementation.
- Use GMT as the last resort if the given or detected time zone ID is unknown.
The JVM might not be able to interprete the timezone of the underlying operating system, for example if the OS uses Windows timezone names which cannot be resolved to identifiers common in iana-tzdb.
Solution:
I suggest you to start the JVM with following system property:
-Duser.timezone=America/Chicago
Upvotes: 2
Reputation: 3895
A java.util.Date
object represents a timestamp. A timestamp is a specific moment in time and has no concept of a timezone. When displaying a java.util.Date
you need to format the time with respect to a given timezone.
The usual way to do this is using java.text.DateFormat
:
Date d = new Date()
DateFormat dateForamt = DateFormat.getDateTimeInstance()
// The default timezone is the timezone that is default for the user, but it
// can be changed like so:
dateFormat.setTimeZone(someTimezone)
System.out.println(dateFormat.format(d))
Upvotes: 1
Reputation: 922
Better use Joda-Time, a date-time object (DateTime) truly does know its assigned time zone. That means an offset from UTC and the rules and history of that time zone’s Daylight Saving Time (DST) and other such anomalies.
The standard date and time classes prior to Java SE 8 are poor. By tackling this problem head-on, Joda-Time became the de facto standard date and time library for Java prior to Java SE 8.
DateTimeZone dateTimeZone = DateTimeZone.forID("America/Chicago");
DateTime dateTime = DateTime.now(dateTimeZone);
System.out.println("Current time is: " + dateTime);
References: Joda Time: http://googleweblight.com/?lite_url=http://www.joda.org/joda-time/&ei=fiQH2-Y-&lc=en-IN&s=1&m=717&host=www.google.co.in&ts=1456818933&sig=ALL1Aj74vOgr-H7yxBKCAWq5KKQ28MTvvw
Time zones: http://joda-time.sourceforge.net/timezones.html
Upvotes: 1