java-time

Java 8 Date API vs Calendar / Date / DateFormat

十年热恋 提交于 2019-11-29 03:43:22
There is this new-n-cool Date API in Java 8, the java.time package . I understand that it is better designed, less ambiguous and more thread-safe than the old classes. Still, I am not sure how to go about using it: Should I use it exclusively instead of the old classes? Should I replace existing usages of old classes whereever I spot them because the new stuff is so much better? Should I refrain from using java.util.Calendar, java.util.Date , java.sql.Date and java.text.DateFormat in favor of the new API all together OR are there use cases where the old classes are still preferable ? Has the

Unable to obtain ZonedDateTime from TemporalAccessor when parsing a Date

我是研究僧i 提交于 2019-11-29 02:39:45
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 ZonedDateTime from TemporalAccessor: {},ISO,Europe/Berlin resolved to 2015-11-13 of type java.time

Grouping items by date

走远了吗. 提交于 2019-11-29 02:04:24
I have items in my list and the items have a field, which shows the creation date of the item. and I need to group them based on a "compression", which the user gives. The options are Day , Week , Month and Year . If the user selects day compression, I need to group my items as such that the items, which are created in the same day, will be groupped. In my example above, only item 1 and item 2 are created in the same day. The others are also groups but they will have only one item because at their day, only one item is created. {{item1, item2}, {item3}, {item4}, {item5}, {item6}, {item7}} If

LocalDateTime , ZonedDateTime and Timestamp

匆匆过客 提交于 2019-11-29 01:19:14
问题 I have a SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I have a domain object with 2 properties (initDate, endDate). I want to create 2 converters to deal with mySQL DB @Convert(converter = LocalDateTimeAttributeConverter.class) private LocalDateTime initDate; @Convert(converter = ZonedDateTimeAttributeConverter.class) private ZonedDateTime endDate; the converter 1 (is OK) @Converter public class

How to obtain the end of the day when given a LocalDate?

[亡魂溺海] 提交于 2019-11-29 00:57:31
How to obtain the end of the day when given a LocalDate? I could get it by doing LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59)); But is there an equivalent 'atStartOfDay' method for the end of the day? LocalDate.now().atStartOfDay(); LocalDate.now().atEndOfDay(); //doesn't work Here are a few alternatives, depending on what you need: LocalDate.now().atTime(23, 59, 59); //23:59:59 LocalDate.now().atTime(LocalTime.MAX); //23:59:59.999999999 But there is no built-in method. As commented by @JBNizet, if you want to create an interval, you can also use an interval up to midnight,

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

只愿长相守 提交于 2019-11-28 21:37:09
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 declared as private static classes. How can I configure MongoTemplate to use them to map Instant fields to

Round minutes to ceiling using Java 8

谁都会走 提交于 2019-11-28 21:08:01
So I'm lucky enough to use Java 8 and the new time APi but I don't see any rounding functions... Basically if the time is... 2014-08-28T10:01.00.000 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.10.123 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.25.123 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.49.123 ----> 2014-08-28T10:02.00.000 2014-08-28T10:01.59.999 ----> 2014-08-28T10:02.00.000 This seems to be ok, but is it right? LocalDateTime now = LocalDateTime.now(Clock.systemUTC()); LocalDateTime newTime = now.plusMinutes(1); System.out.println(newTime.toString()); System.out.println

long timestamp to LocalDateTime

て烟熏妆下的殇ゞ 提交于 2019-11-28 20:09:39
I have a long timestamp 1499070300 (equivalent to Mon, 03 Jul 2017 16:25:00 +0800) but when I convert it to LocalDateTime I get 1970-01-18T16:24:30.300 Here's my code long test_timestamp = 1499070300; LocalDateTime triggerTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone .getDefault().toZoneId()); Juan You need to pass timestamp in milliseconds: long test_timestamp = 1499070300000L; LocalDateTime triggerTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone.getDefault().toZoneId()); System.out.println(triggerTime); Result: 2017-07-03T10:25 Or

DateTimeFormatter Support for Single Digit Day of Month and Month of Year

一笑奈何 提交于 2019-11-28 20:05:32
DateTimeFormmater doesn't seem to handle single digit day of the month: String format = "MM/dd/yyyy"; String date = "5/3/1969"; System.out.println(new SimpleDateFormat(format).parse(date)); System.out.println(LocalDate.parse(date, DateTimeFormatter.ofPattern(format))); In this example, SimpleDateFormat correctly parses the date, but DateTimeFormatter throws an exception. If I were to use zero padded dates, e.g., "05/03/1969", both work. However, if either the day of month or the month of year are single digit, then DateTimeFormatter throws an exception. What is the right DateTimeFormatter

Convert between LocalDate and XMLGregorianCalendar

心不动则不痛 提交于 2019-11-28 20:04:20
What's the best way to convert between LocalDate from Java 8 and XMLGregorianCalendar ? Converting from LocalDate to XMLGregorianCalendar : LocalDate date = LocalDate.now(); GregorianCalendar gcal = GregorianCalendar.from(date.atStartOfDay(ZoneId.systemDefault())); XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal); Converting back is simpler: xcal.toGregorianCalendar().toZonedDateTime().toLocalDate(); The LocalDate stores only year/month/day information. There is no time nor time-zone information in it. The XMLGregorianCalendar stores date (year/month/day)