java-time

How to create DateTimeformatter with optional seconds arguments

可紊 提交于 2019-12-19 06:57:52
问题 I am trying to create a DateTimeformatter to validate following date times: String date1 = "2017-07-06T17:25:28"; String date2 = "2017-07-06T17:25:28.1"; String date3 = "2017-07-06T17:25:28.12"; String date4 = "2017-07-06T17:25:28.123"; String date5 = "2017-07-06T17:25:28.1234"; String date6 = "2017-07-06T17:25:28.12345"; String date7 = "2017-07-06T17:25:28.123456"; String date8 = "2017-07-06T17:25:28."; I have tried the following date time formatter to validate above dates: public static

How to create DateTimeformatter with optional seconds arguments

我们两清 提交于 2019-12-19 06:57:26
问题 I am trying to create a DateTimeformatter to validate following date times: String date1 = "2017-07-06T17:25:28"; String date2 = "2017-07-06T17:25:28.1"; String date3 = "2017-07-06T17:25:28.12"; String date4 = "2017-07-06T17:25:28.123"; String date5 = "2017-07-06T17:25:28.1234"; String date6 = "2017-07-06T17:25:28.12345"; String date7 = "2017-07-06T17:25:28.123456"; String date8 = "2017-07-06T17:25:28."; I have tried the following date time formatter to validate above dates: public static

between java.time.LocalTime (next day)

狂风中的少年 提交于 2019-12-18 21:50:56
问题 Please suggest if there is an API support to determine if my time is between 2 LocalTime instances, or suggest a different approach. I have this entity: class Place { LocalTime startDay; LocalTime endDay; } which stores the working day start and end time, i.e. from '9:00' till '17:00', or a nightclub from '22:00' till "5:00". I need to implement a Place.isOpen() method that determines if the place is open at a given time. A simple isBefore / isAfter does not work here, because we also need to

Java 8 date-time: get start of day from ZonedDateTime

二次信任 提交于 2019-12-18 11:37:56
问题 Is there any difference between these: zonedDateTime.truncatedTo(ChronoUnit.DAYS); zonedDateTime.toLocalDate().atStartOfDay(zonedDateTime.getZone()); Any reason to prefer one against the other? Thanks 回答1: Updated for sake of correction: In most cases yes the same , see following example for Brazil when switching from winter to summer time: ZonedDateTime zdt = ZonedDateTime.of(2015, 10, 18, 0, 30, 0, 0, ZoneId.of("America/Sao_Paulo")); // switch to summer time ZonedDateTime zdt1 = zdt

Jackson Serialize Instant to Nanosecond Issue

天大地大妈咪最大 提交于 2019-12-18 07:12:13
问题 Jackson serialises java.time.Instant with WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS enabled by default. It produces JSON like this { "timestamp":1421261297.356000000 } I wonder if there's a way to get rid of the zeros at the end. I want something like: { "timestamp":1421261297.356 } I tried: mapper.configure( SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false ); mapper.configure( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true ); But this configuration changes it to millisecond

Unable to obtain ZonedDateTime from TemporalAccessor when parsing a Date

匆匆过客 提交于 2019-12-18 03:45:38
问题 With Java 1.8.0_51 the following code (taken from Unable to obtain OffsetDateTime from TemporalAccessor) DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneId.of("Europe/Berlin")); OffsetDateTime offsetDateTime = ZonedDateTime.parse("20151113", formatter).toOffsetDateTime(); System.out.println(offsetDateTime.format(DateTimeFormatter.ISO_DATE)); throws an exception: java.time.format.DateTimeParseException: Text '20151113' could not be parsed: Unable to obtain

Java 8 Date/Time (JSR-310) types mapping with Spring Data MongoDB

倖福魔咒の 提交于 2019-12-18 01:16:33
问题 I have simple document with Java 8 date/time fields @Document public class Token { private Instant createdAt; ... } that I want to persist with Spring Data MongoDB version 1.5. But fields of type java.time.Instant could not be de-serialized correctly because MappingMongoConverter lacks converters for java.time classes. In Spring 4 I found org.springframework.format.datetime.standard.DateTimeConverters with different Converter s including InstantToLongConverter and LongToInstantConverter

How can I validate a local date time within daylight savings time?

我只是一个虾纸丫 提交于 2019-12-17 20:35:18
问题 Mar 12, 2017 02:39:00 "America/Chicago" does not exist. When I set the date and time to this value it does not fail. The time gets set to Mar 12, 2017 03:39:00 an hour later. How can I be notified that this time does not exist. Here is how the time skips forward 01:59:59 3:00:00 As you can see 02:39:00 will never exist on this date. Here is the code I am using package com.company; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time

Java 8 DateTimeFormatter parsing for optional fractional seconds of varying significance

混江龙づ霸主 提交于 2019-12-17 19:32:45
问题 My MCVE (as a TestNG unit test): public class MyDateTimeFormatterTest { private static final String BASE_PATTERN = "yyyy/MM/dd HH:mm:ss"; private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(BASE_PATTERN + "[.SSSSSSSSS]"); private static final LocalDateTime TEST_INPUT = LocalDateTime.of(2015, 5, 4, 12, 34, 56, 123456789); @DataProvider(name = "test-cases") public Iterator<Object[]> getTestCases() { return Arrays.asList(testFor("", ChronoUnit.SECONDS), testFor(".SSS",

java.util.Date Calculate difference in days

佐手、 提交于 2019-12-17 16:39:25
问题 I tried to calculate the difference between two dates and I noticed one thing. When calculating only the days, the start of daylight saving time is included in the interval, so the result will be shorter with 1 day. To obtain accurate results, the value of hours also must be considered. For example: SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy"); Date dfrom = format.parse("03-29-2015"); Date dto = format.parse("03-30-2015"); long diff = dto.getTime() - dfrom.getTime(); System