Parsing Dates Without a Year in Java: Fixing the Common Error

This article demonstrates a common mistake when parsing a date string without a year in Java, shows the resulting DateTimeParseException, and provides a correct solution using DateTimeFormatterBuilder to supply a default year, complete with code examples and expected output.

FunTester
FunTester
FunTester
Parsing Dates Without a Year in Java: Fixing the Common Error

Incorrect Example

This example tries to parse a date string that lacks a year using a simple pattern "dd MMM".

package com.fun

import com.fun.frame.SourceCode
import java.time.LocalDate
import java.time.format.DateTimeFormatter

class TSSS extends SourceCode {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM", Locale.CHINA);
        String date = "02 12";
        LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println(localDate);
        System.out.println(formatter.format(localDate));
    }
}

Output

INFO-> 当前用户:fv,IP:192.168.0.100,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.3
Exception in thread "main" java.time.format.DateTimeParseException: Text '02 12' could not be parsed at index 3
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
	at java.time.LocalDate.parse(LocalDate.java:400)
	... (stack trace truncated)
Process finished with exit code 1

Correct Solution

The pattern "dd MMM" alone is insufficient; we need DateTimeFormatterBuilder to provide a default year for parsing.

package com.fun

import com.fun.frame.SourceCode
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
import java.time.temporal.ChronoField

class TSSS extends SourceCode {
    public static void main(String[] args) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("dd MM")
                .parseDefaulting(ChronoField.YEAR, 2020)
                .toFormatter(Locale.CHINA);
        String date = "02 11";
        LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println(localDate);
        System.out.println(formatter.format(localDate));
    }
}

Correct Output

INFO-> 当前用户:fv,IP:192.168.0.100,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.3
2020-11-02
02 11
Process finished with exit code 0
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentDate ParsinglocaldateDateTimeFormatterDateTimeFormatterBuilder
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.