simpledateformat例子
时间: 2023-07-18 22:24:32 浏览: 38
下面是一个使用SimpleDateFormat进行日期格式化的例子:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(now);
System.out.println("Current time is: " + formattedDate);
}
}
```
这个例子中,我们首先创建一个Date对象表示当前时间,然后创建一个SimpleDateFormat对象,指定格式化模式为"yyyy-MM-dd HH:mm:ss",这个模式表示将日期格式化为年份-月份-日 小时:分钟:秒的形式。接着,我们调用SimpleDateFormat的format方法,将Date对象格式化为指定格式的字符串。最后,我们将格式化后的字符串打印出来。
输出结果类似于:Current time is: 2021-05-12 16:54:21
相关问题
simpledateformat使用例子
SimpleDateFormat是Java中用于日期格式化和解析的类。它可以将日期对象按照指定的格式转换成字符串,也可以将字符串按照指定的格式解析成日期对象。
下面是一个SimpleDateFormat的使用例子:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
public static void main(String[] args) {
// 创建SimpleDateFormat对象,指定日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化当前日期
String formattedDate = sdf.format(new Date());
System.out.println("Formatted Date: " + formattedDate);
try {
// 解析字符串为日期对象
Date parsedDate = sdf.parse("2022-01-01 12:00:00");
System.out.println("Parsed Date: " + parsedDate);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
上述代码中,我们首先创建了一个SimpleDateFormat对象,并指定日期格式为"yyyy-MM-dd HH:mm:ss"。然后,通过调用format方法,将当前日期格式化成字符串并打印输出。接着,我们使用parse方法将字符串"2022-01-01 12:00:00"解析成日期对象,并打印输出。
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。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)