标签云

微信群

扫码加入我们

WeChat QR Code


And what are the semantics at the db side? If it's instant and not date, you're fried anyway.

2018年10月23日42分42秒

MarkoTopolnik As I'm getting from the table it's just date withouth time (as it takes 4 bytes, not 8 bytes as DateTime types).

2018年10月23日42分42秒

It should be safe to use your technique because all the timezone issues should be taken into account by LocalDate#toDate.

2018年10月23日42分42秒

MarkoTopolnik But what if we create an instance of LocalDate, then we call toDate() on it, then clock is rewinded one hour back and the day becomes the previous one, then getTime() is called (it's still a previous day), and then java.sql.Date is constructed (it's still a previous day). So we end up with the previous day. I understand that I can use UTC with no daylight saving times. But still, I see no purpose in using any time-related values, when I don't need time at all (but just date).

2018年10月23日42分42秒

That is not what can happen. The millisecond instant you have is context-independent: it uniquely relates to a timezone valid at that point in time. The only real trouble you can get is when two instants map to the same time label (events when the clocks are turned backwards so the same time labels repeat). There have actually been some clarifications as to the exact semantics in those cases, but you shouldn't be affected by them, anyhow.

2018年10月23日42分42秒

Hopefully JDBC will also take advantage of this and start accepting LocatDate object as parameter to setDate!

2018年10月23日42分42秒

ovgolovin Indeed, you can exchange java.time objects directly with JDBC 4.2 and later. Use PreparedStatement::setObject and ResultSet::getObject rather than setDate/getDate. See my Answer on this same Question for details. myPreparedStatement.setObject( … , localDate ) and myResultSet.getObject( … , LocalDate.class ).

2018年10月23日42分42秒

I don't believe this will work for Joda LocalTime

2018年10月23日42分42秒

You're confusing Java 8 LocalDate with Joda LocalDate. The question is about Joda LocalDate.

2018年10月23日42分42秒

Is toDateTimeAtStartOfDay a typo? Did you mean the withTimeAtStartOfDay method on the Joda-Time DateTime class?

2018年10月23日42分42秒

No, it is not a typo. Before using it I set startDate to, let's say, "2015-01-01' and in it was persisted to database as "2014-12-31". This now works because the conversion to LocalDate is done as it should.

2018年10月23日42分42秒

So can you link to the doc? I can't find that method.

2018年10月23日42分42秒

What does using toDateMidnight solve?

2018年10月23日42分42秒

JodaTime-DateMidnight

2018年10月23日42分42秒

This still casts date to a specific instance of the moment. And it may happen that after this conversion the current moment will pass the end of DST period and the clock will be rewound one hour back. After that the instance of the moment we saved will represent 23:00 of the previous day. That is the problem. And I expected there should be a way to work with dates without resorting to time instances.

2018年10月23日42分42秒

Also, what additionally it offers to what toDate method does?

2018年10月23日42分42秒

The midnight-related classes and methods have been deprecated in latest versions of Joda-Time. Instead call method withTimeAtStartOfDay. The first moment of the day is not always 00:00:00.0.

2018年10月23日42分42秒