refactoring - migrate from Joda time library to Java time (Java 8) -
our software architect made decision remove joda time library our project dependencies , use features of java 8 time. doing research of our project , trying find places using joda time lib , determine using (joda localdate
, datetime
, etc.).
not expert of joda time library , not expert of java 8 time, have questions:
in code saw lot of places this:
datetime jodadatetime = new datetime(); dosomething(jodadatetime.todate());
i little bit confused.
(q1) make sense use joda datetime
in particular case? believe, can use classic java.util.date
, not need joda:
java.util.date date = new date(); dosomething(date);
correct?
also, saw piece of code:
org.joda.time.datetime jodadatetime = new datetime(); org.joda.time.datetime jodadatetimeinpast = jodadatetime.minusseconds(60); dosomething(jodadatetimeinpast.todate());
i think, java 8 time api provides great support calculating dates in past, have idea how replace piece of code above java 8:
localdatetime java8datetime = localdatetime.now(); localdatetime java8datetimeinpast = java8datetime.minusseconds(60); date java8date = date.from(java8datetimeinpast.atzone(zoneid.systemdefault()).toinstant()); dosomething(java8date);
(q2) have done in right way? not sure on 100%. found link table: "converting joda-time java.time" there found info, need use java.time.zoneddatetime
or java.time.offsetdatetime
classes, not java.time.localdatetime
, because localdatetime
-
same concept - date , time without time-zone
please confirm refactoring correct (for q1 , q2).
most questions conversion should handled blog on topic.
q1a: there no point in creating joda-time object change java.util.date
. however, method should probbaly take instant
or zoneddatetime
:
instant instant = instant.now(); dosomething(instant); // method changed take instant
note instant
closest match java.util.date
.
q1b: case involves time-zone, should use zoneddatetime
.
zoneddatetime java8datetime = zoneddatetime.now(); // uses zoneid.systemdefault() zoneddatetime java8datetimeinpast = java8datetime.minusseconds(60); dosomething(java8datetimeinpast.toinstant());
again, assumes dosomething
changed take instant
.
the key using java.time
think data type correct each piece of data.
- just date? use
localdate
- just time? use
localtime
- date, time , time-zone? use
zoneddatetime
- just timestamp no other info? use
instant
- a network date-time format offset (not zone)? use
offsetdatetime
.
Comments
Post a Comment