java-time

(Simple)DateFormat that allow 24:00:00 and 00:00:00 as inputs

早过忘川 提交于 2019-12-10 04:22:20
问题 I've been looking for this for a while, with no success so far. Do you know if there's a "DateFormat" ish class, that will allow me to use "00:00:00" and "24:00:00" as input parameters (they're both midnight) but when called "getHour()" I'll get 0 or 24 as a result? Using "kk" will only allow me to have <1:24> range, meanwhile I'm looking for <0:24> range formatting 回答1: The value 24:00 is not represented in a LocalTime because it is strictly part of the next day. Consideration was given to

Convert Instant to microseconds from Epoch time

倾然丶 夕夏残阳落幕 提交于 2019-12-10 01:45:50
问题 In Instant there are methods: toEpochMilli which converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z getEpochSecond which gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z . Both of these methods lose precision, e.g. in toEpochMilli JavaDoc I see: If this instant has greater than millisecond precision, then the conversion drop any excess precision information as though the amount in nanoseconds was subject to integer division by one

In java.time, how is the result of adding a month calculated?

纵然是瞬间 提交于 2019-12-10 00:40:50
问题 In the JSR-310 java.time API in JDK 8, what are the rules for calculating the result of adding a month to a date. In particular, what happens when you add 1 month to a date like January 31st? LocalDate initial = LocalDate.of(2012, 1, 31); // 31st January 2012 LocalDate result = initial.plusMonths(1); // what is the result? 回答1: Short answer: In the example, the result will be the last day of February, 2012-02-29 . Explanation: The question, "what date do you get if you add a month", is one

Java 8 LocalDateTime round to next X minutes

点点圈 提交于 2019-12-09 17:58:28
问题 I want to convert Java 8 LocalDateTime to nearest 5 minutes. E.g. 1601 -> 1605 1602 -> 1605 1603 -> 1605 1604 -> 1605 1605 -> 1605 1606 -> 1610 1607 -> 1610 1608 -> 1610 1609 -> 1610 1610 -> 1610 I would like to use existing functionality of LocalDateTime or Math api. Any suggestions? 回答1: You can round towards the next multiple of five minutes using: LocalDateTime dt = … dt = dt.withSecond(0).withNano(0).plusMinutes((65-dt.getMinute())%5); You can reproduce your example using LocalDateTime

java.time ISO date format with fixed millis digits (in Java 8 and later)

那年仲夏 提交于 2019-12-09 14:40:16
问题 By default, the toString method of Instant uses the DateTimeFormatter.ISO_INSTANT formatter. That formatter won’t print the digits for fraction-of-second if they happen to be 0. java-time examples: 2015-10-08T17:13:07.589Z 2015-10-08T17:13:07Z Joda-Time examples (and what I'd expect from java.time): 2015-10-08T17:13:07.589Z 2015-10-08T17:13:07.000Z This is really frustrating to parse in some systems. Elasticsearch was the first problem I encountered, there's no pre-defined format that

Java8 Adding Hours To LocalDateTime Not Working

旧时模样 提交于 2019-12-09 14:14:21
问题 I tried like below, but in both the cases it is showing same time? What i am doing wrong. LocalDateTime currentTime = LocalDateTime.now(ZoneId.of("UTC")); Instant instant = currentTime.toInstant(ZoneOffset.UTC); Date currentDate = Date.from(instant); System.out.println("Current Date = " + currentDate); currentTime.plusHours(12); Instant instant2 = currentTime.toInstant(ZoneOffset.UTC); Date expiryDate = Date.from(instant2); System.out.println("After 12 Hours = " + expiryDate); "Current Date"

JSTL LocalDateTime format

☆樱花仙子☆ 提交于 2019-12-09 05:23:03
问题 I want to format my Java 8 LocalDateTime object in "dd.MM.yyyy " pattern. Is there any library to format? I tried code below but got conversion exception. <fmt:parseDate value="${date}" pattern="yyyy-MM-dd" var="parsedDate" type="date" /> Is there any tag or converter for LocalDateTime class in JSTL? 回答1: Actually I had the same problem and ended up forking the original Joda Time jsp tags to create Java 8 java.time JSP tags. With that library your example would be something like this:

Java 8 Offset Date Parsing

独自空忆成欢 提交于 2019-12-09 02:54:22
问题 I need to parse a String in the following format 2015-01-15-05:00 to LocalDate(or smth else) in UTC. The problem is that the following code: System.out.println(LocalDate.parse("2015-01-15-05:00", DateTimeFormatter.ISO_OFFSET_DATE)); outputs 2015-01-15 ignoring the offset. The desired output is 2015-01-16 Thanks in advance! 回答1: The simplest answer is to use OffsetDateTime to represent the data, but you need to default the time: DateTimeFormatter fmt = new DateTimeFormatterBuilder() .append

How to store OffsetDateTime to PostgreSQL “timestamp with time zone” column

非 Y 不嫁゛ 提交于 2019-12-09 02:04:06
问题 I am trying to store OffsetDateTime("2019-01-14 21:10:00.02+03") with timezone(+03) using JDBC to PostgreSQL. But when retrieve data using sql query I always get the +00 result. Is there any way to store offset (+03) with datetime in postgres ? 回答1: Is there any way to store offset (+03) with datetime in postgres ? Yes, but as @Andreas says, the offset will have to be in a separate column. The column type name timestamp with time zone in PostgreSQL is a bit misleading; it is really just

Confusion with Java Time parsing UTC

百般思念 提交于 2019-12-08 16:01:08
问题 I am confused with time handling in java time. I so long worked under the assumption that if a timestamp is specified as a zulu time, java would take care of the offset with regards to local time. To illustrate. I am currently in BST which has an offset of UTC +1. With that in mind, I would expect this zulu time: 2016-09-12T13:15:17.309Z to be 2016-09-12T14:15:17.309 LocalDateTime after parsing it. This is because my default systemtime is set to BST and the above timestamp (zulu time)