java-time

Java Time: Get max number of weeks for particular year

五迷三道 提交于 2019-12-02 01:25:04
问题 I only found a solution for Joda Time. My solution works only if the last day is not in the first week: LocalDate.now() // or any other LocalDate .withDayOfMonth(31) .withMonth(12) .get(weekFields.weekOfWeekBasedYear()) So what is the correct way in Java Time (like in Joda Time)? 回答1: This information is available directly using the java.time.* API. The key method is rangeRefinedBy(Temporal) on TemporalField . It allows you to obtain a ValueRange object that provides the minimum and maximum

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

血红的双手。 提交于 2019-12-01 22:34:48
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 to an incorrect format, but since I did not specify the format this is not the case for me. The code

How to convert timestamp string to epoch time?

南楼画角 提交于 2019-12-01 21:04:11
I have time stamp in format 2017-18-08 11:45:30.345 . I want to convert it to epoch time, so I am doing below: String timeDateStr = "2017-18-08 11:45:30.345"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss.SSS"); ZonedDateTime zdt = ZonedDateTime.parse(timeDateStr, dtf); System.out.println(zdt.toInstant().toEpochMilli()); I am getting below error: java.time.format.DateTimeParseException: Text '2017-18-08 11:45:30.345' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor I also tried different formats but still getting errors. Note : originally the

How to convert timestamp string to epoch time?

余生长醉 提交于 2019-12-01 20:30:56
问题 I have time stamp in format 2017-18-08 11:45:30.345 . I want to convert it to epoch time, so I am doing below: String timeDateStr = "2017-18-08 11:45:30.345"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss.SSS"); ZonedDateTime zdt = ZonedDateTime.parse(timeDateStr, dtf); System.out.println(zdt.toInstant().toEpochMilli()); I am getting below error: java.time.format.DateTimeParseException: Text '2017-18-08 11:45:30.345' could not be parsed: Unable to obtain

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

烂漫一生 提交于 2019-12-01 15:59:28
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 with java.time API, it produces a DatetimeParseException. Below is the following code of java.time API:

How to covert Joda-Time's DateTimeFormat.forStyle() to JSR 310 JavaTime?

人走茶凉 提交于 2019-12-01 13:18:48
I working on convertion Grails Joda-Time plugin to JavaTime . And I've old Joda time code like this: def style switch (type) { case LocalTime: style = '-S' break case LocalDate: style = 'S-' break default: style = 'SS' } Locale locale = LocaleContextHolder.locale return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT) How can I conver it to JSR 310? I can't find anything similar to method forStyle(String style) which accepts style. UPD I found workaround: Locale locale = LocaleContextHolder.locale DateTimeFormatter formatter switch (type) { case LocalTime:

“plusDays” doesn't advance LocalDate in Java 8

99封情书 提交于 2019-12-01 11:30:30
Why is LocalDate not changing even though there is no error during running? DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate date = LocalDate.parse("2005-12-12", formatter); date.plusDays(3); System.out.println(date.toString()); Output: 2005-12-12 Anything I missed out? LocalDate is immutable date = date.plusDays(3); As a String , it doesn't have an effect calling a method on it without assigning the result : date = date.plusDays(3); Read More 来源: https://stackoverflow.com/questions/33333201/plusdays-doesnt-advance-localdate-in-java-8

Java how to get list of time zone ID’s for the given time zone abbreviation

孤者浪人 提交于 2019-12-01 11:12:47
Is it possible to find the list of time zone ID's for a given time zone abbreviation? For example, for the abbreviation IST , the time zone ID's are Asia/Jerusalem , Asia/Kolkata and Europe/Dublin . Interesting question. Since the abbreviations aren’t standardized, there cannot be an authoritative answer nor a bulletproof way to get such an answer. Off the top of my head I thought of two approaches: Get them from your JVM. Find them on the net. Getting the zones from your JVM: String givenAbbr = "IST"; LocalDateTime summerSouthernHemisphere = LocalDate.of(2018, Month.JANUARY, 31).atStartOfDay(

Parsing 2-digit years: Setting the pivot date with an unknown date pattern

梦想的初衷 提交于 2019-12-01 10:39:53
The user will input dates in different patterns to my application. For 2-digit years, he must also determine the pivot date. Example: pattern = yy-MM-dd pivot date = 70 (I add the current millennium and the last century for more dynamics programmatically => 1970 ) 69-04-22 becomes 2069-04-22 70-04-22 becomes 1970-04-22 Like described in this answer it's easy to build a DateTimeFormatter when the date pattern (here ddMMyy ) is known. But since the user inputs the pattern, I'm not sure how to build the DateTimeFormatterBuilder properly. I can't hard code it. Do I have to parse the pattern by

Issue with DateTimeParseException when using STRICT resolver style

风格不统一 提交于 2019-12-01 09:14:32
I am trying to parse a date string using the following pattern: yyMMdd and the STRICT resolver as follows: DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT); LocalDate.parse(expiryDate, formatter); I get the following DateTimeParseException : java.time.format.DateTimeParseException: Text '160501' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2016, MonthOfYear=5, DayOfMonth=1},ISO of type java.time.format.Parsed When I swith to the default resolve style, i.e. ResolverStyle.SMART it allows such dates