首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以日期格式Java将Wed Dec 26 11:11:59转换为26/12/2022

如何以日期格式Java将Wed Dec 26 11:11:59转换为26/12/2022
EN

Stack Overflow用户
提问于 2022-12-02 04:50:03
回答 1查看 64关注 0票数 0

我有一个从角前端到Java后端的日期传递。

我从前端收到的日期格式是: Mon 26 11:11:59 SGT 2022,并且是在Java日期对象中。

如何将此格式转换为dd/MM/yyyy,最后的输出应该是26/12/2022 Java格式的.

当前我的代码如下所示:

代码语言:javascript
复制
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

分析我得到的异常:

代码语言:javascript
复制
java.text.ParseException: Unparseable date: "Mon Dec 26 12:43:19 SGT 2022"
EN

回答 1

Stack Overflow用户

发布于 2022-12-02 06:42:57

首先,您需要将String转换为LocalDate。然后应用所需的格式。

代码语言:javascript
复制
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));
    }
}

输出

代码语言:javascript
复制
26/12/2022
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74651030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档