![]() Never use the 3-4 letter pseudo-zones such as EST or IST as they are not true time zones, not standardized, and not even unique(!). Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. To get the time-of-day, you must specify the wall-clock time of the region expected by the user (the time zone). ![]() The Instant object retrieved via JDBC from the database is in UTC, by definition. You just fetched a date-time directly from a database column of type similar to the SQL-standard TIMESTAMP WITH TIME ZONE.įor sending data to the database, use a PreparedStatement with placeholders. Instant instant = myResultSet( …, Instant.class ) Use appropriate objects to exchange data with your database, not mere strings.Īs of JDBC 4.2, you can directly exchange java.time objects via getObject & setObject. I have to fetch time stamp from DB and retrieve only time and compare two time. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Throwing away valuable information (zone/offset info) is not a good practice. The Z on the end of the input String is short for Zulu and means UTC. The Answer by MadProgrammer is headed in the right direction by using java.time but uses the wrong class: LocalDateTime class purposely lacks any concept of time zone or offset-from-UTC but our input string does. The modern approach uses the java.time classes that supplant the troublesome old legacy classes such as & Date & Calendar. toMillis() // Get entire span as a count of milliseconds. Each argument is a `ZonedDateTime` object here. ![]() Returns a `ZonedDateTime` object.Īnd… Duration.between( zdtStartOfDay, zdt ) // Represent the span of time between the first moment of the day and the target moment. atStartOfDay( ZoneId.of( "Africa/Tunis" ) ) // Determine the first moment of the day (not always 00:00). ![]() toLocalDate() // Extract the date-only value, without time-of-day and without time zone. atZone( ZoneId.of( "Africa/Tunis" ) ) // Adjust into a time zone other than UTC. Instant.class // Extract from database as a `Instant` object in UTC, via JDBC. …, // Specify the column in database of type `TIMESTAMP WITH TIME ZONE`. Tl dr myResultSet.getObject( // Use JDBC 4.2 or later to get *java.time* objects rather than mere strings. LocalDateTime to = LocalDateTime.parse("T09:45:44.000Z", ISODateTimeFormat.dateTime()) ![]() LocalDateTime from = LocalDateTime.parse("T09:39:14.000Z", ISODateTimeFormat.dateTime()) If you're not using Java 8, then use Joda-Time which works in similar way Joda-Time example. (fromTime + " compareTo " + toTime + " = " + pareTo(toTime)) (fromTime + " equals " + toTime + " = " + fromTime.equals(toTime)) (fromTime + " after " + toTime + " = " + fromTime.isAfter(toTime)) (fromTime + " before " + toTime + " = " + fromTime.isBefore(toTime)) LocalDateTime to = LocalDateTime.parse("T09:45:44.000Z", DateTimeFormatter.ISO_DATE_TIME) The simplest solution would be to use Java 8's Date/Time API LocalDateTime from = LocalDateTime.parse("T09:39:14.000Z", DateTimeFormatter.ISO_DATE_TIME) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |