我有一个从角前端到Java后端的日期传递。
我从前端收到的日期格式是: Mon 26 11:11:59 SGT 2022,并且是在Java日期对象中。
如何将此格式转换为dd/MM/yyyy,最后的输出应该是26/12/2022 Java格式的.
当前我的代码如下所示:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date formattedDate = dateFormat.parse(sdf.format(dateFromFrontend));
===> formattedDate to save to DB分析我得到的异常:
java.text.ParseException: Unparseable date: "Mon Dec 26 12:43:19 SGT 2022"发布于 2022-12-02 06:42:57
首先,您需要将String转换为LocalDate。然后应用所需的格式。
import java.util.Locale;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
String raw = "Mon Dec 26 11:11:59 SGT 2022";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
// Your required format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
LocalDate dateTime = LocalDate.parse(raw, dtf);
System.out.println(formatter.format(dateTime));
}
}输出
26/12/2022https://stackoverflow.com/questions/74651030
复制相似问题