我可以知道如何将一种日期格式转换为另一种日期格式。
public static LocalDate localDateToAnotherLocalDate(String oldPattern, String newPattern, String input) {
DateTimeFormatter oldFormat = DateTimeFormatter.ofPattern(oldPattern);
DateTimeFormatter newFormat = DateTimeFormatter.ofPattern(newPattern);
LocalDate localDate = LocalDate.parse(input, oldFormat);
String output = localDate.format(newFormat);
System.out.println();
return getLocalDate(output, newPattern);
}
public static LocalDate getLocalDate(String date, String datePattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern);
return LocalDate.parse(date, formatter);
}
@Test
void getLocalDateToAnotherLocalDateTest() {
LocalDate localDate = DateUtil.localDateToAnotherLocalDate("yyyy-MM-dd", "dd-MM-yyyy", "2022-11-20");
System.out.println("localDate" + localDate.toString());
assertEquals("20-11-2022", localDate.toString());
}
1条答案
按热度按时间nlejzf6q1#
LocalDate对象只能以ISO 8601格式(yyyy-MM-dd)打印。若要以其他格式打印对象,您必须格式化它并将LocalDate储存为字串。