Ronin
Ronin

Reputation: 49

Formatting date in java.time

I am trying to format date were is given in following format 05051988. I want to use LocalDate to format in next 05 may 1988.

I have create private method that i will use later in same class to input formatted date in text. I have already made some code but i at the moment I'm getting 1988-05-05 if I use MM, if I replace it with MMM then it is giving me error message that can not be parsed.

private LocalDate parseDate(){
    LocalDate dateOfBirth = LocalDate.parse(date, DateTimeFormatter.ofPattern("ddMMyyyy"));
    return dateOfBirth;
}

Error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '05051988' could not be parsed at index 2

Upvotes: 2

Views: 2591

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79580

java.time

The accepted answer has mixed the Joda-Time function name with the java.time API. The java.time.format.DateTimeFormatter class does not have a function called print(date-time object).

It has also missed an important thing, Locale while formatting LocalDate into a string having alphabets. Such a string will output the text in JVM's default Locale which may be undesirable. One should always specify a Locale for parsing/formatting a date-time string or object containing alphabetic characters. Check Always specify a Locale with a date-time formatter for custom formats to learn more about it.

Another important point is that while DateTimeFormatter#format can be used to format a date-time object, a more natural way is to use the date-time object's format function e.g. LocalDate#format.

Since your to-be-parsed date string and the expected output are in different formats, you need two DateTimeFormatter instances with applicable patterns.

Demo:

class Main {
    private static final DateTimeFormatter PARSING_FORMATTER = DateTimeFormatter.ofPattern(
            "ddMMuuuu");
    private static final DateTimeFormatter FORMATTING_FORMATTER = DateTimeFormatter.ofPattern(
            "dd MMM uuuu", Locale.ENGLISH);

    public static void main(String[] args) {
        LocalDate dateOfBirth = LocalDate.parse("05051988", PARSING_FORMATTER);
        System.out.println(dateOfBirth); // Default format is ISO_LOCAL_DATE
        String formattedDob = dateOfBirth.format(FORMATTING_FORMATTER);
        System.out.println(formattedDob);
    }
}

Output:

1988-05-05
05 May 1988

Try it on Ideone

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

Upvotes: 1

Ted Bigham
Ted Bigham

Reputation: 4348

You still need to use "ddMMyyyy" to parse the date. Then use "dd MMM yyyy" to format it when you print it.

Something like this:

String reformat(String dateOfBirth) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy");
    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");
    LocalDate date = LocalDate.parse(dateOfBirth, fomatter);
    return formatter2.print(date);
}

Upvotes: 3

Related Questions