java-time

Failed to parse single digit hour and lowercase am-pm of day into Java 8 LocalTime

故事扮演 提交于 2019-11-30 21:08:36
When I try to run the following code: LocalTime test = LocalTime.parse("8:00am", DateTimeFormatter.ofPattern("hh:mma")); I get this: Exception in thread "main" java.time.format.DateTimeParseException: Text '8:00am' could not be parsed at index 0 Any idea why this might be happening? The AM/PM tokens have* to be uppercase: LocalTime test = LocalTime.parse("8:00AM", DateTimeFormatter.ofPattern("hh:mma")); Patterns definitions for hours: Symbol Meaning Presentation Examples ------ ------------ ------------ -------- a am-pm-of-day text PM h clock-hour-of-am-pm (1-12) number 12 K hour-of-am-pm (0

Why my pattern(“yyyyMM”) cannot parse with DateTimeFormatter (java 8)

二次信任 提交于 2019-11-30 20:42:14
When I using SimpleDateFormat , it can parse. SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); format.setLenient(false); Date d = format.parse(date); But When I use Java 8 DateTimeFormatter , DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM"); LocalDate localDate = LocalDate.parse(date, formatter); it throws java.time.format.DateTimeParseException: Text '201510' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2015, MonthOfYear=10},ISO of type java .time.format.Parsed String value for date is "201510" . Ask yourself the question: which day

Why does converting Java Dates before 1582 to LocalDate with Instant give a different date?

二次信任 提交于 2019-11-30 20:15:34
Consider this code: Date date = new SimpleDateFormat("MMddyyyy").parse("01011500"); LocalDate localDateRight = LocalDate.parse(formatter.format(date), dateFormatter); LocalDate localDateWrong = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(); System.out.println(date); // Wed Jan 01 00:00:00 EST 1500 System.out.println(localDateRight); // 1500-01-01 System.out.println(localDateWrong); // 1500-01-10 I know that 1582 is the cutoff between the Julian and Gregorian calendars. What I don't know is why this happens, or how to adjust for it. Here's what I've figured out

Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10

时光总嘲笑我的痴心妄想 提交于 2019-11-30 19:28:38
I´m trying to pase the next String using LocalDateTime, but I always get de unparsed text found error: Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10 Here is my String: convertDate: ' 2016-08-18 14:27:15.103+02 ' And my code: public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException { LocalDate dateTime =LocalDate.parse(convertDate); return dateTime; } I guess is not too complicated, buy I´m not able to see the error. Could the +02 in the String be the cause? Basil

between java.time.LocalTime (next day)

蹲街弑〆低调 提交于 2019-11-30 18:54:58
Please suggest if there is an API support to determine if my time is between 2 LocalTime instances, or suggest a different approach. I have this entity: class Place { LocalTime startDay; LocalTime endDay; } which stores the working day start and end time, i.e. from '9:00' till '17:00', or a nightclub from '22:00' till "5:00". I need to implement a Place.isOpen() method that determines if the place is open at a given time. A simple isBefore / isAfter does not work here, because we also need to determine if the end time is on the next day. Of course, we can compare the start and end times and

Parse ISO timestamp using Java 8 java.time api (standard edition only)

橙三吉。 提交于 2019-11-30 17:41:07
I'm having trouble getting milliseconds from the epoch out of the string in the example. I have tried this three different ways so far, and the example shows the latest attempt. It always seems to come down to that the TemporalAccessor does not support ChronoField . If I could successfully construct an instance of Instant, I could use toEpochMilli() . String dateStr = "2014-08-16T05:03:45-05:00" TemporalAccessor creationAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateStr); Instant creationDate = Instant.from(creationAccessor); Please give concise answers (don't construct a

Java 8 DateTimeFormatter parsing for optional fractional seconds of varying significance

狂风中的少年 提交于 2019-11-30 17:15:20
My MCVE (as a TestNG unit test): public class MyDateTimeFormatterTest { private static final String BASE_PATTERN = "yyyy/MM/dd HH:mm:ss"; private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(BASE_PATTERN + "[.SSSSSSSSS]"); private static final LocalDateTime TEST_INPUT = LocalDateTime.of(2015, 5, 4, 12, 34, 56, 123456789); @DataProvider(name = "test-cases") public Iterator<Object[]> getTestCases() { return Arrays.asList(testFor("", ChronoUnit.SECONDS), testFor(".SSS", ChronoUnit.MILLIS), testFor(".SSSSSS", ChronoUnit.MICROS), testFor(".SSSSSSSSS", ChronoUnit.NANOS))

Java doesn't have information about all IANA time zones

限于喜欢 提交于 2019-11-30 15:29:42
问题 I'm trying to map the values that come from the Front-End to ZoneId class like this: Optional.ofNullable(timeZone).map(ZoneId::of).orElse(null) For most time zones it works fine, however, for some values Java throws exception: java.time.zone.ZoneRulesException: Unknown time-zone ID: America/Punta_Arenas However, it is a valid time-zone according to IANA: https://www.iana.org/time-zones Zone America/Punta_Arenas -4:43:40 - LMT 1890 I was thinking about using offset for such time-zones (just to

Java doesn't have information about all IANA time zones

依然范特西╮ 提交于 2019-11-30 14:13:32
I'm trying to map the values that come from the Front-End to ZoneId class like this: Optional.ofNullable(timeZone).map(ZoneId::of).orElse(null) For most time zones it works fine, however, for some values Java throws exception: java.time.zone.ZoneRulesException: Unknown time-zone ID: America/Punta_Arenas However, it is a valid time-zone according to IANA: https://www.iana.org/time-zones Zone America/Punta_Arenas -4:43:40 - LMT 1890 I was thinking about using offset for such time-zones (just to hardcode values), but I guess there should be more convenient way to solve the issue. Is there a way

How can I have JAX-RS return a Java 8 LocalDateTime property as a JavaScript-style Date String?

喜夏-厌秋 提交于 2019-11-30 13:22:25
I created a RESTful web service using JAX-RS method annotations: @GET @Path("/test") @Produces(MediaType.APPLICATION_JSON) public MyThing test() { MyThing myObject = new MyThing(LocalDateTime.now()); return myObject; } This works nicely, but I'd like to adjust one thing: If the returned Java object contains a property of the new Java 8 LocalDateTime type, it is represented as a JSON object: {"myDateTimeProperty":{"hour":14,"minute":32,"second":39,"year":2014,"month":"NOVEMBER","dayOfMonth":6,"dayOfWeek":"THURSDAY","dayOfYear":310,"monthValue":11,"nano":0,"chronology":{"calendarType":"iso8601",