java.util.date 到 String 使用 DateTimeFormatter

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

java.util.date to String using DateTimeFormatter

javadatetimejava-8jodatime

提问by Cork Kochi

How can I convert a java.util.Dateto Stringusing

如何将 a 转换java.util.DateStringusing

 DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss")

The Date object which I get is passed

我得到的 Date 对象被传递

DateTime now = new DateTime(date);

回答by ΦXoc? ? Пepeúpa ツ

since I asume you are using joda API: ergo, DateTimeFormatter is comming from org.joda.time.format.DateTimeFormatter:

因为我假设您正在使用 joda API:ergo,DateTimeFormatter 来自org.joda.time.format.DateTimeFormatter

 String dateTime = "02-13-2017 18:20:30";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM-dd-yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);

System.out.println(jodatime );

回答by Cork Kochi

DateTime dt = new DateTime(date);
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
dt.toString(dtf)

回答by assylias

If you are using Java 8, you should not use java.util.Datein the first place (unless you receive the Dateobject from a library that you have no control over).

如果您使用的是 Java 8,则首先不应使用java.util.Date(除非您Date从无法控制的库中接收对象)。

In any case, you can convert a Dateto a java.time.Instantusing:

在任何情况下,您都可以使用以下方法将 a 转换Date为 a java.time.Instant

Date date = ...;
Instant instant = date.toInstant();

Since you are only interested in the date and time, without timezone information (I assume everything is UTC), you can convert that instant to a LocalDateTimeobject:

由于您只对日期和时间感兴趣,而没有时区信息(我假设一切都是 UTC),您可以将该时刻转换为LocalDateTime对象:

LocalDateTime ldt = instant.atOffset(ZoneOffset.UTC).toLocalDateTime();

Finally you can print it with:

最后,您可以使用以下命令打印它:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(ldt.format(fmt));

Or use the predefined formatter, DateTimeFormatter.ISO_LOCAL_DATE_TIME.

或者使用预定义的格式化程序DateTimeFormatter.ISO_LOCAL_DATE_TIME.

System.out.println(ldt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

Note that if you don't provide a formatter, calling ldt.toStringgives output in standard ISO 8601format (including milliseconds) - that may be acceptable for you.

请注意,如果您不提供格式化程序,则调用ldt.toString会以标准ISO 8601格式(包括毫秒)提供输出- 这对您来说可能是可以接受的。

回答by RANA DINESH

DateTimeFormatterOBJECT=DateTimeFormatter.ofPattern("DD/MMM/YYYY HH//MM/SS");

String MyDateAndTime= LocalDate.now().format(DateTimeFormatterOBJECT);

回答by Archmede

You can use the joda time formatter class:

您可以使用 joda 时间格式化程序类:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
# conflict with import java.time.format.DateTimeFormatter;

final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss")
            .withZone(DateTimeZone.UTC);

    System.out.println(DateTime.now().toString(formatter));