Date日期类常用方法

时间格式常用符号

Date日期类总结

为什么用SimpleDateFormat

SimpleDateFormat常见方法

SimpleDateFormat解析字符串时间成为日期对象

SimpleDateFormat总结

代码:
代码一:Date日期类
package com.itheima.Time;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
Date d =new Date();
System.out.println(d);
long time =d.getTime();
System.out.println(time);
time += 2*1000;
Date d2 = new Date(time);
System.out.println(d2);
Date d3 = new Date();
d3.setTime(time);
System.out.println(d3);
}
}

代码二:SimpleDateFormat
package com.itheima.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatTest {
public static void main(String[] args) throws ParseException {
Date d = new Date();
System.out.println(d);
long time = d.getTime();
System.out.println(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str1 = sdf.format(d);
System.out.println(str1);
String str2 = sdf.format(time);
System.out.println(str2);
System.out.println("=================================================");
String str3 = "2025-05-21 18:00:20";
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d2 = sdf2.parse(str3);
System.out.println(d2);
}
}
