User3
User3

Reputation: 2535

Date(String) parse error in Java

I am using the following lines of code to parse a String as a Date:

String displayBirthday;
...
java.util.Date  ss1=new Date(displayBirthday);
SimpleDateFormat formatter5=new SimpleDateFormat("yyyy-MM-dd");
displayBirthday = formatter5.format(ss1);
li.add(displayBirthday);

It works fine for many dates, but when I want to parse a date like: 0001-03-10

It gives me the following error:

java.lang.IllegalArgumentException: Parse error: 0001-03-10

I am using a prefix of 0001 for dates which dont have a year as an internal representation. How to overcome this?

Upvotes: 2

Views: 1061

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

java.time

In Mar 2014 (months before the question was posted), the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern date-time API. Since then, it is highly recommended to stop using the legacy date-time API.

Using java.time, the modern date-time API:

You do not need a DateTimeFormatter: java.time API is based on ISO 8601 and therefore you do not need a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format e.g. your date string, 0001-03-10 which can be parsed directly into a LocalDate instance which contains just date units.

Demo:

import java.time.LocalDate;

class Main {
    public static void main(String args[]) {
        String strDateTime = "0001-03-10";
        LocalDate date = LocalDate.parse(strDateTime);
        System.out.println(date);
    }
}

Output:

0001-03-10

ONLINE DEMO

I am using a prefix of 0001 for dates which dont have a year as an internal representation. How to overcome this?

As suggested by Ole V.V., a MonthDay is probably a good answer to it. Note that the default pattern used by MonthDay#parse is --MM-dd. If your string is not in this format, you can build a custom DateTimeFormatter.

An alternative to parsing to MonthDay is building a DateTimeFormatter with default year which will allow your string to be parsed directly into a LocalDate.

Demo:

class Main {
    public static void main(String args[]) {
        // If your string is in --MM-dd format
        MonthDay monthDay = MonthDay.parse("--03-10");
        // If you want the current year, replace 1 with Year.now().getValue()
        LocalDate date = monthDay.atYear(1);
        System.out.println(date);

        // If your string is in MM-dd format
        DateTimeFormatter monthDayFormatter = DateTimeFormatter.ofPattern("MM-dd", Locale.ENGLISH);
        monthDay = MonthDay.parse("03-10", monthDayFormatter);
        // If you want the current year, replace 1 with Year.now().getValue()
        date = monthDay.atYear(1);
        System.out.println(date);

        // An alternative solution
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                .appendPattern("MM-dd")
                // If you want the current year, replace 1 with Year.now().getValue()
                .parseDefaulting(ChronoField.YEAR, 1)
                .toFormatter(Locale.ENGLISH);
        date = LocalDate.parse("03-10", dtf);
        System.out.println(date);
    }
}

Output:

0001-03-10
0001-03-10
0001-03-10

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

Date(java.lang.String)' is deprecated , just use SimpleDateFormat

Just like follows

   SimpleDateFormat formatter5=new SimpleDateFormat("yyyy-MM-dd");
   String displayBirthday = formatter5.format(formatter5.parse("0001-03-10"));
   System.out.println(displayBirthday);

Out put:

   0001-03-10

Upvotes: 3

Related Questions