package com.cnblogs;
import java.math.BigDecimal;
import java.util.Date;
public class TestDate {
/*
本类用于测试Date类
*/
public static void main(String[] args) {
//今天
Date date1 = new Date();
System.out.println(date1.toString());//Sat Jul 10 15:17:27 CST 2021
System.out.println(date1.toLocaleString());//2021-7-10 15:17:27
//昨天
Date date2 = new Date(date1.getTime()-60*60*24*1000);
System.out.println(date2.toLocaleString());//2021-7-9 15:17:27
//after 在...之后 before在...之前
boolean b1 = date1.after(date2);
System.out.println(b1);//true
boolean b2 = date1.before(date2);
System.out.println(b2);//false
//比较 compareTo();
int d = date2.compareTo(date1);
System.out.println(d);//-1
int d2 = date2.compareTo(date2);
System.out.println(d2);//0
//equals();比较两个值是否相等
boolean b3 = date1.equals(date2);
System.out.println(b3);//false
}
}
原文:https://www.cnblogs.com/fangweicheng666/p/14994413.html