java - Date time and simple date time format results are different -


i have following code fragment:

        datetime datetime = new datetime().withzone(datetimezone.forid("america/chicago"))                 .withyear(2016)                 .withmonthofyear(8)                 .withdayofmonth(25)                 .withhourofday(12)                 .withminuteofhour(37);          system.out.println("datetime: ");         system.out.println(datetime.todate().gettime());          string str = "2016-8-25 12:37 cst";         simpledateformat sdf = new simpledateformat("yyyy-m-dd hh:mm z");          system.out.println("sdf: ");         try {             system.out.println(sdf.parse(str).gettime()+"");         } catch (parseexception e) {             // todo auto-generated catch block             e.printstacktrace();         } 

according understanding, both represent same date (and hence epoch should same well). result is:

datetime:  1472146669119 sdf:  1451198220000 

if change timezone cdt, get:

datetime:  1472146668746 sdf:  1451194620000 

so hope kind soul enlighten me (to poor soul).

thanks

update:

i using following modified code:

        datetime datetime = new datetime(2016, 8, 25, 12, 37, datetimezone.forid("america/chicago"));          system.out.println("datetime: ");         system.out.println(datetime.todate().gettime());          string str = "2016-8-25 12:37 cdt";         simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm z");          system.out.println("sdf: ");         try {             system.out.println(sdf.parse(str).gettime()+"");         } catch (parseexception e) {             // todo auto-generated catch block             e.printstacktrace();         } 

now getting:

datetime:  1472146620000 sdf:  1472103420000 

in second bit of code posted, date/time:

new datetime(2016, 8, 25, 12, 37, datetimezone.forid("america/chicago")); //                         ^ - midday 

...is 37 minutes after midday on 25 august 2016.

this date:

string str = "2016-8-25 12:37 cdt"; 

...is 37 minutes after midnight on 25 august 2016.

for dates match expect first 1 should be:

new datetime(2016, 8, 25, 0, 37, datetimezone.forid("america/chicago")); //                        ^ - midnight 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -