第二章节 Date类

目录

1.获取系统当前时间三种方式

2.创建date日期三种方式

3建立系统前七天时间 以及自己活了多少天

4.gettime 方法

5.Date与时间戳Caler类转化


1.获取系统当前时间三种方式


        long ee = System.currentTimeMillis();  //获得的是时间戳
        System.out.println(ee);
        System.out.println(System.currentTimeMillis());
//获得当前计算机系统时间
        System.out.printf("%tF %<tT %<tA%n",System.currentTimeMillis());
        System.out.printf("%tF %<tT %<tA%n",new Date());
        System.out.printf("%tF %<tT %<tA%n",new Date().getTime());

2.创建date日期三种方式

//建立制定日期Date
        //年份 – 年份减去 1900。月 – 0-11 之间的月份。日期 – 1-31 之间的月份中的某一天
        //1999 10 30 

        Date dd = new Date();
        dd.setYear(1999-1900); //-1900
        dd.setMonth(10-1); /-1
        dd.setDate(30);

        Date dd = new Date("1999/10/30 00:00:00");  指定年月日字符串格式建立对象
        Date dd = new Date(1999-1900,10-1,30,00,0,00); //指定年月日 建立日期对象

3建立系统前七天时间 以及自己活了多少天

//建立当前系统的七天前日期
        System.out.printf("%tF %<tT %<tA%n",ee);
        System.out.printf("%tF %<tT %<tA%n %n" ,ee - (1000*3600*24*7L));
        System.out.printf("%tF %<tT %<tA%n %n" ,ee + (1000*3600*24*10L));
        System.out.println("--------------");
        //计算一下自己活了多少天
        long birthday =  dd.getTime();
        long days = (ee - birthday) /1000 /3600 /24 ;
        System.out.println(days);
        System.out.printf("%tF 到 %tF 之间有 %d 天%n", ee, birthday, days);

4.gettime 方法

Date date = new Date();
 long aa = date.getTime();//date的gettime是转为时间戳

5.Date与时间戳Caler类转化

//Date转为时间戳
Date a = new Date();
        long b = a.getTime();
        System.out.println(b);

//时间戳转为Date
 Date a = new Date(6000000);

//Date转为Calendar
 Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值