xorian
xorian

Reputation: 45

Java - How to get the correct date format with LocalDate

I've been having trouble correctly formatting the date as dd-MM-YYYY.

When I arrange the String dateString in the order of year-month-day, or year-day-month, it allows the date to be formatted.

It seems to only work when the yearParsed String as at the begginning of dateString.

Attempting to use DateTimeFormatter.ofPattern("dd-MM-YYYY") didn't seem to affect the date so it looks like I was not using it correctly.

Could you please let me know what I am doing wrong?

The user inputs a day, month and year one at a time, and I am looking to output the date as: 01-12-2000. The if/else are there to add a '0' in front, if the date or month input is a single digit.

Any help would be greatly appreciated.

Thank you!

    String yearParsed = String.valueOf(year);
    String monthParsed;
    String dayParsed;
    if (dayString.length() == 1) {         
        dayParsed = "0" + String.valueOf(day); 
    }
    else {
        dayParsed = String.valueOf(day);
    }
    if (monthString.length() == 1) {         
        monthParsed = "0" + String.valueOf(month);        
    }
    else {
        monthParsed = String.valueOf(month);
    }
    
    String dateString = yearParsed + "-" + monthParsed + "-" + dayParsed;
    //String dateString = dayParsed + "-" + monthParsed + "-" + yearParsed;

    System.out.println("dateString " + dateString);
    
    LocalDate formattedDate = null;  
    DateTimeFormatter dateTimeFormatter;  
    dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
    //dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-YYYY");

    formattedDate = formattedDate.parse(String.format(dateString, dateTimeFormatter));
    System.out.println("Formatted Date = " + formattedDate);

Upvotes: 2

Views: 3004

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

You have used Y (week-based-year) instead of y (year-of-era). Learn the difference from the documentation and from answers to this question.

Simply create a LocalDate with the year, month and day and format it to a String using a DateTimeFormatter.

Demo:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        int day = 12, month = 6, year = 2021;
        LocalDate date = LocalDate.of(year, month, day);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ENGLISH);
        String formatted = dtf.format(date);
        System.out.println(formatted);
    }
}

Output:

2021-06-12

ONLINE DEMO

Here, you can use y instead of u but I prefer u to y.

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

Note

A LocalDate is supposed to represent date units (year, month, day), and not a specific format. The default format used by LocalDate#toString is based on ISO 8601 standard. For a specific format, you need to format it into a String as shown above. It is like representing double d = 5.0 as the 5.000 which is done by formatting d into a String of this format.

public class Main {
    public static void main(String[] args) {
        double d = 5.0;
        NumberFormat formatter = new DecimalFormat("#0.000");
        String formatted = formatter.format(d);
        System.out.println(formatted);
    }
}

Output:

5.000

Upvotes: 1

azro
azro

Reputation: 54148

Regarding your variable LocalDate formattedDate, you're misunderstanding the concept of formatted date.

A formatted date is a String, because you can control it's format.

When the object is a LocalDate instance, it contains value to determine a position in the time, when you just print it it has its default formatting, it you want one specific formatting you need a String representation of your date


String year = "2021", dayString = "1", monthString = "3";

LocalDate date = LocalDate.of(
        Integer.parseInt(year),
        Integer.parseInt(monthString),
        Integer.parseInt(dayString)
);

System.out.println(date); // 2021-03-01

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formattedDate = date.format(dtf);
System.out.println("Formatted Date = " + formattedDate); // Formatted Date = 01-03-2021

Upvotes: 3

Related Questions