java - Storing a "fake" timestamp into a database -


here problem trying solve: read string database a, convert string date object, store date object database b.

ex) database a: read in date string "2015-03-08 02:00:00" database a, convert date object, store database b.

the problem here occurs because 2:00 beginning of dst in u.s. central time, data object converts 2:00 straight 3:00 am, means 3:00 gets stored database b.

is there way correct this? not opposed using joda time if necessary.

i trying focus on above date, 2015-03-08 02:00:00

this code using:

    simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss.s");     sdf.settimezone(timezone.gettimezone("utc"));     string date = "2015-03-08 02:00:00.0";        try      {         d = sdf.parse(date);         sdf.format(d);          //insert database here         // ---         //     }      catch (parseexception e)      {         // todo auto-generated catch block         e.printstacktrace();     } 

you have multiple issues intertwined.

you should not reading strings database date-time values, should reading date-time objects. there many questions on stackoverflow reading/writing date-time values from/to databases, no need repeat here.

if have string, such "2015-03-08 02:00:00", notice lack of indicator of time zone or offset. if want assume string represents time specific central time, must accept fact there no such date-time because daylight saving time (dst) defines 3 am. @ stroke of 2 am, time labeling jumps 2 am. there no point in trying such non-existent date-time.

use proper time zone names

big tip date-time work: avoid thinking time zones "central time" , 3-4 letter codes "cst". these not standardized, nor unique (many duplicates), , further confuse mess daylight saving time. use proper time zone, in pattern of "continent/majorcityorregion".

local date-time

perhaps mean call "local time" date-time not specific 1 time zone. example, "christmas starts @ midnight on december 25th 2015". means different moment in each particular time zone. christmas dawns earlier in paris, montréal, example.

joda-time

let's interpret string localdatetime in joda-time. first, convenience, replace space "t" take advantage of joda-time’s built-in parsers iso 8601 formats.

string input = "2015-03-08 02:00:00"; string inputstandardized = input.replace( " ", "t" );  // convenience, convert input text comply iso 8601 standard’s canonical format. replace space between date & time portions "t". 

next parse standardized string.

localdatetime localdatetime = localdatetime.parse( inputstandardized ); 

dump console.

system.out.println( "inputstandardized: " + inputstandardized ); system.out.println( "localdatetime: " + localdatetime ); 

when run.

inputstandardized: 2015-03-08t02:00:00 localdatetime: 2015-03-08t02:00:00.000 

this local date-time stored in sql database using sql type timestamp without time zone. type means no adjustments utc time zone made in either getting (select) or putting (insert / update) database values. see postgres doc more info on these sql types.

zoned date-time

if meant represent specific moment in specific time zone such america/chicago, when need assign time zone. kind of time-zone-specific values, in database use data type timestamp time zone. type name misleading -- means respect time zone, adjusts incoming data utc. data's original time zone lost.

unfortunately, 1 of few situations joda-time lets down. rather adjustment, joda-time refuses, throwing exception. ☹

see yourself… let's add following code example code above.

datetimezone zone = datetimezone.forid( "america/chicago" ); datetime datetimechicago = localdatetime.todatetime( zone ); // if input lacks offset, joda-time *assigns* value specified time zone. if input has offset, joda-time *adjusts* value specified zone. 

dump console.

system.out.println( "zone: " + zone ); system.out.println( "datetime: " + datetimechicago ); 

when run.

exception in thread "main" org.joda.time.illegalinstantexception: illegal instant due time zone offset transition (daylight savings time 'gap'): 2015-03-08t02:00:00.000 (america/chicago … 

there appears no generalized workaround, hacks. basically, if expect time zone, make adjustment yourself. see discussions this, this, this, , the joda-time faq.

java.time

in java 8 , later, have new built-in date-time framework in java.time package (tutorial). framework inspired joda-time, , has advantages on joda-time. 1 of advantages handling of dst non-existent value problem.

string input = "2015-03-08 02:00:00"; string inputstandardized = input.replace( " ", "t" );    localdatetime localdatetime = localdatetime.parse( inputstandardized ); 

let's adjust local date-time assign specific time zone. java.time framework detects non-existent date-time , automatically slides time-of-day forward respect dst transition.

zoneid zone = zoneid.of( "america/chicago" ); zoneddatetime zdt = zoneddatetime.of( localdatetime, zone ); 

dump console.

system.out.println("inputstandardized: " + inputstandardized ); system.out.println("localdatetime: " + localdatetime ); system.out.println("zone: " + zone ); system.out.println("zdt: " + zdt ); 

when run.

inputstandardized: 2015-03-08t02:00:00 localdatetime: 2015-03-08t02:00 zone: america/chicago zdt: 2015-03-08t03:00-05:00[america/chicago] 

sql

as said above, can search stackoveflow info on getting date-times in , out of databases.

ideally, java.time, directly feed either localdatetime or zoneddatetime jdbc driver. drivers have not yet updated handle java.time types. until driver updated, fall on java.sql.* classes. convenient conversion methods can found on both new , old classes bundled java.

java.sql.timestamp ts = java.sql.timestamp.valueof( localdatetime ); 

…or…

instant instant = zdt.toinstant(); java.sql.timestamp ts = java.sql.timestamp.from( instant ); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -