java中将日期时间转换为dd/MM/yy格式的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18928064/
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-08-12 12:35:21  来源:igfitidea点击:

Convert dateTime to date in dd/MM/yy format in java

javadatedatetime

提问by Sara

I have a JODA DateTime2012-12-31T13:32:56.483+13:00. I would like to convert it to Date in dd/MM/yyformat. So I'm expecting code to return Date like - 31/12/12.

我有一个 JODA DateTime2012-12-31T13:32:56.483+13:00。我想将其转换为日期dd/MM/yy格式。所以我期待代码返回像 - 31/12/12 这样的日期。

Code -

代码 -

    // Input dateTime = 2012-12-31T13:32:56.483+13:00
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
    Date date = simpleDateFormat.parse(dateTime.toString("dd/MM/yy"));

Results:

结果:

Output - Mon Dec 31 00:00:00 NZDT 2012
Expected Output - 31/12/12

When I do the following, I get the expected output but I don't know how to convert it to Date-

当我执行以下操作时,我得到了预期的输出,但我不知道如何将其转换为 Date-

String string = simpleDateFormat.format(date); 

Please help me.

请帮我。

Thx

谢谢

EDIT - I want my end result to be Util Date in dd/MM/yy format. I Do not want String output. My input is Joda DateTime yyyy-MM-ddThh:mm:ss:+GMT. I need to convert JodaDateTime to UtilDate.

编辑 - 我希望我的最终结果是 dd/MM/yy 格式的 Util Date。我不想要字符串输出。我的输入是 Joda DateTime yyyy-MM-ddThh:mm:ss:+GMT。我需要将 JodaDateTime 转换为 UtilDate。

采纳答案by Thomas W

As I said originally, Date objects do not have an inherent format. java.util.Dateholds a millisecond time value, representing both date & time. Dates are parsed from string, or formatted to string, via your choice of DateFormat.

正如我最初所说,Date 对象没有固有格式。java.util.Date保存毫秒时间值,表示日期和时间。通过您选择的 DateFormat,日期从字符串解析,或格式化为字符串。

The strings may be formatted per specification, but the Date objects behind them are always full precision & do not have any inherent notion of format.

字符串可以按照规范进行格式化,但它们背后的 Date 对象始终是全精度的,并且没有任何固有的格式概念。



To truncate a combined "date and time" java.util.Dateto just the date component, leaving it effectively at midnight:

要将组合的“日期和时间”截断java.util.Date为仅日期组件,将其有效地保留在午夜:

public static Date truncateTime (Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime( date);
    cal.set( Calendar.HOUR_OF_DAY, 0);
    cal.set( Calendar.MINUTE, 0);
    cal.set( Calendar.SECOND, 0);
    cal.set( Calendar.MILLISECOND, 0);
    return cal.getTime();
}

If you're coming from JodaTime DateTime, you can do this more easily working mostly in the JodaTime API.

如果您来自 JodaTime DateTime,您可以更轻松地完成这项工作,主要是在 JodaTime API 中工作。

public static Date truncateJodaDT (DateTime dt) {
    java.util.Date result = dt.toDateMidnight().toDate();
    return result;
}

Hope this helps!

希望这可以帮助!

See:

看:



Now I'm unsure again, what you want. You want the date in string format now?

现在我又不确定了,你想要什么。您现在想要字符串格式的日期吗?

return simpleDateFormat.format( date);    // from java.util.Date

Or with JodaTime:

或者使用 JodaTime:

return dateTime.toString( "dd/MM/yy");    // from org.joda.time.DateTime

回答by kdureidy

Here is how you do it

这是你如何做到的

Format formatter = new SimpleDateFormat("dd/MM/yy");
String string = formatter.format(date);

回答by ravthiru

Using Java 8 provided date-time classes

使用 Java 8 提供的日期时间类

LocalDate parsedDate = LocalDate.parse(dateStr,DateTimeFormatter.ISO_ZONED_DATE_TIME);

回答by Basil Bourque

tl;dr

tl;博士

OffsetDateTime.parse( "2012-12-31T13:32:56.483+13:00" ).toLocalDate()

Joda-Time supplanted by java.time

由 java.time 取代的 Joda-Time

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

ISO 8601

ISO 8601

Your input string is in standard ISO 8601format.

您的输入字符串采用标准ISO 8601格式。

The java.time classes use the standard formats by default, so no need to specify a formatting pattern.

java.time 类默认使用标准格式,因此无需指定格式模式。

Date-time objects vs strings

日期时间对象与字符串

As the accepted Answerexplains, you should not conflate date-time objects with strings that may textually represent their value. A java.util.Dateobject has no formatas it has no text. Ditto for the java.time classes. You can use a java.time object to parsea string, or generatea string, but the object is distinct and separate from the string.

正如接受的答案所解释的那样,您不应将日期时间对象与可能以文本方式表示其值的字符串混为一谈。一个java.util.Date对象有没有格式,因为它没有任何文本。java.time 类也是如此。您可以使用 java.time 对象来解析字符串或生成字符串,但该对象是不同的并且与字符串分离。

OffsetDateTime

OffsetDateTime

Parse your input string as a OffsetDateTimeas it includes an offset-from-UTC though it lacks a time zone.

将您的输入字符串解析为 a,OffsetDateTime因为它包含一个从 UTC 的偏移量,尽管它没有时区。

OffsetDateTime odt = OffsetDateTime.parse( "2012-12-31T13:32:56.483+13:00" );

LocalDate

LocalDate

If you want a date-only value, use the LocalDateclass. Remember that for any given moment, the date varies around the globe by time zone. A minute after midnight is a new day in Paris while still “yesterday” in Montréal Québec. If you want the date from that same offset of +13:00, simply call toLocalDate.

如果您想要仅日期值,请使用LocalDate该类。请记住,对于任何给定时刻,全球各地的日期都因时区而异。午夜过后一分钟是巴黎的新一天,而魁北克蒙特利尔仍是“昨天”。如果您想要来自 的相同偏移量的日期+13:00,只需调用toLocalDate.

LocalDate localDate = odt.toLocalDate();

The Question says you do not want a string, only a date object. So there you go. If you later want a string, call toStringfor a String to be generated in standard ISO 8601 format, 2012-12-31. For other formats, search Stack Overflow for DateTimeFormatter.

问题说你不想要一个字符串,只想要一个日期对象。所以你去。如果以后需要字符串,请调用toString以标准 ISO 8601 格式生成的字符串,2012-12-31. 对于其他格式,在 Stack Overflow 中搜索DateTimeFormatter.



About java.time

关于 java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多