java-time

What is the Standard way to Parse different Dates passed as Query-Params to the REST API?

。_饼干妹妹 提交于 2019-12-02 18:08:05
问题 I am working on a REST API which supports Date as a query param. Since it is Query param it will be String. Now the Date can be sent in the following formats in the QueryParams: yyyy-mm-dd[(T| )HH:MM:SS[.fff]][(+|-)NNNN] It means following are valid dates: 2017-05-05 00:00:00.000+0000 2017-05-05 00:00:00.000 2017-05-05T00:00:00 2017-05-05+0000 2017-05-05 Now to parse all these different date-times i am using Java8 datetime api. The code is as shown below: DateTimeFormatter formatter = new

Java Time period in decimal number of years

旧时模样 提交于 2019-12-02 17:01:30
问题 If I calculate the difference between 2 LocalDate 's in java.time using: Period p = Period.between(testDate, today); Then I get an output with the number of years, months, days like: Days = 9 Months = 6 Years = 18 Does anyone know a clean way to represent that as a decimal type value (ie, above would be something around 18.5... )? 回答1: I would avoid using Period, and instead just calculate the difference in days: float years = testDate1.until(today, ChronoUnit.DAYS) / 365.2425f; 回答2: You

Save a formatted String to a LocalDate

六月ゝ 毕业季﹏ 提交于 2019-12-02 15:41:01
问题 I have a DateConverter class: public class DateConverter extends StringConverter<LocalDate> { String pattern = "EEE, dd. MM. uuuu"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern,Locale.US); @Override public String toString(LocalDate object) { try { return formatter.format(object); } catch(Exception ex) { return ""; } } @Override public LocalDate fromString(String string) { try { return LocalDate.parse(string, formatter); } catch(Exception ex) { return null; } } public

DateTimeParseException: Text '2019-06-07 12:18:16' could not be parsed

对着背影说爱祢 提交于 2019-12-02 13:15:43
I have following code to convert an Instant to String then convert it back to I String timestampString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")); LOGGER.info("timestampString: " + timestampString); Instant instant = LocalDateTime.parse(timestampString, DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")).toInstant(ZoneOffset.UTC); it print the timestampString as: 2019-06-07 12:45:57 and failed at parse the string: java.time.format.DateTimeParseException: Text '2019-06-07 12:45:57' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor:

Converting date without time for different timezone

狂风中的少年 提交于 2019-12-02 08:54:31
问题 I store the date in UTC long and displayed in user timezone. But when I try to store only days without time it misleading to different dates. Eg: Scheduling event on 05/06/2016 (06 May 2016). This date is unique for all regions without timezone. If user from GMT+5:30 timezone trying to add a event on 05/06/2016 then it ISO-8601 format is 2016-05-05T16:00:00.000Z and milliseconds is 1462464000000 . Then user from GMT timezone try to view this event. The date will be 05/05/2016 instead of 05/06

Java Time period in decimal number of years

核能气质少年 提交于 2019-12-02 08:43:16
If I calculate the difference between 2 LocalDate 's in java.time using: Period p = Period.between(testDate, today); Then I get an output with the number of years, months, days like: Days = 9 Months = 6 Years = 18 Does anyone know a clean way to represent that as a decimal type value (ie, above would be something around 18.5... )? I would avoid using Period, and instead just calculate the difference in days: float years = testDate1.until(today, ChronoUnit.DAYS) / 365.2425f; You mentioned in one of your comments that you need quarter year precision if you need the current quarter you can use

String to ZonedDateTime is changing format

 ̄綄美尐妖づ 提交于 2019-12-02 08:12:40
问题 String ip="2011-05-01T06:47:35.422-05:00"; ZonedDateTime mzt = ZonedDateTime.parse(ip).toInstant().atZone(ZoneOffset.UTC); System.out.println(mzt); System.out.println("-----"); String ip2="2011-05-01T00:00:00.000-05:00"; ZonedDateTime mzt2 = ZonedDateTime.parse(ip2).toInstant().atZone(ZoneOffset.UTC); System.out.println(mzt2); Output: 2011-05-01T11:47:35.422Z ----- 2011-05-01T05:00Z Why is the date format getting changed in case 2? I am getting SQLServer Database error due to this. 回答1: This

Save a formatted String to a LocalDate

半世苍凉 提交于 2019-12-02 07:18:32
I have a DateConverter class: public class DateConverter extends StringConverter<LocalDate> { String pattern = "EEE, dd. MM. uuuu"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern,Locale.US); @Override public String toString(LocalDate object) { try { return formatter.format(object); } catch(Exception ex) { return ""; } } @Override public LocalDate fromString(String string) { try { return LocalDate.parse(string, formatter); } catch(Exception ex) { return null; } } public String getPattern(){ return pattern; } } And I have this piece of code: allProd.forEach((prod) -> {

Converting date without time for different timezone

百般思念 提交于 2019-12-02 07:15:13
I store the date in UTC long and displayed in user timezone. But when I try to store only days without time it misleading to different dates. Eg: Scheduling event on 05/06/2016 (06 May 2016). This date is unique for all regions without timezone. If user from GMT+5:30 timezone trying to add a event on 05/06/2016 then it ISO-8601 format is 2016-05-05T16:00:00.000Z and milliseconds is 1462464000000 . Then user from GMT timezone try to view this event. The date will be 05/05/2016 instead of 05/06/2016 . Is there any way to convert date without any timezone. Java 8 provides the solution for your

String to ZonedDateTime is changing format

人盡茶涼 提交于 2019-12-02 05:56:57
String ip="2011-05-01T06:47:35.422-05:00"; ZonedDateTime mzt = ZonedDateTime.parse(ip).toInstant().atZone(ZoneOffset.UTC); System.out.println(mzt); System.out.println("-----"); String ip2="2011-05-01T00:00:00.000-05:00"; ZonedDateTime mzt2 = ZonedDateTime.parse(ip2).toInstant().atZone(ZoneOffset.UTC); System.out.println(mzt2); Output: 2011-05-01T11:47:35.422Z ----- 2011-05-01T05:00Z Why is the date format getting changed in case 2? I am getting SQLServer Database error due to this. This is what toString from documentation said The format used will be the shortest that outputs the full value of