gpt4 book ai didi

java - 如何使用日期格式将 "24-Jun-2012"更改为 "24-06-2012"

转载 作者:行者123 更新时间:2023-11-29 09:48:41 26 4
gpt4 key购买 nike

我正在尝试将“12-Jun-2013”​​日期转换为“12-06-2013”​​,但它给了我异常(exception)。

String req="12-Jun-2013"

SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MMM/yyyy",Locale.UK);
try {
Date date = (Date) sdfSource.parse(req);
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy",Locale.UK);
req = sdfDestination.format(date);
System.out.println("final object"+req);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

我遇到异常

java.text.ParseException: Unparseable date: "12-Jun-2013"

最佳答案

你的来源 SimpleDateFormat 应该是

SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MMM-yy",
Locale.UK);

你的目标 SimpleDateFormat 应该是

SimpleDateFormat sdfDestination = new SimpleDateFormat(
"dd-MM-yyyy", Locale.UK);

关于java - 如何使用日期格式将 "24-Jun-2012"更改为 "24-06-2012",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17268714/

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