gpt4 book ai didi

java : How can I cast Date from "Thu May 01 00:00:00 WEST 2014 " to "2014-01-05 00:00:00.0"

转载 作者:行者123 更新时间:2023-12-02 05:47:23 24 4
gpt4 key购买 nike

我如何才能将日期从“Thu May 01 00:00:00 WEST 2014”转换到“2014-01-05 00:00:00.0”

我尝试使用这段代码:

SimpleDateFormat toFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
String dateStr = getDtdebut().toString();
Date date = new Date();
try {
date = toFormat.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

但我有这个错误:

java.text.ParseException: Unparseable date: "Thu May 01 00:00:00 WEST 2014"
at java.text.DateFormat.parse(Unknown Source)
at ensa.pfe.action.GestionOperations.filtre(GestionOperations.java:386)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
..........

最佳答案

java.util.Date 只是时间戳值(即自 UNIX 纪元以来的毫秒数)的包装类。该对象与任何特定的日期格式甚至日历系统都没有任何联系。

您需要的只是格式化日期,而不是使用某些默认格式化程序将其转换为字符串,然后再次将其解析回格式独立对象。使用DateFormat#format .

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
System.out.println(format.format(getDtdebut()));

关于java : How can I cast Date from "Thu May 01 00:00:00 WEST 2014 " to "2014-01-05 00:00:00.0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929906/

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