Skip to content

Tag: java-time

Unable to parse date which is missing minutes?

I am trying to parse a date-time string that does not contain minutes 2019-10-12T07Z. When I run the above code, it throws the following exception stack trace Expected output Any idea what I should do? Answer You have to create custom DateTimeFormatter representing the time and offset. See that code run at 2019-10-12T07:00Z If you want to display the

Convert time from specific timezone to UTC (ColdFusion/Java)

We need to take a datetime string (which we’ve already received and validated from user form inputs), treat it as being specific to the America/New_York timezone, and convert it to UTC. We’re currently running ColdFusion 2018, which is limited in its native datetime functionality. So I wanted to tap into the underlying Java. Most CF-related solutions are pre-java.time, so I

Why LocalDateTime formatted with zone offset?

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, which is documented as (emphasis mine): Obtains the current date-time from the system clock in the default time-zone. This will query the

why nano seconds have to use int instead of long in java

I have been doing this exercise and this is the code Doesnt nanoseconds have to use long instead of int because the nanoseconds in the range? Answer That’s because like documentation says, we have a duration which consist of two fields, one is seconds and the other one is nanos. So when you ask for duration between, you get 2

How to parse DateTime with Time between the dates?

I want to parse a date-time variant that has the time in between the dates. Why is the following not working? Result: java.time.format.DateTimeParseException: Text ‘Tue Mar 1 01:29:47 2022’ could not be parsed at index 4 Answer I think you have to make sure that you provide a specific Locale for abbreviated months and days of week you use the

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

Parse datetime with offset string to LocalDateTime

I am trying to parse following datetime String to LocalDateTimeObject, however I am not able to identify the format of the datetime string. Sat, 09 Oct 2021 02:10:23 -0400 How should I determine the pattern of the above string? Answer You should first check if the date string matches any of the Predefined Formatters. If not, then you have to