2016-07-23 25 views
0

Ich habe ein seltsames Problem, wenn eine Zeichenfolge in eine LocalParse String Local

import java.time.LocalDateTime; 
import java.time.format.DateTimeFormatter; 

public class Main { 

    public static void main(String args[]) 
    { 
     DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME; 
     LocalDateTime.parse("00:00",formatter); 
    } 

} 

Gib mir analysieren:

Exception in thread "main" java.time.format.DateTimeParseException: Text '00:00' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 00:00 of type java.time.format.Parsed 
    at java.time.format.DateTimeFormatter.createError(Unknown Source) 
    at java.time.format.DateTimeFormatter.parse(Unknown Source) 
    at java.time.LocalDateTime.parse(Unknown Source) 
    at Main.main(Main.java:9) 
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 00:00 of type java.time.format.Parsed 
    at java.time.LocalDateTime.from(Unknown Source) 
    at java.time.format.Parsed.query(Unknown Source) 
    ... 3 more 
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {},ISO resolved to 00:00 of type java.time.format.Parsed 
    at java.time.LocalDate.from(Unknown Source) 
    ... 5 more 

Ich möchte einen String mit Format analysieren "Stunde: Minuten" zu eine lokale Zeit (24H Format). Es ist mir egal, welcher Monat/Jahr/Tag wie vereinbart ist, ich will nur die Zeit.

+0

Scheint, dass Ihre Daten "00:00" nicht mit dem von ISO_LOCAL_TIME vorgegebenen Format kompatibel sind. – user1929959

Antwort

5

Es ist mir egal, was Monat/Jahr/Tag ist, ich will nur die Zeit.

Das legt nahe, Sie sollte es als gegeben LocalTime, Parsing, dass das ist, was die Saite tatsächlich darstellt. Sie können dann jede LocalDate, um es hinzuzufügen, um ein LocalDateTime zu erhalten, wenn Sie wirklich wollen Sie mehr Informationen haben, so zu tun bekommen, dann haben Sie:

import java.time.*; 
import java.time.format.*; 

public class Test {  
    public static void main(String args[]) { 
     DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME; 
     LocalTime time = LocalTime.parse("00:00",formatter); 
     LocalDate date = LocalDate.of(2000, 1, 1); 
     LocalDateTime dateTime = date.atTime(time); 
     System.out.println(dateTime); // 2000-01-01T00:00 
    } 
} 

Wenn Sie ein LocalDateTime überhaupt und nur Arbeit zu schaffen vermeiden mit der LocalTime wäre das noch besser.

+0

Oh, ich wusste nichts über LocalTime-Klasse. – amchacon