Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert String "yyyy-MM-dd" to LocalDateTime

Is there any way to convert a date String to LocalDateTime where the format "yyyy-MM-dd" ?

If I try this:

DateTimeFormatter DATEFORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime ldt = LocalDateTime.parse(string, DATEFORMATTER);

I got this exception:

java.time.format.DateTimeParseException: Text '2017-03-13' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2017-03-13 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at hu.npsh.workforce.utils.Util.stringToLocalDateTime(Util.java:284)
    at hu.npsh.workforce.utils.util.StringLocalDateTimeConversionTest.stringToLocalDateTimeTest(StringLocalDateTimeConversionTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2017-03-13 of type java.time.format.Parsed
    at java.time.LocalDateTime.from(LocalDateTime.java:461)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 26 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2017-03-13 of type java.time.format.Parsed
    at java.time.LocalTime.from(LocalTime.java:409)
    at java.time.LocalDateTime.from(LocalDateTime.java:457)
    ... 28 more

I know that the main problem, that the pattern does not contain the hour and the minute.. But what if I want a create a method what gets a String and a DateTimeFormatter and I want to return with LocalDateTime

Is there a correct, nice solution?

EDIT:

My goal, making a method what get a String and a DateTimeFormatter And returns with a LocalDateTime. The Pattern can be anything (what is valid).

like image 552
LakiGeri Avatar asked Mar 13 '17 11:03

LakiGeri


People also ask

How convert string from YYYY MM DD to LocalDate in Java?

out. println(today); //Custom pattern is yyyy/MM/dd DateTimeFormatter formatter = DateTimeFormatter. ofPattern("dd-MMM-yyyy"); LocalDate date = LocalDate. parse("29-Mar-2019", formatter); System.

How do I parse LocalDateTime?

parse(CharSequence text) parse() method of a LocalDateTime class used to get an instance of LocalDateTime from a string such as '2018-10-23T17:19:33' passed as parameter. The string must have a valid date-time and is parsed using DateTimeFormatter. ISO_LOCAL_DATE_TIME.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


2 Answers

Use LocalDate to create a localDate and then you can add the timepart if you need it:

    DateTimeFormatter DATEFORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");     LocalDate ld = LocalDate.parse("2017-03-13", DATEFORMATTER);     LocalDateTime ldt = LocalDateTime.of(ld, LocalDateTime.now().toLocalTime());     System.out.println(ldt); 

or LocalDateTime

ldt = LocalDateTime.of(ld, LocalDateTime.MIN.toLocalTime()); 

if you just need an empty timepart

EDIT:

Look at this solution with this you can build your dynamic parser:

    DateTimeFormatter DATEFORMATTER1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");      DateTimeFormatter DATEFORMATTER = new DateTimeFormatterBuilder().append(DATEFORMATTER1)     .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)     .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)     .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)     .toFormatter();      //DateTimeFormatter DATEFORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");     LocalDateTime ldt = LocalDateTime.parse("2017-03-13", DATEFORMATTER); 
like image 75
Jens Avatar answered Sep 30 '22 10:09

Jens


You can not convert "2017-03-13" to a LocalDateTime since there is no time information in the string, only date. You can convert it to a LocalDate

DateTimeFormatter dateformatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate ld = LocalDate.parse("2017-03-13", dateformatter); 

after this we can covert it to LocalDateTime

LocalDateTime ldt = ld.atStartOfDay(); 
like image 25
Anton Balaniuc Avatar answered Sep 30 '22 10:09

Anton Balaniuc