Reputation: 31
I am using new date in java jsp page as:
<jsp:useBean id="today" class="java.util.Date" scope="page"></jsp:useBean>
And using this today for retrieving the executions that performed on 'this date'.
I am confused here,is this new date will take date of app server or client (system time in india) date.
As app server is in USA (GMT)
and client is in India (IST)
,So due to this difference we are facing problems like executed values not showing on UI due to time difference.
So how to over come this problem, please share your ideas
Upvotes: 0
Views: 160
Reputation: 339053
The correct Answer by Anoop LL should be accepted. JSP pages are indeed compiled on the server-side, and their Java code is executed on the server-side. I'll add a bit more info.
Date
is UTCThe java.util.Date
is in UTC. Its toString
method confusingly applies the JVM’s current default time zone while generating the string. This creates the illusion of a time zone in the Date
.
You should avoid the troublesome old class, java.util.Date
, now legacy, supplanted by the java.time classes.
To capture the current moment in UTC, use the Instant
class.
Instant instant = Instant.now();
To view this moment through the lens of some region’s wall-clock time, apply a time zone, a ZoneId
, to get a ZonedDateTime
.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or GMT
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
As a shortcut, go directly to ZonedDateTime
by calling its now
method and passing the desired time zone.
ZonedDateTime zdt = ZonedDateTime.now( z );
Never rely on the JVM’s current default time zone. It may change at any time with any call from any code in any thread of any app within the JVM. Instead, specify your desired/expected time zone explicitly by passing any option time zone argument.
You can further adjust into other time zones.
ZonedDateDate zdtLosAngeles = instant.atZone( ZoneId.of( "America/Los_Angeles" ) );
ZonedDateDate zdtParis = instant.atZone( ZoneId.of( "Europe/Paris" ) );
ZonedDateDate zdtKolkata = instant.atZone( ZoneId.of( "Asia/Kolkata" ) );
ZonedDateDate zdtAuckland = instant.atZone( ZoneId.of( "Pacific/Auckland" ) );
All four of the ZonedDateTime
objects in the code above represent the very same moment, the very same point on the timeline. They each carry a different wall-clock time, as if four people on a world-wide conference call each simultaneously looked up at a clock on their wall.
When in doubt, work in UTC. Most of your business logic, data storage, and data exchange should be in UTC. Stop thinking about your own local time zone. Think of UTC as the “One True Time”. All other zoned times are mere variation on that UTC theme.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 0
Reputation: 2276
java.util.Date does not contain information about time zone. Look how you display Date value in the page.
Upvotes: 1
Reputation: 1575
Definitely it will take server time as the JSP is rendered in Server. If you want to use client Date then you can use java script which runs on client side
Upvotes: 3