Day14_Date类

Date类

  • Date表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calendar类中的方法所取代。
  • 时间单位
    • 1秒=1000毫秒
    • 1毫秒=1000微秒
    • 1微秒=1000纳秒
package com.oop.Demo11;

import java.util.Date;

public class Demo08 {
    public static void main(String[] args) {
        //1、创建date对象
        Date date1 = new Date ();
        //today
        System.out.println (date1.toString ());
        System.out.println (date1.toLocaleString ());
        //yesterday
        Date date2=new Date (date1.getTime ()-(60*60*24*1000));
        System.out.println (date2.toLocaleString ());
        //方法after、before
        boolean b1=date1.after (date2);
        System.out.println (b1);//true
        boolean b2=date1.before (date2);
        System.out.println (b2);//false
        //比较compareTo();
        /**compareTo()原码
         * public int compareTo(Date anotherDate) {
         *         long thisTime = getMillisOf(this);
         *         long anotherTime = getMillisOf(anotherDate);
         *         return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
         *     }
         *前边的小返回-1,相等返回0;前边的大返回1
         */
        int i=date1.compareTo (date2);
        System.out.println (i);//1
        //比较是否相等equals
        boolean b3=date1.equals (date2);
        System.out.println (b3);//false
    }
}
原文地址:https://www.cnblogs.com/lemonlover/p/14062570.html