java - UTC Timestamp + Joda Time -


i trying utc timestamp in simple java program using joda:

public timestamp getcurrentutc(localdatetime date, datetimezone srctz, datetimezone dsttz, locale l) {     datetime srcdatetime = date.todatetime(srctz);     datetime dstdatetime = srcdatetime.todatetime(dsttz);      system.out.println("utc time:" + dstdatetime.getmillis());           system.out.println("utc time:" + new timestamp(dstdatetime.getmillis()));      return new timestamp(dstdatetime.getmillis()); } 

the output of program follows:

utc time:1378265162047 utc time:2013-09-03 23:26:02.047 

the millisecond value correct utc time (i.e. confirmed gmt-4 timezone) second value est timezone.

what need utc value unchanged java.sql.timestamp (ie tz independent), database write. possible?

edit 1

datetime srcdatetime = date.todatetime(srctz);  datetime dstdatetime = srcdatetime.todatetime(dsttz);  system.out.println("utc time:" + dstdatetime.getmillis()); 

i know srcdatetime local date (gmt-4), , dstdatetime utc (gmt-0). output values of dates follows:

source date:2013-09-04t09:10:43.683-04:00  destination date: 2013-09-04t13:10:43.683z 

i tried combinations try utc value of dstdatetime java.sql.timestamp:

system.out.println("utc time:" + dstdatetime.getmillis());  system.out.println("utc time:" + new timestamp(srcdatetime.todatetime(datetimezone.utc).getmillis()));  system.out.println("utc time:" + new timestamp(dstdatetime.todatetime(datetimezone.utc).getmillis())); 

the print output testing:

utc time:1378298760226 - correct utc  utc time:2013-09-04 08:46:00.226 - incorrect local date time instead of expected utc  utc time:2013-09-04 08:46:00.226 - incorrect local date time instead of expected utc 

the first print line correct utc timestamp. need same value type java.sql.timestamp. tried returned local date time of machine.

edit 2

i tried following:

system.out.println("utc timestamp:" + date.todatetime(srctz).getmillis()); system.out.println("utc timestamp:" + new timestamp(date.todatetime(srctz).getmillis())); 

the output follows:

utc time:1378342856315 - correct utc time utc timestap:2013-09-04 21:00:56.315 - local time other expected utc time 

whenever try convert timestamp, loose valid utc value after.

in terms of method's parameters:

srctz = datetimezone.fortimezone(timezone.gettimezone("america/montreal") dsttz = datetimezone.fortimezone(timezone.gettimezone("etc/utc")) local l = new locale("en", "ca") 

any appreciated.

nick.

edit 3

hello matt,

thank response. getting same results you. did not know thing printing etc.. more specifically:

system.out.println("utc timestamp:" + srcdatetime.todatetime(dsttz).getmillis()); system.out.println("utc timestamp:" + srcdatetime.todatetime(dsttz)); system.out.println("utc timestamp:" + new timestamp(srcdatetime.todatetime(dsttz).getmillis())); 

yields output:

utc timestamp:1378389098468 - correct utc timestap (thu, 05 sep 2013 13:51:38 gmt) utc timestamp:2013-09-05t13:51:38.468z - correct utc time utc timestamp:2013-09-05 09:51:38.468 - local time printed, utc expected 

the problem brought attention when realized db storing local time instead of utc:

+---------------------+ | effectivedate       | +---------------------+ | 2013-09-05 09:34:11 | +---------------------+ 

the mysql timezone set '-00:00'

mysql> select current_timestamp; +---------------------+ | current_timestamp   | +---------------------+ | 2013-09-05 13:48:09 | +---------------------+ 

debugging application using eclipse debugger realized local date time (2013-09-05 09:51:38.468) being passed db (can't post images, not enough points...). datatype straight timestamp, no string manipulation. maybe eclipse debugger using string.println() function well, not sure..

i appreciate debugging our application. did not want take time (no pun intended) , effort...

kind regards,

nick.

i hope saves 3 days of bullshit. set default timezone somewhere logical in code. makes things more portable having set env variable etc.. can adding following somewhere logical code, constructor etc..:

datetimezone.setdefault(datetimezone.utc);

you can print utc, concatenate utc whatever...


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -