java-time

Is there a equivalent to ChronoUnit.between that returns fraction instead of integer?

余生颓废 提交于 2021-02-18 22:35:27
问题 Methods such is ChronoUnit.HOURS.between(start, end) returns long so I can't get the fraction from there. Is there an alternative method/approach that would return a fraction? 回答1: The whole point of ChronoUnit.HOURS.between(start, end) is to get the number of HOURS between the two timepoints. For example: it's either 1 or 2 hours between, there is no such thing as 1.5658 hours*. If you need more precicion, use another ChronoUnit, i.e. Minutes or seconds. The problem with decimal fractions is

Java 8 DateTimeFormatter parsing optional sections

£可爱£侵袭症+ 提交于 2021-02-18 22:08:00
问题 I need to parse date-times as strings coming as two different formats: 19861221235959Z 1986-12-21T23:59:59Z The following dateTimeFormatter pattern properly parses the first kind of date strings DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X") but fails on the second one as dashes, colons and T are not expected. My attempt was to use optional sections as follows: DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X") Unexpectedly, this parses the second kind of date

Java 8 DateTimeFormatter parsing optional sections

倾然丶 夕夏残阳落幕 提交于 2021-02-18 22:04:46
问题 I need to parse date-times as strings coming as two different formats: 19861221235959Z 1986-12-21T23:59:59Z The following dateTimeFormatter pattern properly parses the first kind of date strings DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X") but fails on the second one as dashes, colons and T are not expected. My attempt was to use optional sections as follows: DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X") Unexpectedly, this parses the second kind of date

What is the difference between ZonedDateTime.withZoneSameInstant and ZonedDateTime.withZoneSameLocal?

北慕城南 提交于 2021-02-18 05:49:58
问题 Let's say I have a ZonedDateTime: ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("US/Pacific")); I would like to know which date/time it is let's say in Berlin. I have two methods : zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/Berlin")); // probably this is the right one to get the corresponding date/time in Berlin zonedDateTime.withZoneSameLocal(ZoneId.of("Europe/Berlin")); The docs for the withZoneSameLocal method say: "The local date-time is only

What is the difference between ZonedDateTime.withZoneSameInstant and ZonedDateTime.withZoneSameLocal?

前提是你 提交于 2021-02-18 05:49:32
问题 Let's say I have a ZonedDateTime: ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("US/Pacific")); I would like to know which date/time it is let's say in Berlin. I have two methods : zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/Berlin")); // probably this is the right one to get the corresponding date/time in Berlin zonedDateTime.withZoneSameLocal(ZoneId.of("Europe/Berlin")); The docs for the withZoneSameLocal method say: "The local date-time is only

java.time DateTimeFormatter pattern for timezone offset

余生长醉 提交于 2021-02-16 04:19:49
问题 I am trying to parse: 2014-05-02-10.45.05.993280-5:00 where the -5:00 is the offset from UTC. Using a java.time DateTimeFormatter in Java 8. For the first bit I have the following: yyyy-MM-dd-HH.mm.ss.SSSSSS however, I can't figure out what the pattern should be to parse the offset also. If I had the offset with 4 digits (-05:00) I could use: yyyy-MM-dd-HH.mm.ss.SSSSSSxxx , but this doesn't work for 3 digits. Any ideas? 回答1: Use capital letter X instead of x, hence XXX. The difference is that

Kotlin DateTimeParseException

此生再无相见时 提交于 2021-02-11 12:32:24
问题 Getting date from https://api.spacexdata.com/v3/launches This date have format: 2006-03-25T10:30:00+12:00. I want convert it to "dd, mm, yyyy", but always getting error: "java.time.format.DateTimeParseException: Text '2006-03-25T10:30:00+12:00' could not be parsed, unparsed text found at index 10" my code: val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US) val myDate = LocalDate.parse(launchDate, formatter) var launchDateConverted: String= myDate.toString() i getting data

Migrating to Java 8 DateTime [duplicate]

我怕爱的太早我们不能终老 提交于 2021-02-10 20:01:38
问题 This question already has answers here : How to get start and end range from list of timestamps? (2 answers) Date year value showing valid when invalid [duplicate] (3 answers) Closed 2 years ago . I'm in the process of changing our existing SimpleDateFormat based code to use the new Java 8 LocalDateTime and ZonedDateTime classes. I couldn't find an easy way to convert this piece. Considering sample date and format. String testDate = "2012-12-31T10:10:10-06:00"; String testFormat = "yyyy-MM-dd

Migrating to Java 8 DateTime [duplicate]

岁酱吖の 提交于 2021-02-10 19:59:53
问题 This question already has answers here : How to get start and end range from list of timestamps? (2 answers) Date year value showing valid when invalid [duplicate] (3 answers) Closed 2 years ago . I'm in the process of changing our existing SimpleDateFormat based code to use the new Java 8 LocalDateTime and ZonedDateTime classes. I couldn't find an easy way to convert this piece. Considering sample date and format. String testDate = "2012-12-31T10:10:10-06:00"; String testFormat = "yyyy-MM-dd

DateTimeParseException - could not be parsed at index 0

别说谁变了你拦得住时间么 提交于 2021-02-09 09:25:57
问题 I'm trying to parse the following date "Wed, 26 Feb 2020 03:42:25 -0800" String datePattern = "EEE, dd MMM yyyy HH:mm:ss Z"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern); ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateStr, formatter); But i get the following error: java.time.format.DateTimeParseException: Text 'Wed, 26 Feb 2020 03:42:25 -0800' could not be parsed at index 0 Thanks 回答1: You need to provide a Locale that uses the language used by the date String