java - Using two LocalDateTime instances to calculate a duration -
i trying calculate duration between 2 instances of localdatetime
. special thing here each instance of localdatetime
anywhere in world:
localdatetime start
nevada , localdatetime end
tokyo. each "time" associated localdatetime
is, enough, local location.
so if said...
localdatetime start = localdatetime.parse("2015-07-14t10:00:00");
, said start
represented chicago, mean 10:00 in chicago time. say...
localdatetime end = localdatetime.parse("2015-07-14t03:00:00");
, end
represents moscow, 3:00am in moscow time.
can create robust enough solution allow start
, end
represent any cities in world , still correctly calculate duration between two?
"localdatetime" not mean particular locality
i think misunderstand purpose of localdatetime
. "local" means any locality, not specific locality. in "christmas starts @ midnight on december 25, 2015" mean any locality’s midnight. christmas starts in paris several hours earlier montréal, example.
if know date-time meant represent date-time in nevada, use joda-time datetime
assigned proper time zone name of america/boise
. in new java.time package (tutorial) built java 8 , later, use zoneddatetime
object assigned time zone.
similarly, if know date-time local tokyo, not use localdatetime
. use joda-time datetime
assigned time zone of asia/tokyo
.
elapsed
elapsed time between pair of localdatetime
instances makes no sense. example, times may 14:00 , 18:00 on same date, not mean 4 hours difference. if meant 14:00 in paris , 18:00 in chicago, several hours difference, not two.
i not discussing calculating elapsed time has been handled many many times on stackoverflow. i'm trying clarify concepts here. can move on existing questions & answers calculating elapsed time.
databases store utc
generally in sql databases should using data type timestamp time zone
(a.k.a. timestampz
z
"zulu"). misnomer means "with respect time zone". incoming data offset utc or other time zone information adjusted utc. data's offset or time zone not preserved.
the sql data type timestamp without time zone
(a.k.a. timestamp
) means same localdatetime
in java: no time zone @ all. not tied timeline. offset or time zone information input data ignored, no adjustment made.
postgres doc may explain.
stick utc
when retrieving such value database, utc value may adjusted particular time zone admin tool (such pgadmin in postgres) or database driver or app.
in app best keep date-time values in utc as possible. of storage, business logic, , data exchange in utc. adjust particular time zone when expected user.
to convert localdatetime time zone
if have localdatetime
object, , want assign time zone, here example code. adjust same moment seen in montréal , in utc. first example shown in joda-time, in java.time.
joda-time
example in joda-time 2.8.
localdatetime ldt = new localdatetime( "2015-07-14t10:00:00" ); // in particular. datetimezone zonechicago = datetimezone.forid( "america/chicago" ); datetime datetimechicago = ldt.todatetime( zonechicago ); datetime datetimemontreal = datetimechicago.withzone( datetimezone.forid( "america/montreal" ) ); datetime datetimeutc = datetimechicago.withzone( datetimezone.utc );
dump console.
system.out.println( "localdatetime (nowhere): " + ldt ); system.out.println( "chicago: " + datetimechicago ); system.out.println( "montréal: " + datetimemontreal ); system.out.println( "utc: " + datetimeutc);
when run.
localdatetime (nowhere): 2015-07-14t10:00:00.000 chicago: 2015-07-14t10:00:00.000-05:00 montréal: 2015-07-14t11:00:00.000-04:00 utc: 2015-07-14t15:00:00.000z
java.time
example in java.time of java 8 update 51.
localdatetime ldt = localdatetime.parse( "2015-07-14t10:00:00" ); // in particular. zoneid zonechicago = zoneid.of( "america/chicago" ); zoneddatetime zdtchicago = zoneddatetime.of( ldt, zonechicago ); zoneddatetime zdtmontreal = zdtchicago.withzonesameinstant( zoneid.of( "america/montreal" ) ); zoneddatetime zdtutc = zdtchicago.withzonesameinstant( zoneoffset.utc ); // zoneoffset subclass of zoneid.
dump console.
system.out.println( "localdatetime (nowhere): " + ldt ); system.out.println( "chicago: " + zdtchicago ); system.out.println( "montréal: " + zdtmontreal ); system.out.println( "utc: " + zdtutc);
when run.
localdatetime (nowhere): 2015-07-14t10:00 chicago: 2015-07-14t10:00-05:00[america/chicago] montréal: 2015-07-14t11:00-04:00[america/montreal] utc: 2015-07-14t15:00z
Comments
Post a Comment