How to format LocalDate object to MM/dd/yyyy and have format persist
Asked Answered
D

4

34

I am reading text and storing the dates as LocalDate variables.

Is there any way for me to preserve the formatting from DateTimeFormatter so that when I call the LocalDate variable it will still be in this format.

EDIT:I want the parsedDate to be stored in the correct format of 25/09/2016 rather than printing as a string

My code:

public static void main(String[] args) 
{
    LocalDate date = LocalDate.now();
    DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
    String text = date.format(formatters);
    LocalDate parsedDate = LocalDate.parse(text, formatters);

    System.out.println("date: " + date); // date: 2016-09-25
    System.out.println("Text format " + text); // Text format 25/09/2016
    System.out.println("parsedDate: " + parsedDate); // parsedDate: 2016-09-25

    // I want the LocalDate parsedDate to be stored as 25/09/2016
}
Doro answered 25/9, 2016 at 17:43 Comment(1)
A LocalDate object does not have a format - it cannot, by itself, remember that it has to be in a specific format such as MM/dd/yyyy.Scouting
B
52

EDIT: Considering your edit, just set parsedDate equal to your formatted text string, like so:

parsedDate = text;

A LocalDate object can only ever be printed in ISO8601 format (yyyy-MM-dd). In order to print the object in some other format, you need to format it and save the LocalDate as a string like you've demonstrated in your own example

DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
String text = date.format(formatters);
Brag answered 25/9, 2016 at 17:51 Comment(4)
cannot convert from String to LocalDate error when I try that assignmentDoro
Oh I'm sorry, parsedDate will have to be a string in order to be saved in that format. So you'll have to define parsedDate as a string.Brag
So there is no other format that a LocalDate variable can take other than yyyy-MM-dd unless you store it instead as a String? ThanksDoro
No, see Joe C's answer! Maybe it will explain things betterBrag
M
17

Just format the date while printing it out:

public static void main(String[] args) {
    LocalDate date = LocalDate.now();
    DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
    String text = date.format(formatters);
    LocalDate parsedDate = LocalDate.parse(text, formatters);

    System.out.println("date: " + date);
    System.out.println("Text format " + text);
    System.out.println("parsedDate: " + parsedDate.format(formatters));
}
Mitsue answered 25/9, 2016 at 17:46 Comment(0)
C
10

No, you cannot have a format persist, because you cannot override toString of LocalDate (constructor of LocalDate is private, it is imposible extends) and there are not a method to change the format in LocalDate persistently.

Maybe, you could create a new class and use an static method to change the format, but you have always to use MyLocalDate.myToString(localDate) instead localDate.toString() when you want other format.

 public class MyLocalDate {
    
    public static String myToString(LocalDate localDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        return localDate.format(formatter);
    }   
}

When you invoke you have to use this way

FechaInicioTextField.setText(MyLocalDate.myToString(fechaFacturaInicial));

instead of

FechaInicioTextField.setText(fechaFacturaInicial.toString());
Chaparro answered 4/8, 2020 at 20:52 Comment(0)
V
5

Short answer: no.

Long answer: A LocalDate is an object representing a year, month and day, and those are the three fields it will contain. It does not have a format, because different locales will have different formats, and it will make it more difficult to perform the operations that one would want to perform on a LocalDate (such as adding or subtracting days or adding times).

The String representation (produced by toString()) is the international standard on how to print dates. If you want a different format, you should use a DateTimeFormatter of your choosing.

Voelker answered 25/9, 2016 at 19:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.