import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Test001 {
public static void main(String[] args) {
Date dt = new Date();
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS zzz").withZone(ZoneId.of("America/New_York"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
System.out.println(formatter.format(localDateTime));
System.out.println(sdf.format(dt));
}
}
输出
2016-04-19 11:25:34.917 ET
2016-04-19 11:25:34.637 EDT
我的笔记本电脑时区是"America/New York"(即美国/加拿大东部时间)。
我想知道如何使用DateTimeFormatter
来获取"EDT"而不是"ET"。
我也想知道为什么DateTimeFormatter
在解析/格式化模式方面与SimpleDataFormat
不兼容(正如这个例子所展示的那样)。
SimpleDateFormat
类自Java 8以来基本上是一个遗留类。"版本说明:此日期和时间部分使用java.util
包中的日期和时间API。 JDK 8发布的java.time
API提供了全面的日期和时间模型,比java.util
类提供了显着的改进。java.time
API在日期时间指南中进行了描述。 遗留日期时间代码页面可能特别有用。" - Mr. Polywhirl