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
Answer from azro on Stack OverflowRegarding 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
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
Videos
SimpleDateFormat will not work if he/she is starting with LocalDate, which is new in Java 8. From what I can see, you will have to use DateTimeFormatter.
LocalDate localDate = LocalDate.now(); // For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);
That should print 05 May 1988. To get the period (AKA full stop) after the day and before the month, you might have to use "dd'.LLLL yyyy".
It could be short as:
LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
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 formatter = DateTimeFormatter
.ofPattern("d/MM/uuuu")
.localizedBy(myLocale); // In Java 15+, this localizes the digits; you can comment it
String text = myDate.format(formatter);
Just format the date while printing it out:
var date = LocalDate.now();
var locale = Locale.forLanguageTag("fa");
var formatter = DateTimeFormatter
.ofPattern("d/MM/uuuu")
.localizedBy(locale); // In Java 15+, this localizes the digits; you can comment it
var dateText = date.format(formatter);
var parsedDate = LocalDate.parse(dateText, formatter);
System.out.println("date: " + date);
System.out.println("Text format " + dateText);
System.out.println("parsedDate: " + parsedDate.format(formatter));