java-time

DateTimeParseException - could not be parsed at index 0

邮差的信 提交于 2021-02-09 09:20:44
问题 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

DateTimeParseException on Java 11 but works on Java 10

拥有回忆 提交于 2021-02-09 02:55:32
问题 The following testcase runs perfectly under Java 10: import java.time.Instant; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; class Test { public static void main (String[] args) throws java.lang.Exception { DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder(). appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz"). toFormatter(); Instant result = dateFormatter.parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from); System.out.println("Result:

DateTimeParseException on Java 11 but works on Java 10

匆匆过客 提交于 2021-02-09 02:50:50
问题 The following testcase runs perfectly under Java 10: import java.time.Instant; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; class Test { public static void main (String[] args) throws java.lang.Exception { DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder(). appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz"). toFormatter(); Instant result = dateFormatter.parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from); System.out.println("Result:

DateTimeParseException on Java 11 but works on Java 10

非 Y 不嫁゛ 提交于 2021-02-09 02:50:10
问题 The following testcase runs perfectly under Java 10: import java.time.Instant; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; class Test { public static void main (String[] args) throws java.lang.Exception { DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder(). appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz"). toFormatter(); Instant result = dateFormatter.parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from); System.out.println("Result:

DateTimeParseException on Java 11 but works on Java 10

£可爱£侵袭症+ 提交于 2021-02-09 02:50:09
问题 The following testcase runs perfectly under Java 10: import java.time.Instant; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; class Test { public static void main (String[] args) throws java.lang.Exception { DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder(). appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz"). toFormatter(); Instant result = dateFormatter.parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from); System.out.println("Result:

Convert ms into a string date with Java 8

Deadly 提交于 2021-02-08 05:25:34
问题 I have a timestamp as ms and format this with a SimpleDateFormater like this: SimpleDateFormat sdfDate = new SimpleDateFormat("MM/d/yyyy h:mm a"); return sdfDate.format(new Date(timeStamp)); Now I'd like to change this to use the new Java 8 abilities. Seems like I really can't find a way to get this working with the new Java 8 Classes. Anyone got a hint? 回答1: If all you have is a millisecond value (assuming from the Unix Epoch) you can get an LocalDateTime using something like: LocalDateTime

“no suitable method found for between(Date, Date)" when trying to calculate difference in days between two dates

99封情书 提交于 2021-02-05 12:32:15
问题 How to calculate the difference between current day and date of the object that user had previously selected from jXDatePicker swing component and that had been added as Date to that object. In my current code at the last line I'm getting this error message: no suitable method found for between(Date, Date) Date currentDate = new Date(); Date objDate = obj.getSelectedDate(); //getting date the user had //previously selected and that been //added to object long daysDifference = ChronoUnit.DAYS

Java 8 LocalDate.plusMonths is adding to days and months

喜夏-厌秋 提交于 2021-02-05 11:39:21
问题 I'm getting strange formatting issue when adding months to a LocalDate. Here is the Scala code and output: val virtualToday: LocalDate = LocalDate.parse("2015-01-01") val eightDaysFromToday: LocalDate = virtualToday.plusDays(8) val sixMonthsFromToday: LocalDate = virtualToday.plusMonths(6) println("virtualToday " + virtualToday) println("eightDaysFromToday " + eightDaysFromToday) println("sixMonthsFromToday " + sixMonthsFromToday) println( "virtualToday with formatting " + virtualToday

How can I represent and operate on time values greater than 24:00 in Java?

拜拜、爱过 提交于 2021-02-05 05:36:10
问题 I'm currently doing some work in an application domain that uses time values greater than 24:00 to represent times after midnight that are still associated with the previous day's details. For instance, it might use 25:15 on Monday to represent 1:15 AM on Tuesday, since from a domain standpoint, this value is still associated with Monday's data. This type of time usage is briefly mentioned in the Wikipedia article for the 24-hour clock: Time-of-day notations beyond 24:00 (such as 24:01 or 25

DateTimeParse Exception

妖精的绣舞 提交于 2021-02-04 08:42:11
问题 I am constantly getting an exception error in my code when parsing a date. The Date looks like this: Wed May 21 00:00:00 EDT 2008 This is the code for trying to read it: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"); Property property = new Property(); property.setSale_date(LocalDateTime.parse(linesplit[8], formatter)); Expected result: A LocalDateTime of 2008-05-21T00:00 . What I got instead: Exception in thread "main" java.time.format