Search results
Results from the WOW.Com Content Network
To make timestamp-to-date conversion correct when the timestamp and the JVM are using different time zones you can use Joda Time's LocalDateTime (or LocalDateTime in Java8) like this: Timestamp timestamp = resultSet.getTimestamp("time_column"); LocalDateTime localDateTime = new LocalDateTime(timestamp);
About java.time. The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat. The Joda-Time project, now in maintenance mode, advises migration to the java.time classes. To learn more, see the Oracle Tutorial. And search Stack ...
Also, the java.sql.Timestamp class which you're using there should not be abused as it's specific to the JDBC API in order to be able to store or retrieve a TIMESTAMP/DATETIME column type in a SQL database and convert it from/to java.util.Date.
The java.sql.Timestamp class has a valueOf method to directly parse such strings. String input = "2007-11-11 12:13:14" ; java.sql.Timestamp ts = java.sql.Timestamp.valueOf( input ) ; java.time. In Java 8 and later, the java.time framework makes it easier to verify the results.
No need to ever use java.util.Date or java.sql.Timestamp again. The java.util.Date class represents a moment on the timeline in UTC with resolution of milliseconds . That class is now replaced by java.time.Instant , also a moment in UTC , but with a finer resolution of nanoseconds .
java.util.Date time=new java.util.Date((long)timeStamp*1000); If you already had milliseconds, then just new java.util.Date((long)timeStamp); From the documentation: Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
java.time.Instant. A java.sql.Timestamp maps to a java.time.Instant, a moment on the timeline in UTC. Notice the new conversion method toInstant added to the old class. java.sql.Timestamp ts = myResultSet.getTimestamp( … ); Instant instant = ts.toInstant(); Time Zone. Apply the desired/expected time zone (ZoneId) to get a ZonedDateTime.
288. You may use java.util.Date class and then use SimpleDateFormat to format the Date. Date date=new Date(millis); We can use java.time package (tutorial) - DateTime APIs introduced in the Java SE 8. var instance = java.time.Instant.ofEpochMilli(millis); var localDateTime = java.time.LocalDateTime.
A java.sql.Date object does indeed have a time-of-day. That time is set to the first moment of the day in UTC, 00:00:00.000. Clearly stated in the JavaDoc. The java.sql.Date class is a hack, created because in its early years Java lacked a date-only type to match the date-only DATE type in SQL.
You can use SimlpeDateFormat to format your date like this: long unixSeconds = 1372339860; // convert seconds to milliseconds. Date date = new java.util.Date(unixSeconds*1000L); // the format of your date. SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");