hutool格式化时间为年月日时分
时间: 2024-02-13 11:01:21 浏览: 27
使用Hutool进行时间格式化可以使用DateTime类的format方法。
示例代码如下:
```java
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
public class Test {
public static void main(String[] args) {
DateTime now = DateUtil.date();
String format = DateUtil.format(now, "yyyy-MM-dd HH:mm");
System.out.println(format);
}
}
```
输出结果为:2022-02-22 10:30
相关问题
Java格式化时间到年月日时分秒
可以使用Java中的SimpleDateFormat类来格式化时间,示例代码如下:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String formattedDate = formatter.format(date);
System.out.println("Formatted Date: " + formattedDate);
}
}
```
运行以上代码,将会输出当前时间的格式化结果,示例输出如下:
```
Formatted Date: 2021-08-23 15:20:45
```
其中,SimpleDateFormat的构造函数中的字符串参数表示时间的格式化规则,"yyyy"表示年份,"MM"表示月份,"dd"表示日期,"HH"表示小时(24小时制),"mm"表示分钟,"ss"表示秒。详细的格式化规则可以参考Java官方文档。
java格式化时间年月日时分秒
在Java中,可以使用SimpleDateFormat类来格式化时间。以下是一个示例:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
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(formattedDate);
}
}
```
在上面的示例中,我们首先创建了一个Date对象,表示当前时间。然后,我们创建了一个SimpleDateFormat对象,指定了要输出的时间格式。最后,我们调用了SimpleDateFormat的format方法来将Date对象格式化为字符串。
输出结果应该类似于:2022-01-01 00:00:00。
相关推荐
![](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)