java 更改字符串日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34846358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:27:22  来源:igfitidea点击:

Changing String date format

javadatesimpledateformat

提问by Clement Ong

my current date format is : 08/11/2008 00:00

我当前的日期格式是:08/11/2008 00:00

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 :

我需要将此输出转换为 2008/11/08 00:00 但是,使用研究的 SimpleDateFormat 无法这样做并给我一个完全不同的输出,这是我的代码如下:

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

i am not too sure about how as to the mechanics detected 0014/05/01 00:00 instead of

我不太确定如何检测到机械师 0014/05/01 00:00 而不是

2008/11/08 00:00

2008/11/08 00:00

i look forward to all sugguestions Thanks in advance

我期待所有的建议 提前致谢

回答by MadProgrammer

The first thing you need to do is parse the original value to a Dateobject

您需要做的第一件事是将原始值解析为一个Date对象

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);

Once you have that done, you can format the dateValueany way you want...

完成后,您可以dateValue按照您想要的任何方式格式化...

SimpleDateFormat output = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println("" + output.format(dateValue) + " real date " + startTime);

which outputs:

输出:

2008/11/08 00:00 real date 08/11/2008 00:00

The reason you're getting 0014/05/01 00:00is SimpleDateFormat(when using yyyy/MM/dd HH:mm) is using 08for the year, 11for the month and 2008for the day, it's doing an internal rolling of the values to correct the values to a valid date

你得到的原因0014/05/01 00:00SimpleDateFormat(当使用时yyyy/MM/dd HH:mm08用于年、11月和2008日,它正在对值进行内部滚动以将值更正为有效日期

回答by Ole V.V.

I should like to contribute the modern answer.

我想贡献现代答案。

    DateTimeFormatter currentFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm");
    DateTimeFormatter convertedOutputFormatter = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm");
    String startTime = "08/11/2008 00:00";
    LocalDateTime starting = LocalDateTime.parse(startTime, currentFormatter);
    System.out.println(starting.format(convertedOutputFormatter));

This prints

这打印

2008/11/08 00:00

The SimpleDateFormatclass you tried to use is long outdated and notoriously troublesome. You have experienced but a little of the trouble with it. Instead I am using and recommending java.time, the modern Java date and time API.

SimpleDateFormat您尝试使用的课程已经过时并且出了名的麻烦。你已经经历了一些麻烦。相反,我正在使用和推荐java.time现代 Java 日期和时间 API。

As others have said, you need to use two formatters, one for specifying your current format and one for specifying the desired format.

正如其他人所说,您需要使用两种格式化程序,一种用于指定当前格式,另一种用于指定所需格式。

What went wrong in your code?

你的代码出了什么问题?

Your formatter with format pattern yyyy/MM/dd HH:mminterpreted 08/11/2008 as the 2008th day of the 11th month of year 8 of the common era. It doesn't disturb a SimpleDateFormatwith default settings that there are only 30 days in November, it just continues counting into the following months and years, ending up on a date five and a half years later (2008 divided by 365.25 is very close to 5.5).

您使用格式模式的格式化程序yyyy/MM/dd HH:mm将 08/11/2008 解释为共同纪元 8 年第 11 个月的第 2008 天。SimpleDateFormat11 月只有 30 天的默认设置不会打扰它,它只是继续计算接下来的几个月和几年,最终在五年半后的日期(2008 除以 365.25 非常接近 5.5 )。

Question: Can I use java.timewith my Java version?

问:我可以使用java.time我的 Java 版本吗?

If using at least Java 6, you can.

如果至少使用Java 6,则可以。

  • In Java 8 and later the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It's called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bpand subpackages.
  • 在 Java 8 和更高版本中,新的 API 是内置的。
  • 在 Java 6 和 7 中获得 ThreeTen Backport,新类的 backport(ThreeTen for JSR 310,现代 API 首次被描述)。
  • 在(较旧的)Android 上,使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。确保从包org.threeten.bp和子包中导入日期和时间类。

Links

链接

回答by MetaSnarf

It is because the source date string you parsed is in a differentformat than your SimpleDateFormatter. Your source is in MM/dd/yyyy HH:mmformat. Change the string in the SimpleDateFormat to MM/dd/yyyy HH:mm.

这是因为您解析的源日期字符串与 SimpleDateFormatter 的格式不同。您的来源是MM/dd/yyyy HH:mm格式。将 SimpleDateFormat 中的字符串更改为MM/dd/yyyy HH:mm.

Try using another format after your first format.

在第一种格式之后尝试使用另一种格式。

SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM/dd/yyyy HH:mm");
//You gotta parse it to a Date before correcting it
Date parsedDate = simpleDateFormat2.parse(startTime);
simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm")
String newFormatttedDate = simpleDateFormat2.format(parsedDate);

回答by tkausl

You're parsing the date 08/11/2008 00:00with the format yyyy/MM/dd HH:mmbut this date is notin this format.

您正在08/11/2008 00:00使用格式解析日期,yyyy/MM/dd HH:mm但此日期不是此格式。