java 日期格式化
时间: 2023-06-28 07:07:28 浏览: 94
Java 中日期格式化可以使用 SimpleDateFormat 类。具体步骤如下:
1. 创建一个 SimpleDateFormat 对象,并指定日期格式。
2. 调用 SimpleDateFormat 对象的 format() 方法,将日期对象格式化为字符串。
下面是一个示例代码,将当前日期格式化为 "yyyy-MM-dd HH:mm:ss" 格式的字符串:
```
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = dateFormat.format(currentDate);
System.out.println("Formatted Date: " + formattedDate);
}
}
```
输出结果为:
```
Formatted Date: 2021-08-18 16:45:12
```
你可以根据需要修改日期格式。具体格式化规则可以参考 SimpleDateFormat 类中的文档说明。
相关问题
Java 时间格式化
Java时间格式化是指将日期和时间格式化为特定的字符串格式。在Java中,可以使用SimpleDateFormat类或DateTimeFormatter类来进行时间格式化。SimpleDateFormat类是Java早期版本中用于格式化日期和时间的类,而DateTimeFormatter类是Java 8中引入的新类,它提供了更加灵活和线程安全的时间格式化方式。使用这两个类,可以将日期和时间格式化为各种不同的字符串格式,例如"yyyy-MM-dd HH:mm:ss"、"EEE, MMM d, ''yy"等等。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)