2016-08-19 592 views
4

我正嘗試填寫Flash使用LocalDateTime下一個字符串,但我總是去未解析文本中找到的錯誤:錯誤java.time.format.DateTimeParseException:無法解析,索引10發現未解析文本

Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10 

這裏是我的字符串:convertDate: '2016年8月18日14:27:15.103 + 02'

而且我的代碼:

public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException { 
    LocalDate dateTime =LocalDate.parse(convertDate); 
    return dateTime; 
} 

我想是不是太ç omplicated,買我無法看到錯誤。字符串中的+02是否是原因?

+0

我認爲是日期和時間之間的空間。這是一個空間字符還是一個不間斷的空間?如果你的字符串來自某種形式的表單,那麼它可能與空間不同,而不是字符 –

+0

它來自Postgre數據庫 –

回答

2

TL;博士

OffsetDateTime odt = OffsetDateTime.parse ("2016-08-18 14:27:15.103+02" , DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss.SSSX")) ; 

詳細

Answer by greg-449是關於問題(使用了日期時間值的日期,僅對象),但不是解決正確。

該答案使用LocalDateTime,這會不必要地丟棄有關offset-from-UTC的寶貴信息。 A LocalDateTime確實不是代表時間線上的特定時刻,只是關於可能時刻的模糊想法,取決於調整到特定時區。

+02offset-from-UTC,意思是「比UTC提前兩小時」。因此,在UTC時間內,同時一刻的時間是12小時,比14小時少2小時。這確實代表時間線上的特定時刻。這個抵消是你拋棄LocalDateTime而不是OffsetDateTime的寶貴信息。

字符串格式採用SQL格式,接近標準ISO 8601格式。僅僅用T替換中間的空格。 java.time類默認使用ISO 8601格式,因此不需要指定格式化模式。

String input = "2016-08-18 14:27:15.103+02"; 
String inputModified = input.replace (" " , "T"); 

不幸的是,爪哇8具有在解析縮寫爲只是一個小時的偏移值或偏移值省略小時和分鐘之間的冒號的錯誤。在Java 9中已修復。但在Java 8中,我們需要調整輸入。

// Workaround for Java 8 where 2-digit offset fails parsing. Fixed in Java 9. 
int lengthOfAbbreviatedOffset = 3; 
if (inputModified.indexOf ("+") == (inputModified.length() - lengthOfAbbreviatedOffset)) { 
    // If third character from end is a PLUS SIGN, append ':00'. 
    inputModified = inputModified + ":00"; 
} 
if (inputModified.indexOf ("-") == (inputModified.length() - lengthOfAbbreviatedOffset)) { 
    // If third character from end is a PLUS SIGN, append ':00'. 
    inputModified = inputModified + ":00"; 
} 

現在解析。

OffsetDateTime odt = OffsetDateTime.parse (inputModified); 

轉儲到控制檯。請注意我們如何將+02轉換爲+02:00

System.out.println ("input: " + input + " | inputModified: " + inputModified + " | odt: " + odt); 

input: 2016-08-18 14:27:15.103+02 | inputModified: 2016-08-18T14:27:15.103+02:00 | odt: 2016-08-18T14:27:15.103+02:00

或者,指定的格式設置模式。使用這種格式化模式時,偏移解析錯誤不會引起誤解。

DateTimeFormatter f = DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss.SSSX"); 
    OffsetDateTime odt = OffsetDateTime.parse (input , f); 

數據庫

Postgres來了,你要檢索值的日期時間對象,而不是字符串。

如果您的JDBC驅動程序符合JDBC 4.2,則可以撥打ResultSet::getObject以獲得InstantOffsetDateTime。如果沒有,請致電ResultSet::getTimestamp以獲得java.sql.Timestamp,然後通過在時間戳對象上調用toInstant立即轉換爲java.time。

堅持與java.time爲您的業務邏輯;簡要使用java.sql類型,僅用於與數據庫交換。

5

你的代碼使用LocalDate只解析日期 - 不是一個日期和時間解析發現的日期後的空間,讓你得到一個錯誤。

因此,您應該使用LocalDateTime,但LocalDateTime.parse(String)預計ISO格式日期不是您正在使用的格式。

因此,您需要使用DateTimeFormatter來指定輸入字符串的格式。喜歡的東西:

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX"); 
LocalDateTime result = LocalDateTime.parse(convertDate, format); 
+0

是的!這工作!我曾嘗試過使用LocalDateTime,但使用了DateTimeFormatter。謝謝:) –