So ändern Sie das Ausgangszeitformat in AssertThat

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: So ändern Sie das Ausgangszeitformat in AssertThat

by Anonymous » 03 Apr 2025, 08:14

Verwenden von Hamcrest Assertthat localDatetime, ich erhalte das Datum im AM/PM -Format. Was sollte getan werden, um die Ausgabe im 24-Stunden-Format zu erhalten?

Code: Select all

LocalDateTime actualJobExecutionTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
.....

LocalDateTime expectedDateTime = LocalDateTime.now();

assertThat(actualJobExecutionTime, LocalDateTimeMatchers.after(expectedDateTime));
< /code>
Ausgabe: < /p>
java.lang.AssertionError:
Expected: the date is after Mi., 02 Apr. 2025 08:28:16.482 AM
but: date is Mo., 31 März 2025 09:33:35.000 AM
< /code>
Grund dafür in localDatatimeFormatter.class: < /p>
package org.exparity.hamcrest.date.core.format;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.exparity.hamcrest.date.core.TemporalFormatter;

public class LocalDateTimeFormatter implements TemporalFormatter {
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy hh:mm:ss.SSS a");
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy");

public LocalDateTimeFormatter() {
}

public String describe(LocalDateTime temporal) {
return temporal.format(DATE_TIME_FORMAT);
}

public String describeDate(LocalDateTime temporal) {
return temporal.format(DATE_FORMAT);
}
}
< /code>
meine vorläufige (und möglicherweise endgültige) Lösung besteht darin, 2 zusätzliche Klassen zu schreiben /neu zu schreibenimport org.exparity.hamcrest.date.core.DateMatcher;
import org.exparity.hamcrest.date.core.IsAfter;
import org.exparity.hamcrest.date.core.wrapper.LocalDateTimeWrapper;

import java.time.LocalDateTime;

public abstract class CustomHamcrestDateMatcher {
public CustomHamcrestDateMatcher() {
}

public static DateMatcher after(LocalDateTime date) {
return new IsAfter(new LocalDateTimeWrapper(date), new CustomLocalDateTimeFormatter());
}
}

2) CustomLocalDatetimeFormatter (mit neuem/spezifischem DateTime -Format):

Code: Select all

import org.exparity.hamcrest.date.core.TemporalFormatter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CustomLocalDateTimeFormatter implements TemporalFormatter {
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss");
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy");

public CustomLocalDateTimeFormatter() {
}

public String describe(LocalDateTime temporal) {
return temporal.format(DATE_TIME_FORMAT);
}

public String describeDate(LocalDateTime temporal) {
return temporal.format(DATE_FORMAT);
}
}


Top