Java uses SimpleDateFormat to format dates

In Java, the SimpleDateFormat class can be used to format dates. Before using SimpleDateFormat, it is necessary to add the corresponding dependencies in the pom.xml file of the project. The Maven coordinates of the dependent class library are as follows: <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> Briefly introduce the library: Commons lang is an open source project under the Apache Software Foundation that provides many commonly used Java tool classes. The DateUtils class provides methods for formatting and parsing dates. Implement the complete sample and write the complete Java code as follows: import org.apache.commons.lang3.time.DateUtils; import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String formattedDate = sdf.format(date); System.out.println("Formatted Date: " + formattedDate); try { Date parsedDate = sdf.parse(formattedDate); System.out.println("Parsed Date: " + parsedDate); } catch (Exception e) { e.printStackTrace(); } // Using commons-lang DateUtils String formattedDate2 = DateUtils.format(date, "MM/dd/yyyy"); System.out.println("Formatted Date 2: " + formattedDate2); try { Date parsedDate2 = DateUtils.parseDate(formattedDate2, "MM/dd/yyyy"); System.out.println("Parsed Date 2: " + parsedDate2); } catch (Exception e) { e.printStackTrace(); } } } The above code first creates a Date object for the current date. Then, use SimpleDateFormat to format the date and output the formatted date. Next, use SimpleDateFormat to parse the formatted date and output the parsed date. Finally, the code uses the DateUtils class of commons lang to format and parse dates, and the output result is the same as the previous method. Summary: Using SimpleDateFormat can easily format and parse dates, meeting most date format requirements. The DateUtils class in commons lang provides more functionality, making date processing more convenient. In actual development, suitable methods can be selected according to needs.