Date、Calendar类
Date类大部分方法都已经过时;
Calendar类是Date类的替代。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210508091641109.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x1b2h6eno=,size_16,color_FFFFFF,t_70#pic_center)
~~~java
package Demo4;
import java.util.Date;
//Date表示特定的时间
public class DateDemo {
public static void main(String[] args) {
Date Date = new Date();
System.out.println(Date);//目前时间
Date date = new Date(Date.getTime()-(60*60*24*1000));
System.out.println(date.toString());//上一天的时间
boolean a = Date.after(date);
System.out.println(a);
boolean b = Date.before(date);//判断时间的前后
System.out.println(b);
int c = Date.compareTo(date);//compareTo是比较
System.out.println(c);
boolean d = Date.equals(date);
System.out.println(d);//equals判断时间是否相同
}
}
package Demo4;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();//创建Calender对象
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());//时间毫秒值!!
int year = calendar.get(Calendar.YEAR);//时间的年
System.out.println(year);
int a = calendar.get(Calendar.MONTH);//留意Calender后面的一般表示提取时间的因素!!
int a2 = calendar.get(Calendar.DAY_OF_MONTH);
int a3 = calendar.get(Calendar.DATE);
System.out.println(a);
System.out.println(a2);
System.out.println(a3);
//改变时间
Calendar calendar1c = Calendar.getInstance();
calendar1c.set(Calendar.DAY_OF_MONTH,4);
System.out.println(calendar1c.getTime().toLocaleString());
}
}