Home > Software engineering >  Converting Joda DateTime to JavaTime
Converting Joda DateTime to JavaTime

Time:01-04

I am trying to update my existing elasticsearch springboot project and as the source code is fairly old it still uses joda time. Now I have to upgrade all the functions of Joda time to java time. Now in the project We use Date Time of Joda Time

Code Sample for Joda Time

DateTime target = new DateTime(String targetDate, UTC);

We use this function currently in our code to convert a String to Date. Using this function the String

2022-10-01T00:00:00.000

gets converted to

2022-10-01T00:00:00.000Z

I am trying to replicate the same in java time.

I tried to parse the targetDate using OffsetDateTime and ZonedDateTime but both gave me errors.

Text '2022-10-01T00:00:00.0000' could not be parsed at index 24

After some attempts I was able to move forward by using LocalDateTime

LocalDateTime target = LocalDateTime.parse(String targetDate);

Which was able to parse the String but the format was not correct the format I got was

2022-10-01T00:00Z

I also tried using the formatter with LocalDateTime

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'");
LocalDateTime target= LocalDateTime.parse(targetDate,formatter);

But This Still gave me the Error

Text '2022-10-01T00:00:00.0000' could not be parsed at index 24

Now I am a bit confused regarding this.

Any help is appreciated. And Please correct me if my way of asking question or formatting is wrong at any point still new to this.

Regards.

EDIT: Sorry for the confusion but as pointed out I should have mentioned that I want the returned value as the java.time datetime object and not a String so that I can further perform some logic on it. Sorry for this.

Thanks and Regards

CodePudding user response:

The String "2022-10-01T00:00:00.000" can be parsed to a LocalDateTime because it only consists of year, month of year, day of month, hour of day, minute of hour, second of minute and fractions of second.

Your desired output String "2022-10-01T00:00:00.000Z" represents the same values plus an offset, the Z for Zulu time, which basically means UTC.

If you want to add an offset to the input String with java.time, you can parse it to a LocalDateTime and then append the desired offset, which results in an OffsetDateTime. You can print that in a desired format using a DateTimeFormatter, either use a prebuilt one or define one yourself.

Here's a small example:

public static void main(String[] args) {
    // input value
    String dateTime = "2022-10-01T00:00:00.000";
    // parse it and append an offset
    OffsetDateTime odt = LocalDateTime.parse(dateTime).atOffset(ZoneOffset.UTC);
    // define a formatter that formats as desired
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
    // and print the OffsetDateTime using that formatter
    System.out.println(odt.format(dtf));
}

Output:

2022-10-01T00:00:00.000Z

Clarification update:

There is just a single instance OffsetDateTime in my example with the following values:

  • year (2022)
  • month of year (10)
  • day of month (1)
  • hour of day (0)
  • minute of hour (0)
  • second of minute (0)
  • franctions of second (0)
  • offset (UTC / 00:00)

This instance of OffsetDateTime can be used for calculations (e.g. add/subtract days, months or other units) and it can be formatted as String. It also has a toString() method we don't have under control, but is used if you don't explicitly format it.

The following lines (first one is the last of my example above) show some different usages:

// print formatted by the DateTimeFormatter from the above example
System.out.println(odt.format(dtf));
// print the object directly, implicitly using its toString()
System.out.println(odt);
// print formatted by a prebuilt DateTimeFormatter (several are available)
System.out.println(odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
// print with a formatter that uses locale dependant expressions like month names
System.out.println(odt.format(
                DateTimeFormatter.ofPattern("EEEE, MMM dd HH:mm:ss xxx",
                                            Locale.ENGLISH)));

the last one also uses a different representation for UTC: instead of Z it shows the offset in hours and minutes.

Output:

2022-10-01T00:00:00.000Z
2022-10-01T00:00Z
2022-10-01T00:00:00Z
Saturday, Oct 01 00:00:00  00:00
  • Related