SimpleDateFormat 日时分秒
时间: 2023-11-22 14:48:43 浏览: 36
以下是Java中使用SimpleDateFormat格式化日期和时间的例子:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat in = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
String formattedDateOut = out.format(date);
String formattedDateIn = in.format(date);
System.out.println("Formatted Date (yyyy-MM-dd hh:mm:ss): " + formattedDateOut);
System.out.println("Formatted Date (MM/dd/yyyy hh:mm:ss): " + formattedDateIn);
}
}
```
输出:
```
Formatted Date (yyyy-MM-dd hh:mm:ss): 2022-08-23 03:22:45
Formatted Date (MM/dd/yyyy hh:mm:ss): 08/23/2022 03:22:45
```
在上面的例子中,我们使用了SimpleDateFormat类来格式化日期和时间。我们创建了两个SimpleDateFormat对象,一个用于输出格式(out),另一个用于输入格式(in)。我们使用format()方法将Date对象格式化为字符串,并将其存储在formattedDateOut和formattedDateIn变量中。最后,我们将格式化后的日期和时间输出到控制台。