gpt4 book ai didi

java - 如何转换日期格式

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

谁能帮忙转换日期格式吗?

我返回的日期对象包含“Mon Jul 12 00:00:00 IST 2010”

我正在尝试将此日期格式转换为 "MM/dd/yyyy" 但出现解析异常。请帮我转换一下

<小时/>

OP评论中的代码:

String mydatObj = myDate.toString(); 
Date formatedDate = getDateFormat(mydatObj);
public static Date getDateFormat(String dateString) {
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
try {
// set isLenient to false to adhere to the date format.
format.setLenient(false);
date = format.parse(dateString);
} catch (ParseException parseException) {
// ignore
LOG.error(parseException.getMessage(), parseException);
}
return date;
}

最佳答案

好吧,由于您尝试解析格式错误的日期,因此您收到了 ParseException

这是一个小代码片段,适用于您拥有的格式:

// parse the date
DateFormat f = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
Date d = f.parse("Mon Jul 12 00:00:00 IST 2010"); // works

// now print the date
DateFormat out = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(out.format(d));

关于java - 如何转换日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3235240/

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