simpledateformat("yyyy-MM-dd HH:mm:ss")
时间: 2023-09-29 15:05:10 浏览: 48
This is a format for displaying dates and times in the format of year-month-day hour:minute:second.
For example, if the current date and time is January 1st, 2022 at 3:45 PM and 30 seconds, it would be displayed as "2022-01-01 15:45:30".
相关问题
用SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
好的,使用SimpleDateFormat("yyyy-MM-dd HH:mm:ss")格式化输出时间的代码如下:
```java
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
// 创建Calendar实例
Calendar calendar = Calendar.getInstance();
// 获取当前时间
System.out.println("当前时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
// 将时间往前推6个小时
calendar.add(Calendar.HOUR_OF_DAY, -6);
// 格式化输出时间
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
System.out.println("当前时间前6个小时:" + time);
}
}
```
运行代码后,将输出当前时间和当前时间前6个小时的时间,格式均为"yyyy-MM-dd HH:mm:ss"。
SimpleDateFormat 中的yyyy-MM-dd HH:mm:ss.SSS说明
SimpleDateFormat 中的 yyyy-MM-dd HH:mm:ss.SSS 是指日期格式化的模板,具体含义如下:
- yyyy:代表年份,比如 2022;
- MM:代表月份,比如 01 表示一月份,12 表示十二月份;
- dd:代表月份中的天数,比如 01 表示一号,31 表示三十一号;
- HH:代表小时,24 小时制,比如 00 表示零点,23 表示二十三点;
- mm:代表分钟,比如 00 表示零分,59 表示五十九分;
- ss:代表秒数,比如 00 表示零秒,59 表示五十九秒;
- SSS:代表毫秒数,比如 000 表示零毫秒,999 表示九百九十九毫秒。
所以,如果使用 yyyy-MM-dd HH:mm:ss.SSS 格式化一个日期,会得到类似于 "2022-01-01 12:34:56.789" 的字符串。