Reputation: 2062
from the UI (jsf) I get a java.util.Date
Object (lets call ita
). As far as I know this one does not have any timezone information.
Afterwards, I use code like this to do some business logic:
Date bar = new DateTime(a).plusDays(1).toDate()
And finally I store this bar
into my database
I wonder if I will get some timezone issues with this? Because JodaTime uses timezone information from the server?!
For the database, I use
@Temporal(TemporalType.*)
and on my Oracle DB datatype DATE
.
Background: I use JodaTime for adding and subtracting days/hours/weeks because if the nice API
Upvotes: 0
Views: 182
Reputation: 338730
You ignored the comments by Jon Skeet. You must become conciously aware of the fact that one day does not mean 24 hours. Because of anomalies such as Daylight Saving Time (DST), a day can run longer or shorter than 24 hours.
You need to be clear about which period your situation requires, 24 hours or one day.
For adding 24 hours, time zone is irrelevant.
Time zone is crucial in determining dates and days. For any given moment, the date varies around the world. For example a new day dawns earlier in Paris than in Montréal.
Furthermore, time zone defines rules for handling anomalies such as Daylight Time Saving (DST).
When omitted, your JVM’s current default time zone is applied. This is risky as that default can change at any moment during runtime by any code in any thread of any app in that JVM calling TimeZone.setDefault
.
I suggest you always specify a time zone by passing the optional argument rather than rely implicitly on the JVM’s current default. Ditto for Locale
.
The makers of Joda-Time went on to define JSR 310, now implemented as the java.time framework built into Java 8 and later. While Joda-Time is still actively supported, the makers have asked us to move to java.time as soon as is convenient.
Convert your java.util.Date
to an Instant
, a moment on the timeline in UTC.
Instant instant = myJavaUtilDate.toInstant();
Adding 24 hour is simple.
Instant instantPlus24Hours = instant.plus( 24 , ChronoUnit.Hours );
Adding a day requires the context of a particular time zone. An Instant
is always in UTC by definition. So we need to apply a time zone to generate a ZonedDateTime
.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
ZonedDateTime zdtDayLater = zdt.plusDays( 1 );
Upvotes: 2
Reputation: 29150
If you want to use the Joda-Time library, because of the various shortcomings of the Date class. You can do it
Date dt = new Date();
DateTime dateOriginal = new DateTime(dt);
DateTime datePlusOne = dateOriginal.plusDays(1);
Update: For timezone issue, you can to it
LocalDateTime.from(dt.toInstant().atZone(ZoneId.of("UTC"))).plusDays(1);
For timezone related issue, you can check
Upvotes: -1
Reputation: 3992
Your date will be stored in your local time zone, but there is no information on the db about the timezone (assuming you don't handle it manually).
Upvotes: 0