更改字符串日期格式 [英] Changing String date format
问题描述
我目前的日期格式为:
08/11/2008 00:00
my current date format is : 08/11/2008 00:00
我需要将此输出转换为
2008/11 / 08 00:00
然而,使用SimpleDateFormat进行研究后它无法这样做并给我一个完全不同的输出,这里是我的代码如下:
I need to convert this output to 2008/11/08 00:00 However, using the SimpleDateFormat as researched it is unable to do so and give me a totally different output, here are my codes as Follows :
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
Date starting= simpleDateFormat2.parse(startTime);
System.out.println("" + simpleDateFormat2.format(starting) + " real date " + startTime);
我知道我正在解析正确的字符串,因为发生了以下输出:
i do know that i am parsing in the right string given that the following output occurs :
0014/05/01 00:00 real date 08/11/2008 00:00
我不太确定机械师如何检测到0014/05/01 00:00而不是
i am not too sure about how as to the mechanics detected 0014/05/01 00:00 instead of
2008/11/08 00:00
2008/11/08 00:00
我期待所有的建议
提前付款
i look forward to all sugguestions Thanks in advance
推荐答案
您需要做的第一件事是将原始值解析为 Date
对象
The first thing you need to do is parse the original value to a Date
object
String startTime = "08/11/2008 00:00";
// This could be MM/dd/yyyy, you original value is ambiguous
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date dateValue = input.parse(startTime);
完成后,您可以格式化 dateValue
你想要的任何方式......
Once you have that done, you can format the dateValue
any way you want...
SimpleDateFormat output = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println("" + output.format(dateValue) + " real date " + startTime);
输出:
2008/11/08 00:00 real date 08/11/2008 00:00
您获得 0014/05/01 00:00
的原因是 SimpleDateFormat
(使用<$时) c $ c> yyyy / MM / dd HH:mm )年内使用 08
, 11 $当天的c $ c>和当天的
2008
,它正在内部滚动值以将值更正为有效日期
The reason you're getting 0014/05/01 00:00
is SimpleDateFormat
(when using yyyy/MM/dd HH:mm
) is using 08
for the year, 11
for the month and 2008
for the day, it's doing an internal rolling of the values to correct the values to a valid date
这篇关于更改字符串日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!