[英]java.util.date to String using DateTimeFormatter
How can I convert a java.util.Date
to String
using如何将
java.util.Date
转换为String
使用
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);
If you are using Java 8, you should not use java.util.Date
in the first place (unless you receive the Date
object from a library that you have no control over).如果您使用的是 Java 8,则首先不应使用
java.util.Date
(除非您从无法控制的库中收到Date
对象)。
In any case, you can convert a Date
to a java.time.Instant
using:在任何情况下,您都可以使用以下方法将
Date
转换为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 LocalDateTime
object:由于您只对日期和时间感兴趣,而没有时区信息(我假设一切都是 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.toString
gives output in standard ISO 8601 format (including milliseconds) - that may be acceptable for you.请注意,如果您不提供格式化程序,调用
ldt.toString
会以标准ISO 8601格式(包括毫秒)提供输出 - 这对您来说可能是可以接受的。
DateTime dt = new DateTime(date);
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
dt.toString(dtf)
java.util.Date date = new java.util.Date(System.currentTimeMillis());
Instant instant = date.toInstant();
LocalDateTime ldt = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
System.out.println(ldt.format(fmt));
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 );
DateTimeFormatterOBJECT=DateTimeFormatter.ofPattern("DD/MMM/YYYY HH//MM/SS");
String MyDateAndTime= LocalDate.now().format(DateTimeFormatterOBJECT);
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.