更改字符串日期格式 [英] Changing String date format

查看:40
本文介绍了更改字符串日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的日期格式是: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(当使用 yyyy/MM/dd HH:mm) 使用 08 表示年份,11 表示月份,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆