JAVA中的Date类

java中的Date类

JDK 8 中,Date类只有两个构造方法是可以使用的

  • Date(); 用来创建当前日期时间的Date对象
  • Date(long date); 用于创建指定时间的Date对象,其中date参数表示1970年1月1日0时0分0秒(称为历元)以来的毫秒数,即时间戳。1s = 1000ms,与当前所在时区有关,亚洲大概8点。
  • Date(int year,int month,int date); 指定创建的日期对象的年月日
  • Date(int year,int month,int date,int hrs,int min); 指定创建的日期对象的年月日时分

成员方法:

  • public String toLocalString();

    返回本地的格式日期信息

  • public boolean after(Date when);

    测试此日期是否在指定日期之后,

    返回:当此对象表示的时间比when表示的时间晚,才返回ture,否则返回false

  • public boolean before(Date when);

    测试此日期是否在指定日期之前

    返回:当此对象表示的时间比when表示的早,返回ture,否则返回false

  • public int compareTo(Date anotherDate);

    比较两个日期的顺序

    返回:当此对象表示的时间与anotherDate的时间相等,则返回0

    此对象的时间在anotherDate之前则返回小于0的值

    否则返回大于0的值

  • public void setTime(long time);

    设置此Date对象的时间,以表示1970连1月1日00:00:00 GET 以后time毫秒的时间点

    与当前所在时区有关

    例:time = 1000 ;

    则表示的是 1970连1月1日00:00:01 GET

import java.util.Date;
public class Item{
    public static void main(String[] args){
        Date now = new Date();
        Date time = new Date();
        
        System.out.println(now.toLocalString());
        System.out.println(time);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值