Home >Java >javaTutorial >What are the commonly used date formatting tools in Java function libraries?
The Java function library provides a variety of date formatting tools: SimpleDateFormat: can use pattern strings to format and parse dates. (Example: yyyy-MM-dd) DateTimeFormatter: A more comprehensive formatting tool available in the java.time API, created from pattern strings. (Example: yyyy-MM-dd) Joda-Time: Date and time library from the Apache community, providing more advanced functionality. (For example: time zone processing, date range operation)
Common date formatting tools in Java function library
java.time
is a date and time API introduced in Java 8, which provides rich functions for date and time processing, including multiple commonly used date formatting tools.
SimpleDateFormat:
SimpleDateFormat
class provides a way to format and parse dates and times. It uses a pattern string to define the desired format, such as yyyy-MM-dd
.
import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample { public static void main(String[] args) { Date date = new Date(); // 使用模式字符串进行格式化 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(date); System.out.println("格式化后的日期:" + formattedDate); // 使用解析字符串进行解析 SimpleDateFormat sdfParse = new SimpleDateFormat("yyyy-MM-dd"); Date parsedDate = sdfParse.parse(formattedDate); System.out.println("解析后的日期:" + parsedDate); } }
DateTimeFormatter:
DateTimeFormatter
class is introduced in the java.time
API, which provides a more comprehensive and Configurable date formatting capabilities. Create a DateTimeFormatter
instance by specifying the pattern string via the ofPattern
method.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateTimeFormatterExample { public static void main(String[] args) { LocalDate date = LocalDate.now(); // 使用模式字符串创建 DateTimeFormatter DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 格式化日期 String formattedDate = dtf.format(date); System.out.println("格式化后的日期:" + formattedDate); // 解析日期 LocalDate parsedDate = LocalDate.parse(formattedDate, dtf); System.out.println("解析后的日期:" + parsedDate); } }
Joda-Time:
Joda-Time is a widely used date and time API developed by the Apache community. It provides additional functionality not found in the java.time
API, such as time zone handling and date range operations.
import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class JodaTimeExample { public static void main(String[] args) { DateTime dateTime = new DateTime(); // 使用模式字符串创建 DateTimeFormatter DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); // 格式化日期 String formattedDate = dtf.print(dateTime); System.out.println("格式化后的日期:" + formattedDate); // 解析日期 DateTime parsedDateTime = dtf.parseDateTime(formattedDate); System.out.println("解析后的日期:" + parsedDateTime); } }
The above is the detailed content of What are the commonly used date formatting tools in Java function libraries?. For more information, please follow other related articles on the PHP Chinese website!