Consider code: The output is 2022-05-04-17 while UTC time is 2022-05-04-10. The documentation says that LocalDateTime is without zone offset – so why there is 7 hour shift? (My local time zone is +7UTC) Answer You’ve called LocalDateTime.now(), which is documented as (emphasis mine): Obtains the current date-time from the system clock in the default time-zone. This will query the
Tag: java-time
LocalDate.parse can’t parse the character on the last index wehere there is no character (nor empty space)
I have in my notepad file text: I checked is there any empty spaces, there is none. When I write: I receive an error: But there is no index 10! Why it thinks there is index[10]? Answer Trim possible whitespaces before parsing the date using String::trim:
Java Instant.parse cannot validate date
I have an instant field and trying to validate the values in this field using the following approach: Instant.parse(“2021-09-29 09:35:07.531”) However, it throws the error: java.time.format.DateTimeParseException: Text ‘2021-09-29 09:35:07.531’ could not be parsed at index 10. So, how can I test if the given Instant date in String format is a valid Instant date? I am implementing an Import feature
Create a DateTimeFormater with an Optional Section at Beginning
I have timecodes with this structure hh:mm:ss.SSS for which i have a own Class, implementing the Temporal Interface. It has the custom Field TimecodeHour Field allowing values greater than 23 for hour. I want to parse with DateTimeFormatter. The hour value is optional (can be omitted, and hours can be greater than 24); as RegEx (d*dd:)?dd:dd.ddd For the purpose of
Manipulating and comparing dates with GregorianCalendar and can’t get the code to work properly
I’m struggling to find the error in my code here. paytotal is coming out 0 when it should have a number. firstDayOfPaycheck is the date Oct. 23rd 2020. lastDayOfPaycheck is the date Nov. 6 2020. My …
Error: Text ‘1/31/2020’ could not be parsed at index 0 while trying to parse string date
I have a date as string as below String test Date = “1/31/2020”; I am using the below code public static String getPeriodMonth(String periodEndDate) { LocalDate localDate; …
DateTimeFormatter – Strict vs Lenient unexpected behaviour
I have some text that is only being parsed by a DateTimeFormatter when the parse style is Strict – and not when it’s Lenient. This seems like the opposite behaviour to what I’d expect? Example: Output: Answer Having posted this as a bug – https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8228353 I got the following reply: According to DateTimeFormatterBuilder’s spec, appendPattern(“yy”) translates to appendValueReduced(ChronoField.YEAR_OF_ERA, 2, 2000),
What’s the difference between ZonedDateTime and OffsetDateTime?
I’ve read the documentation, but I still can’t get when I should use one or the other: OffsetDateTime ZonedDateTime According to documentation OffsetDateTime should be used when writing date to database, but I don’t get why. Answer Q: What’s the difference between java 8 ZonedDateTime and OffsetDateTime? The javadocs say this: “OffsetDateTime, ZonedDateTime and Instant all store an instant on
Unit testing a class with a Java 8 Clock
Java 8 introduced java.time.Clock which can be used as an argument to many other java.time objects, allowing you to inject a real or fake clock into them. For example, I know you can create a Clock….
Convert java.util.Date to java.time.LocalDate
What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate? Date input = new Date(); LocalDate date = ???