java中Date类测试总结

import java.util.Date;

public class DateStu {
 /*Date过时的时间类,包含了很多过时的方法,不建议使用;但是有些功能暂时没有替代品
  *api:
  *1.构造器:(),(long date)根据从1970年1月1日 0时0分0秒到指定时间的毫秒数生成对象
  *2.getTime():获取当前对象时间与元年的毫秒数差
  *3.setTime(long time) :设置当前date对象的时间距离元年的毫秒数
  */
 public static void main(String[] args)
 {
  demo2();
 }
 //构造器
 public static void demo1(){
  Date date = new Date();//默认本地当前时间
  System.out.println(date.toLocaleString());
 }
 //getTime,after,before
 public static void demo2(){
  Date date = new Date();
  System.out.println(date.getTime());
  //生成昨天的时间
  long num = 24*60*60*1000;
  long now = date.getTime();
  long yesDay = now-num;
  //生成昨天的时间
  Date date1 = new Date(yesDay);
  System.out.println(date1.toLocaleString());
  //昨天的时间,和今天的时间比较
  System.out.println(date.after(date1));
  System.out.println(date.before(date1));
  //建立前天的时间
  date1.setTime(now-num*2);
  System.out.println(date1.toLocaleString());
 }
}

一个不错的java项目:JAVA WEB 项目教程-模仿天猫整站 J2EE版

猜你喜欢

转载自blog.csdn.net/wanghuiwei888/article/details/78857992