java Date对象的基本使用

关于Date对象

人的出生年月
用户的注册时间
信息发布时间
这些情况都需要记录日期

构造对象

三种构造方江苏

  • 无参构造
  • 年月日构造
  • 年月日时分秒构造

获取现在时间

        Date date = new Date();
        System.out.println(date);

获取指定的时间 

	Date date = new Date(年,月,日)
	Date date = new Date(年,月,日,时,分,秒)

 

获取信息

年份

默认拿到的年份需要加1900才可

        int year = date.getYear() + 1900;
        System.out.println(year);

月份

        int month = date.getMonth() + 1;
        System.out.println(month);
        int month = date.getMonth() + 1;
        System.out.println(month);

 日期

        int date = now.getDate();
        System.out.println(date);

星期

        int day = now.getDay();
        System.out.println(day);

小时

        int hours = now.getHours();
        System.out.println(hours);

分钟

        int minutes = now.getMinutes();
        System.out.println(minutes);

        int seconds = now.getSeconds();
        System.out.println(seconds);

getTime 获取时间戳

        long time = now.getTime();
        System.out.println(time);

示例 测试程序性能


        Date start = new Date();
        for (int i = 0; i < 10000; i++) {
            System.out.println("hello");
        }
        Date end = new Date();

        long result = end.getTime() - start.getTime();
        System.out.println(result);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值