Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a year String to a LocalDate with Java8

With Joda library, you can do

DateTimeFormat.forPattern("yyyy").parseLocalDate("2008")

that creates a LocalDate at Jan 1st, 2008

With Java8, you can try to do

LocalDate.parse("2008",DateTimeFormatter.ofPattern("yyyy"))

but that fails to parse:

Text '2008' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2008},ISO of type java.time.format.Parsed

Is there any alternative, instead of specifically writing sth like

LocalDate.ofYearDay(Integer.valueOf("2008"), 1)

?

like image 800
facewindu Avatar asked Dec 28 '16 08:12

facewindu


People also ask

How do I convert a string to LocalDate?

Parsing String to LocalDate parse() method takes two arguments. The first argument is the string representing the date. And the second optional argument is an instance of DateTimeFormatter specifying any custom pattern. //Default pattern is yyyy-MM-dd LocalDate today = LocalDate.

How do I parse a date in LocalDate?

Date date = new Date(); LocalDate localDate = date. toInstant(). atZone(ZoneId.

How do I add a year to my LocalDate?

The plusYears() method of LocalDate class in Java is used to add the number of specified years in this LocalDate and return a copy of LocalDate. This method adds the years field in the following steps: Add the years to the year field. Check if the date after adding years is valid or not.

How do I extract year from LocalDate?

The year for a particular LocalDate can be obtained using the getYear() method in the LocalDate class in Java. This method requires no parameters and it returns the year which can range from MIN_YEAR to MAX_YEAR.


2 Answers

    String yearStr = "2008";
    Year year = Year.parse(yearStr);
    System.out.println(year);

Output:

2008

If what you need is a way to represent a year, then LocalDate is not the correct class for your purpose. java.time includes a Year class exactly for you. Note that we don’t even need an explicit formatter since obviously your year string is in the default format for a year. And if at a later point you want to convert, that’s easy too. To convert into the first day of the year, like Joda-Time would have given you:

    LocalDate date = year.atDay(1);
    System.out.println(date);

2008-01-01

In case you find the following more readable, use that instead:

    LocalDate date = year.atMonth(Month.JANUARY).atDay(1);

The result is the same.

If you do need a LocalDate from the outset, greg449’s answer is correct and the one that you should use.

like image 99
Ole V.V. Avatar answered Sep 28 '22 06:09

Ole V.V.


LocalDate parsing requires that all of the year, month and day are specfied.

You can specify default values for the month and day by using a DateTimeFormatterBuilder and using the parseDefaulting methods:

DateTimeFormatter format = new DateTimeFormatterBuilder()
     .appendPattern("yyyy")
     .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
     .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
     .toFormatter();

LocalDate.parse("2008", format);
like image 42
greg-449 Avatar answered Sep 28 '22 05:09

greg-449