Pat*_*tty 3 java timezone datetime datetime-format simpledateformat
我正在尝试以 ( ) 格式获取日期的输出String:
\n\n\n20170801\xe2\x80\x8b \xe2\x80\x8b123030\xe2\x80\x8b \xe2\x80\x8b美国/洛杉矶
\n
但使用这段代码:
\n\nSimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hhmmss Z", Locale.US);\nsdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));\nSystem.out.println(sdf.format(new java.util.Date()));\nRun Code Online (Sandbox Code Playgroud)\n\n我得到的输出为(注意区域部分):
\n\n\n\n\n20174904 024908 -0700
\n
知道如何修复它吗?我需要打印"\xe2\x80\x8bAmerica/Los_Angeles"而不是"-0700".
看起来SimpleDateFormat不支持你想要的。
以下是可能的格式:
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
Run Code Online (Sandbox Code Playgroud)
您可以在格式化日期后添加“America/Los_Angeles”:
TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles");
DateFormat sdf = new SimpleDateFormat("yyyyMMdd hhmmss", Locale.US);
sdf.setTimeZone(timeZone);
System.out.println(sdf.format(new Date()) + " " + timeZone.getID());
DateFormat sdfWithTimeZone = new SimpleDateFormat("yyyyMMdd hhmmss zzzz", Locale.US);
sdfWithTimeZone.setTimeZone(timeZone);
System.out.println(sdfWithTimeZone.format(new Date()));
Run Code Online (Sandbox Code Playgroud)
输出:
20171004 031611 America/Los_Angeles
20171004 031611 Pacific Daylight Time
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3009 次 |
| 最近记录: |