Java: date / time from database to milliseconds, hour disappears

I have a problem with dates and time zones.

I have a MySQL InnoDB database that contains two fields DATE(yyyy-MM-dd)and TIME(HH:mm:ss). They are known as UTC (0 GMT). My computer is in CET (+1 GMT).

dateObjectis the result of this resultSet.getTime("date_field")( java.sql.Date )

timeObjectis the result of this resultSet.getDate("time_field")( java.sql.Time )

DATE is stored in the database like 2014-02-22TIME15:00

System.out.println("Untouched "+dateObject+" "+timeObject);

long date = dateObject.getTime();
long time = timeObject.getTime();

System.out.println("Touched "+new Date(date+time));

Results in the next release:

Untouched 2014-02-22 15:00:00
Touched Sat Feb 22 14:00:00 CET 2014

Why is one hour skipped from Touched exit? I expected the following:

Untouched 2014-02-22 15:00:00
Touched Sat Feb 22 15:00:00 CET 2014

To sort things out, I also tried the following:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date(date+time)));

And the result:

2014-02-22 14:00:00

Generally. I expect GMT + 1 to show 16:00 (local) and GMT + 0 to display 15:00

+3
2

SO-answer. toString().

java.util.Date.toString() => output dependent on your system time zone
                             in format pattern "EEE MMM dd HH:mm:ss zzz yyyy"

java.sql.Date.toString() => output in format "yyyy-MM-dd" (your dateObject)
java.sql.Time.toString() => output in format "HH:mm:ss" (your timeObject)

sql . , .

:

:

java.sql.Date dateObj = new java.sql.Date(2014 - 1900, Calendar.FEBRUARY, 22);
Time timeObj = new Time(15, 0, 0);
Time midnight = new Time(0, 0, 0);
Date d = new Date(dateObj.getTime() + timeObj.getTime());

System.out.println("dateObj: " + dateObj + "/" + dateObj.getTime()); 
// dateObj: 2014-02-22/1393023600000, one hour less than a full day because of UTC-long

System.out.println("timeObj: " + timeObj + "/" + timeObj.getTime()); 
// timeObj: 15:00:00/50400000 => one hour less as UTC-long

System.out.println("midnight: " + midnight + "/" + midnight.getTime()); 
// midnight: 00:00:00/-3600000 => one hour less, negative!

System.out.println(new Date(dateObj.getTime())); // Sat Feb 22 00:00:00 CET 2014
System.out.println(new Date(timeObj.getTime())); // Thu Jan 01 15:00:00 CET 1970
System.out.println(d); // Sat Feb 22 14:00:00 CET 2014

: DateObject, timeObject , utc-long - . Date, , , .

: , utc-longs, , , . , undefined! , , / /. , , , :

Date d = new Date(dateObj.getTime() + timeObj.getTime() - midnight.getTime());
System.out.println(d); // Sat Feb 22 15:00:00 CET 2014, correct - what you wanted
0

, ma ( timeObject db 15:00:00 UTC):

TimeZone tz = TimeZone.getTimeZone("Gmt0");
SimpleDateFormat sdfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdfFull.setTimeZone(tz);

Date updateDate = sdfFull.parse(dateObject.toString()+" "+timeObject.toString());

System.out.println(updateDate);

, :

Sat Feb 22 16:00:00 CET 2014
+1

All Articles