SimpleDateFormat
时间: 2023-11-22 17:01:59 浏览: 71
SimpleDateFormat格式化日期
SimpleDateFormat is a class in Java that is used to format and parse dates in a specified pattern. It is part of the java.text package and is commonly used to convert a Date object to a String and vice versa.
For example, you can create a SimpleDateFormat object with a specific pattern as follows:
```
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
```
This creates a SimpleDateFormat object that can format dates in the pattern "yyyy-MM-dd". You can then use this object to format a date as a string:
```
Date date = new Date();
String dateString = sdf.format(date);
```
This will format the current date in the specified pattern and return a string. You can also use a SimpleDateFormat object to parse a string into a Date object:
```
String dateString = "2021-05-12";
Date date = sdf.parse(dateString);
```
This will parse the string "2021-05-12" into a Date object using the specified pattern.
SimpleDateFormat supports a variety of pattern symbols that can be used to format and parse dates in different ways. For example, "yyyy" represents the year, "MM" represents the month, and "dd" represents the day of the month. The full list of pattern symbols can be found in the Java documentation.
阅读全文