This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReadDateZeroWithJDBC { | |
public static void main(String args[]) throws ClassNotFoundException, SQLException { | |
Connection conn = setupJDBCAndConnect(); | |
//expected epoch unix time | |
java.util.Date dateExpected = new java.util.Date(0); | |
//actual created_at_utc from the user record in DB | |
PreparedStatement stmtRead = createSelectStatement(conn, 1); | |
ResultSet rs = stmtRead.executeQuery(); | |
rs.next(); | |
java.util.Date dateRetrieved = new Date(rs.getTimestamp(3).getTime()); | |
System.out.println("Dates comparison result:" | |
+ "\nExpected: " + dateExpected | |
+ "\nExpected as UTC0: " + dateExpected.toGMTString() | |
+ "\nExpected as long: " + dateExpected.getTime() | |
+ "\nActual: " + dateRetrieved | |
+ "\nActual as UTC0: " + dateRetrieved.toGMTString() | |
+ "\nActual as long: " + dateRetrieved.getTime()); | |
conn.close(); | |
} | |
private static PreparedStatement createSelectStatement(Connection conn, int userId) throws SQLException { | |
String sql = "SELECT * FROM users WHERE id = ?"; | |
PreparedStatement stmt = conn.prepareStatement(sql); | |
stmt.setInt(1, userId); | |
return stmt; | |
} | |
private static Connection setupJDBCAndConnect() throws ClassNotFoundException, SQLException { | |
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); | |
String url = "jdbc:sqlserver://localhost;databaseName=test"; | |
String userName = "tester"; | |
String password = "test"; | |
return DriverManager.getConnection(url, userName, password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment