simpledateformat
时间: 2023-09-05 22:12:23 浏览: 113
SimpleDateFormat是Java中的一个日期格式化类,用于将日期对象格式化为指定格式的字符串,或将字符串解析为日期对象。它可以根据指定的模式字符串格式化日期,例如将日期格式化为"yyyy-MM-dd"或"HH:mm:ss"等格式。同时,SimpleDateFormat也支持本地化日期格式,例如将日期格式化为不同语言环境下的格式。使用SimpleDateFormat可以方便地进行日期格式化和解析操作。
相关问题
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.
Simpledateformat
SimpleDateFormat 是 Java 中的一个日期格式化类,它可以将 Date 对象格式化为指定格式的字符串,也可以将字符串解析为 Date 对象。SimpleDateFormat 中的格式化字符串由一系列日期和时间格式化符号组成,例如 yyyy 表示年份,MM 表示月份,dd 表示日期,HH 表示小时,mm 表示分钟,ss 表示秒等等。例如,以下代码将当前时间按照指定格式进行格式化:
```
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(new Date());
```
在这个例子中,formattedDate 的值将会是当前时间按照 "yyyy-MM-dd HH:mm:ss" 格式进行格式化后得到的字符串。我们也可以将字符串解析为 Date 对象,例如:
```
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2022-07-01 10:30:00");
```
在这个例子中,将字符串 "2022-07-01 10:30:00" 解析为一个 Date 对象,并将其赋值给变量 date。
阅读全文
相关推荐








