java-time

`uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?

寵の児 提交于 2019-11-26 19:06:01
The DateTimeFormatter class documentation says about its formatting codes for the year: u year year 2004; 04 y year-of-era year 2004; 04 … Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. For parsing, this will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive. If the count of letters is less than four (but not two), then the sign is only output for negative years as per SignStyle

What's the difference between java 8 ZonedDateTime and OffsetDateTime?

梦想的初衷 提交于 2019-11-26 18:47:03
问题 I've read the documentation, but I still can't get when I should use one or the other: OffsetDateTime ZonedDateTime According to documentation OffsetDateTime should be used when writing date to database, but I don't get why. 回答1: Q: What's the difference between java 8 ZonedDateTime and OffsetDateTime? The javadocs say this: " OffsetDateTime , ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant.

How to get milliseconds from LocalDateTime in Java 8

旧巷老猫 提交于 2019-11-26 17:56:15
问题 I am wondering if there is a way to get current milliseconds since 1-1-1970 (epoch) using the new LocalDate , LocalTime or LocalDateTime classes of Java 8. The known way is below: long currentMilliseconds = new Date().getTime(); or long currentMilliseconds = System.currentTimeMillis(); 回答1: I'm not entirely sure what you mean by "current milliseconds" but I'll assume it's the number of milliseconds since the "epoch," namely midnight, January 1, 1970 UTC. If you want to find the number of

How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?

我是研究僧i 提交于 2019-11-26 17:48:29
问题 If I use a pattern like d/M/yy for creating a Java 8 DateTimeFormatter (e.g. using DateTimeFormatter.ofPattern(pattern); (which I will only use for parsing, not formatting), it will interpret all two-letter years as 20xx, e.g. parsing a string like 13/5/99 to be interpreted as 2099-05-13 , which in my case is wrong (it was meant to be in the year 1999). In my application, I'm trying to parse dates from OCR'd documents, which could e.g. still be from the 90ies, so having the old

How to set format of string for java.time.Instant using objectMapper?

蓝咒 提交于 2019-11-26 17:45:48
问题 I have an entity with java.time.Instant for created data field: @Getter @Setter @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode public class Item { private String id; private String url; private Instant createdDate; } I am using com.fasterxml.jackson.databind.ObjectMapper to save item to Elasticsearch as JSON: bulkRequestBody.append(objectMapper.writeValueAsString(item)); ObjectMapper serializes this field as an object: "createdDate": { "epochSecond": 1502643595, "nano": 466000000 }

Format Instant to String

对着背影说爱祢 提交于 2019-11-26 17:20:24
I'm trying to format an Instant to a String using the new java 8 time-api and a pattern: Instant instant = ...; String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant); Using the code above I get an Exception which complains an unsupported field: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra at java.time.Instant.getLong(Instant.java:608) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) ... JodaStephen Time Zone To format an Instant a time-zone is required. Without a time-zone, the formatter does not

What is the motivation for two different week-based-year definitions in JSR-310?

非 Y 不嫁゛ 提交于 2019-11-26 17:13:57
问题 These are the two fields in the package java.time.temporal : IsoFields.WEEK_BASED_YEAR WeekFields.ISO.weekBasedYear() ISO-8601 defines a so-called week date besides the other two kinds of date, namely the usual calendar date (consisting of year, month and day-of-month) and the ordinal date (consisting of year and day-of-year). A weekdate is defined in the format YYYY-'W'ww-e . w stands for the week-of-year, e for the numerical ISO-day-of-week. Y stands for the week-based-year and is identical

Calculate days between two dates in Java 8

佐手、 提交于 2019-11-26 17:11:35
I know there are lots of questions on SO about how to get, but I want and example using new Java 8 Date api. I also know JodaTime library, but I want a work way without external libraries. Function needs to complain with these restrictions: Prevent errors from date savetime Input are two Date's objects (without time, I know localdatetime, but I need to do with date instances) syntagma If you want logical calendar days , use DAYS.between() method from java.time.temporal.ChronoUnit : LocalDate dateBefore; LocalDate dateAfter; long daysBetween = DAYS.between(dateBefore, dateAfter); If you want

Persist java.time.Instant (JDK8) with JPA2/Hibernate

孤街浪徒 提交于 2019-11-26 16:59:21
问题 Neither JPA nor Hibernate currently support the new date/time classes brought by JSR-310 in JDK8 (JPA ticket, Hibernate ticket). Nonetheless, I'd like to code with the JDK8 date/time classes as they are finally well designed. In particular, I'm interested in java.time.Instant , not in full support for all java.time.* types, as all my entities will use this particular class (or so I think now, at least :-) One option is to write a type converter, as defined by JPA 2.1. However, our app server

java DateTimeFormatterBuilder fails on testtime

落花浮王杯 提交于 2019-11-26 16:49:20
I have a simple jUnit test for DateTimeFormatterBuilder . At runtime it works, when some String comes on Spring-MVC hanlder ( @RequestParam ) At testtime it fails with the same String value. Tested value: 25-May-2018 11:10 Method to be tested: public void getTimeDifference(@RequestParam String startDate, @RequestParam String endDate) { DateTimeFormatter DATE_TIME_FORMAT = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yyyy HH:mm").toFormatter(); LocalDateTime.parse(startDate,DATE_TIME_FORMAT); return messages; } Test-Method: @Test public void testFormat() throws