JDBC bleibt bei 1900 falscher Zeitstempel bestehenJava

Java-Forum
Anonymous
 JDBC bleibt bei 1900 falscher Zeitstempel bestehen

Post by Anonymous »

Ich entwickle eine App, die einige Daten mit dem Zeitstempel im sehr-pasten speichern. Aber ich möchte den Zeitstempel bei UTC (GMT+0) speichern. ">

Code: Select all

    

org.postgresql
postgresql
42.7.1


< /code>
public class Main {

/*
create table theentity (
theid integer not null,
thevalue timestamp(6),
primary key (theid)
)
*/
public static void main(String[] args) {
TimeZone.setDefault( TimeZone.getTimeZone( ZoneId.of( "Europe/Paris" ) ) );
LocalDateTime d_1900_01_01_T_00_09_23 = LocalDateTime.of( 1900, 1, 1, 0, 9, 23, 0 );
LocalDateTime d_1900_01_01_T_00_09_22 = LocalDateTime.of( 1900, 1, 1, 0, 9, 22, 0 );
LocalDateTime d_1900_01_01_T_00_09_21 = LocalDateTime.of( 1900, 1, 1, 0, 9, 21, 0 );
LocalDateTime d_1900_01_01_T_00_09_20 = LocalDateTime.of( 1900, 1, 1, 0, 9, 20, 0 );
LocalDateTime d_1900_01_01_T_00_09_19 = LocalDateTime.of( 1900, 1, 1, 0, 9, 19, 0 );

try(Connection c = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/hibernate_orm_test?preparedStatementCacheQueries=0&escapeSyntaxCallMode=callIfNoReturn",
"postgres", "root")) {
PreparedStatement p = c.prepareStatement( "insert into theentity values(?, ?)" );
bindAndExecute( p, 1, d_1900_01_01_T_00_09_23 );
bindAndExecute( p, 2, d_1900_01_01_T_00_09_22 );
bindAndExecute( p, 3, d_1900_01_01_T_00_09_21 );
bindAndExecute( p, 4, d_1900_01_01_T_00_09_20 );
bindAndExecute( p, 5, d_1900_01_01_T_00_09_19 );
} catch (Exception e) {
e.printStackTrace();
}
}

private static void bindAndExecute(PreparedStatement p, int id, LocalDateTime localDateTime)
throws SQLException {
p.setInt( 1, id );
p.setTimestamp(2,
Timestamp.valueOf( localDateTime ),
Calendar.getInstance( TimeZone.getTimeZone( ZoneId.of( "GMT" ) ) )
);
p.executeUpdate();
}

}
In der Sortierung habe ich versucht, 5 Zeitstempel zu bestehen (beachten Sie, dass diese Zeitstempel aufgrund von TimeZone.setDefault (Timezone.getTimezone (zoneId.of ("Europa/Paris" auf Europa/Paris basieren ("Europa/Paris", basieren auf Europa ("Europa/Paris". ))); < /code>) < /p>
  • 1900-01-01T00: 09: 23 < /li>
    1900-01 -01t00: 09: 22
  • 1900-01-01T00: 09: 21
  • 1900-01-01T00: 09: 20
  • 1900-01-01T00: 09: 29
Diese Linie würde Europa/Paris TZ in UTC TZ übersetzen:

Code: Select all

p.setTimestamp(2,
Timestamp.valueOf( localDateTime ),
Calendar.getInstance( TimeZone.getTimeZone( ZoneId.of( "GMT" ) ) ) );
< /code>
Führen Sie 5 Zeilen in PostgreSQL aus: < /p>
theid |      thevalue
-------+---------------------
1 | 1900-01-01 00:00:02
2 | 1900-01-01 00:00:01
3 | 1900-01-01 00:00:00
4 | 1899-12-31 23:09:20
5 | 1899-12-31 23:09:19
(5 rows)
< /code>
Die meisten von Ihnen denken vielleicht, dass Paris bei GMT+1 ist. Es ist richtig, war aber nicht korrekt. Mit 1900 war es bei GMT+00: 09: 21 (9 Minuten, 21 Sekunden) !! < /p>
Also wurden die ersten 3 Zeitstempel korrekt auf die Tabelle gespeichert. Aber das Seltsame ist in Reihe 4. Es ist 1899-12-31 23:09:20 
Aber ich würde erwartet, dass es 1899-12-31 23:59:59 sein würde. Aber scheint die alte API der DateTime -API um 1899 zu sehen, dass sie bei GMT+1 liegt. Es verursachte Zeile 4, 5 und so auf ... sich irren !!
Kannst ihr das erklären?>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post