gpt4 book ai didi

java - 字符串 (dd-MM-yyyy HH :mm) to Date (yyyy-MM-dd HH:mm) | Java

转载 作者:搜寻专家 更新时间:2023-11-01 01:00:02 26 4
gpt4 key购买 nike

我在“dd-MM-yyyy HH:mm”中有一个字符串,需要将其转换为格式为日期的对象“yyyy-MM-dd HH:mm”。

下面是我用来转换的代码

oldScheduledDate = "16-05-2011 02:00:00";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = (Date)formatter.parse(oldScheduledDate);

现在当我打印 oldDate 时,我得到了

Sat Nov 01 02:00:00 GMT 21,这是完全错误的,我在这里做错了什么?

最佳答案

    String dateSample = "10-01-2010 21:10:05";

String oldFormat = "dd-MM-yyyy HH:mm:ss";
String newFormat = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);


try {
System.out.println(sdf2.format(sdf1.parse(dateSample)));

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

关于java - 字符串 (dd-MM-yyyy HH :mm) to Date (yyyy-MM-dd HH:mm) | Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4511946/

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