![](/img/trans.png)
[英]How to format date/time/zone to pattern with offset from UTC in Java?
[英]How to use offset time in Java Simple Date Format
當我運行以下代碼將時間字符串轉換為Java日期時,它失敗了。
String s = "04/17/2017 06:46:53 -600";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
Date value = format.parse(s);
Exception in thread "main" java.text.ParseException: Unparseable date: "04/17/2017 06:46:53 -600"
at java.text.DateFormat.parse(Unknown Source)
將日期字符串轉換為java date的正確格式字符串是什么?
RFC 822時區:對於格式,使用RFC 822 4位時區格式:
RFC822TimeZone:
Sign TwoDigitHours Minutes
TwoDigitHours:
Digit Digit
ISO 8601時區:圖案字母的數量指定格式化和解析的格式,如下所示:
ISO8601TimeZone:
OneLetterISO8601TimeZone
TwoLetterISO8601TimeZone
ThreeLetterISO8601TimeZone
OneLetterISO8601TimeZone:
Sign TwoDigitHours
Z
TwoLetterISO8601TimeZone:
Sign TwoDigitHours Minutes
Z
ThreeLetterISO8601TimeZone:
Sign TwoDigitHours : Minutes
Z
“小時”始終顯示為兩位數,因此您需要通過-0600
作為時區或-06
擁有一位數小時的唯一方法是:
常規時區:如果時區具有名稱,則將它們解釋為文本。 對於表示GMT偏移值的時區,使用以下語法:
GMTOffsetTimeZone:
GMT Sign Hours : Minutes
Sign: one of
+ -
Hours:
Digit
Digit Digit
Minutes:
Digit Digit
Digit: one of
0 1 2 3 4 5 6 7 8 9
添加-0600代替-600 閱讀鏈接
import java.text.*;
import java.util.*;
class TestFormatDate {
public static void main(String arg[]) throws Exception{
String s = "04/17/2017 06:46:53 -0600";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
Date value = format.parse(s);
System.out.println("value "+value);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.