2

I am getting an Assertion error " Body Content Expected child but was null when asserting the andExpect XML. If I input as as a String "2020-10-01-5:00" it works fine but if I concatenate the date into a string like:

    LocalDate startDate = LocalDate.now().minusDays(90);
    String startDateLine =  "<start-date>" + startDate + "-5:00</start-date>\n";

It throws the AssertionError. I have verified that the XML is correct before the call so I am unsure what about getting the date and converting to a string causes the test to fail.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
h418982
  • 23
  • 2

1 Answers1

2

Update

Do not add the offset string to the LocalDate string in order to convert it into an OffsetDateTime string. Shown below is the idiomatic way to convert a LocalDate to OffsetDateTime

LocalDate.of(2020, 10, 1)
         .atStartOfDay()
         .atOffset(ZoneOffset.of("-05:00"));

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2020, 10, 1);
        LocalDateTime ldt = date.atStartOfDay();
        OffsetDateTime odt = ldt.atOffset(ZoneOffset.of("-05:00"));
        System.out.println(odt);
    }
}

Output:

2020-10-01T00:00-05:00

ONLINE DEMO

You can get the String representation of an OffsetDateTime using the function OffsetDateTime#toString e.g.

String strOdt = odt.toString();

Original answer

  1. Change your input to have the timezone offset in the format HH:mm e.g. -05:00 so that it conforms to ISO 8601 standards.
  2. Use DateTimeFormatterBuilder with .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) to default the hour-of-day to 0.
  3. Parse the given string to OffsetDateTime as it has timezone offset and OffsetDateTime is the best fit to represent Date-Time with timezone offset.

Demo:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf =new DateTimeFormatterBuilder()
                                .appendPattern("u-M-d[H:m:s]XXX")
                                .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                                .toFormatter(Locale.ENGLISH);
        
        OffsetDateTime odt = OffsetDateTime.parse("2020-10-01-05:00", dtf);
        System.out.println(odt);
    }
}

Output:

2020-10-01T00:00-05:00

ONLINE DEMO

Notice the optional pattern inside a square bracket.

Learn more about the modern Date-Time API* from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    Good answer. It may just be me being lazy, if I don’t already have an offset string, I tend to use `ZoneOffset.ofHours(-5)`. One may argue that your `ZoneOffset.of("-05:00")` is easier to read. Perhaps depending on the reader’s background. – Ole V.V. Aug 14 '21 at 13:03