To create Date in yyyy-MM-dd format we will make use of,
the LocalDate from java.time package,
the DateTimeFormatter class from the java.time.format package.
Let's take a look at a few examples,
Example 1: From Current Date
package org.code2care.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateFormattingyyyyMMddExample {
private static final String YYYY_MM_DD = "yyyy-MM-dd";
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
DateTimeFormatter dataTimeFormatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
String formattedDate = currentDate.format(dataTimeFormatter);
System.out.println("Date Today: " + formattedDate);
}
}
Output: 2023-07-03

Example 2: Formatting an existing Date as String
package org.code2care.examples;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateFormattingyyyyMMddExample2 {
private static final String YYYY_MM_DD = "yyyy-MM-dd";
public static void main(String[] args) {
String dateString = "2023-07-03";
LocalDate localDate = LocalDate.parse(dateString);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
String formattedDate = localDate.format(formatter);
System.out.println("Date: " + formattedDate);
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode] - Python
- Convert JSON to Java Collection Map using Jackson - Java
- Default speed of Marquee tag : SCROLLAMOUNT - Html
- Fix: AirPods not visible under Mac Bluetooth devices - MacOS
- How to clear Microsoft Teams Cache on Mac (macOS) - Teams
- AWS CLI Command to Get a List of SNS Topics - AWS
- Efficient way to perform HTTP cURL GET/POST Requests with Payload - cURL
- How to enable line numbers in IntelliJ Android Studio for all files - Android-Studio