0

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

matthias
  • 1,938
  • 23
  • 51
  • You do store it as a date or as a timestamp? What is SQL type? – Alex Salauyou Mar 15 '16 at 11:36
  • 3
    What are you *intending* `plusDays(1)` to do? If you just want to add 24 hours, you can do that without getting Joda Time involved at all. If you really want to add one day (which will usually be 24 hours, but won't be if there's a time zone transition) then you should be specifying which time zone you care about. – Jon Skeet Mar 15 '16 at 11:40
  • @JonSkeet so you would suggest the use of DateUtils instead of JodaTime? – matthias Mar 15 '16 at 12:03
  • 1
    No, I didn't say that - it's trivial to add 24 hours without any extra libraries. But the first thing you need to do is work out what you're actually trying to achieve; you haven't communicated that yet. – Jon Skeet Mar 15 '16 at 13:03
  • I really just use JodaTime to add/subtract days,weeks to an existing java.util.Date – matthias Mar 15 '16 at 13:23

3 Answers3

2

24 hours != 1 day

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.

Time Zone

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.

java.time supplants Joda-Time

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();

Add 24 hours

Adding 24 hour is simple.

Instant instantPlus24Hours = instant.plus( 24 , ChronoUnit.Hours );

Add 1 day

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 );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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).

Chris311
  • 3,794
  • 9
  • 46
  • 80
-1

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

  1. Is java.sql.Timestamp timezone specific?
Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • well this is exacetly what I am doing. The question is if I will run into timezone issues with this – matthias Mar 15 '16 at 11:57