Используя Hamcrest AssertThatme LocalDateTime, я получаю дату в формате AM/PM. Что нужно сделать, чтобы получить вывод в формате 24 часа?LocalDateTime actualJobExecutionTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
.....
LocalDateTime expectedDateTime = LocalDateTime.now();
assertThat(actualJobExecutionTime, LocalDateTimeMatchers.after(expectedDateTime));
< /code>
output: < /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>
Нашел причину этого в 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>
Итак, мое предварительное (и, возможно, окончательное) решение - написать /переписать 2 дополнительные классы: < /p>
customhamprestdatematcher: < /li>
< /ol>
import 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 (с новым/конкретным форматом DateTime):
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);
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... assertthat
Как изменить формат времени вывода в AssertThat ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Вход в систему веб -сайта с помощью вывода и сохранения вывода и сохранения вывода и сеанса?
Anonymous » » в форуме Php - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-