java-time

Coverting String to LocalTime with/without nanoOfSeconds

巧了我就是萌 提交于 2019-12-05 00:55:17
I need to convert a string to LocalTime (java-8 not joda) that may or maynot have nanoOfSeconds in the string. The String format is in the form of 07:06:05 or 07:06:05.123456 The string may or may not have a decimal place in the seconds and when it does there could be any number of characters to represent the Nano Seconds part. Using a DateTimeForamtter such as DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:mm:ss"); or DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:mm:ss.SSSSSS"); I can use an IF statement to distinguish between the two formats such as: DateTimeFormatter dtf;

Convert Instant to microseconds from Epoch time

北城余情 提交于 2019-12-05 00:52:39
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 million. I don't see corresponding methods to obtain more precise timestamp. How can I get number of

Comparing ThreeTen backport to JSR-310

我怕爱的太早我们不能终老 提交于 2019-12-04 20:46:29
问题 For some reasons, we can't use java 8 right now - we're still stuck at java 7. However, I'd like to use the new JSR-310 date/time APIs right now, using the official backport ThreeTen. Its homepage states: The backport is NOT an implementation of JSR-310, as that would require jumping through lots of unnecessary hoops. Instead, this is a simple backport intended to allow users to quickly use the JSR-310 API on Java SE 6 and 7. Questions: What are your experience with ThreeTen? Are there some

New Date & Time API in Java 8

送分小仙女□ 提交于 2019-12-04 18:37:31
问题 On this page i read the following: To do calculations with dates, it is also very easy. Probably the best improvement compared to the current situation with Java < 1.8: Period p = Period.of(2, HOURS); LocalTime time = LocalTime.now(); LocalTime newTime = time.plus(p); // or time.plus(5, HOURS); or time.plusHours(5); I don't clearly see the advantage prior to Versions < 1.8. Maybe someone can give me an example? Atm i am asking myself, where the improvement of the new date & time API comes

Spring Boot issues serializing java.time.LocalDateTime with Jackson to return ISO-8601 JSON timestamps?

十年热恋 提交于 2019-12-04 16:26:54
问题 I'm working on converting some models in a spring-boot REST API app to use java 8's java.time.LocalDateTime instead of joda's DateTime . I want the timestamps returned from API call to adhere to the ISO_8601 format. Motivation is to be forward compatible with Java 8's time (more here). The part that's proving difficult is when it comes to serialize an object containing LocalDateTime to JSON. For example, I have the following entity: // ... misc imports import java.time.LocalDateTime; import

How to get next MonthDay (next Christmas) with java 8 time API?

蹲街弑〆低调 提交于 2019-12-04 16:14:10
Let's say I want to know how many days are until Christmas with a method that works any day of any year so next Christmas may be this year or next year that I don't know if it is a leap year or not. I might calculate the next Christmas date and then calculate the days from now until then. I can represent Christmas Day as MonthDay.of(12, 25) but I can't find how that helps. I found it is easy to calculate the date of next Monday this way: ZonedDateTime nextMonday = ZonedDateTime.now() .with(TemporalAdjusters.next(DayOfWeek.MONDAY)) .truncatedTo(ChronoUnit.DAYS); But I can't find any

Java8 LocalDateTime parsing error

戏子无情 提交于 2019-12-04 13:46:02
I am trying to parse the following timestamp string 03-feb-2014 13:16:31 using java.time but it is throwing an error. Here is my code. String timestamp = "03-feb-2014 13:16:31"; DateTimeFormatter format; DateTimeFormatterBuilder formatBuilder = new DateTimeFormatterBuilder(); formatBuilder.parseCaseInsensitive(); formatBuilder.append(DateTimeFormatter.ofPattern("dd-MMM-YYYY HH:mm:ss")); format = formatBuilder.toFormatter(); LocalDateTime localdatetime = LocalDateTime.parse(timestamp, format); But I am getting the following error. Exception in thread "main" java.time.format

Count days between two dates with Java 8 while ignoring certain days of week

帅比萌擦擦* 提交于 2019-12-04 11:35:14
问题 Below I have 3 methods. The first is very simple. It just counts the total number of days. The second, however, will not only count the days, but will ignore the days of the week that are passed in to the method. My problem is that the third method is not always correct. It should match the second method. I am guessing it has something to do with leap years, because the difference is usually +=3|4 when it is incorrect. Additional Info I am attempting to mock Excel's weekday(serial_number,

Instant Time parsing error (Unable to obtain Instant from TemporalAccessor)

守給你的承諾、 提交于 2019-12-04 04:18:03
问题 After running my program, I get this weird crash occurring after around 2 hours of running it stating that it can't parse the date. Text '2016-10-26T12:31:39.084726218Z' could not be parsed: Unable to obtain Instant from TemporalAccessor: {InstantSeconds=1477485099, NanoOfSecond=84726218},ISO of type java.time.format.Parsed at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919) Does anyone knows why it gives this? Since when looking online, I found that it could be due

How to convert two digit year to full year using Java 8 time API

岁酱吖の 提交于 2019-12-04 03:01:21
问题 I wish to remove the Joda-Time library from my project. I am trying to convert a two digit year to full year. The following code from Joda-Time can fulfil the purpose. Below is the following code of joda-time DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormat.forPattern("yy"); int year = LocalDate.parse("99"", TWO_YEAR_FORMATTER).getYear(); System.out.println(year); Output: 1999 This is the output that I expect and that makes sense in my situation. However, when I try the same procedure