在Java中可以使用Date类和Calendar类来处理日期

但是Date类很多方法都过时了,推荐使用Canlendar类来处理日期,并对日期的格式化做了介绍。下面的部分将会逐一介绍

 

    Java提供了Date类来处理日期、时间,Date类既包含日期,也包含时间。Date类从JDK1.0版本就开始存在,存在时间久远,提供的6个构造器中,已有4个建议放弃使用,目前使用的为:

  • Date():生成一个以系统当前时间日期为准的Date对象。
  • Date(long date):根据指定的long整型数来生成一个Date对象。
  • boolean after(Date when):判断日期是否在指定的日期when之后
  • boolean before(Date when):判断日期是否在指定的日期when之前。
  • int compareTo(Date antherDate):比较两个日期大小,后面时间大于前面时间返回-1,否则返回1.
  • Boolean equals(Object obj):两个时间表示同一时间是返回true
  • long getTime():返回该对象对应long型整数
  • void setTime(long time):设置该对象的时间。

  

public class DateTest {
    public static void main(String[] args) throws InterruptedException {
        
        long start = System.currentTimeMillis();
        Date d1 = new Date(start);
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        Date d2 = new Date(end);
        System.out.println(d1.before(d2));
        System.out.println(d1.after(d2));
        System.out.println(d1.getTime());
        System.out.println(d1.equals(d2));    
        
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2021-06-07
  • 2021-10-18
  • 2022-12-23
  • 2021-05-23
  • 2022-01-22
  • 2021-07-04
猜你喜欢
  • 2022-01-23
  • 2022-12-23
  • 2021-12-26
  • 2021-10-07
  • 2022-12-23
  • 2022-02-23
  • 2021-12-16
相关资源
相似解决方案