gpt4 book ai didi

java - 字符串到日期格式

转载 作者:行者123 更新时间:2023-12-01 07:09:40 25 4
gpt4 key购买 nike

我在将String 转换为Date 格式时遇到问题。请帮我。下面是我的代码:

String strDate = "23/05/2012"; // Here the format of date is MM/dd/yyyy

现在我想将上面的字符串转换为日期格式,例如“2012年5月23日”。

我正在使用下面的代码,但我得到的值是“Wed May 23 00:00:00 BOT 2012

String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Wed May 23 00:00:00 BOT 2012

我如何获得“2012 年 5 月 23 日”的值。请 friend 们帮助我......

最佳答案

您必须再次呈现日期。

您拥有该字符串,并且可以将其正确解析回 Date 对象。现在,您必须按照您想要的方式呈现 Date 对象。

您可以再次使用SimpleDateFormat,更改模式。您的代码应该类似于

String string = "23/05/2012";
Date date = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(string);
String newFormat = new SimpleDateFormat("dd MMMM, yyyy").format(date);
System.out.println(newFormat); // 23 May, 2012

关于java - 字符串到日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16960176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com