java - Convert database time to IST datetime -
i have time value stored in database in hh:mm:ss
format (using mysql's time
type). time considered value of ist timezone. server on java code runs follows utc timezone.
how can formatted datetime in yyyy-mm-dd hh:mm:ss
in ist (or in utc millis)? following i've tried till now:
// ... code truncated brevity datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss"); localtime time = resultset.gettime("send_time").tolocaltime(); localdatetime datetime = time.atdate(localdate.now()); system.out.println(datetime.format(formatter));
the above correctly prints datetime on local machine, on ist, i'm concerned how behave on remote server.
your approach fine , should work regardless of computer's time zone since there no time zone information in either localtime
or localdatetime
. 1 possible issue localdate.now()
returns today's date in computer's local time zone, not in ist. may want replace localdate.now(zoneid.of("asia/calcutta"))
.
or commented @olev.v. use new driver facilities derive localtime
directly:
localtime time = resultset.getobject("send_time", localtime.class);
note possible caveats approach:
- if time zone use introduces dst, may end 2 identical times in db different instants - using utc store times more robust
time
in mysql can store values smaller00:00
, larger23:59:59.999999
, in case may experience unexpected behaviours on java side.
Comments
Post a Comment