日期操作类(Date类)

在之前一直在编写简单Java类,但是所编写的数据表与简单Java类的转换里面缺少了Date日期的转换。
在Java里面提供有一个java.util.Date类取得当前日期时间。

范例:取得当前的日期时间

public class TestDemo{
    
    public static void main(String[] args) throws Exception{
        Date date=new Date();
        System.out.println(date);
    }   


}
9003228-bff20f75f33f0692.png
image.png

long可以描述出日期时间数据,Date类中提供如下重要方法:
无参构造:public Date()
有参构造:public Date(long date)//接收long型数据

Date类型转换为long型:public long getTime();

范例:Date与long型转换。

public class TestDemo{
    
    public static void main(String[] args) throws Exception{
        long cur=System.currentTimeMillis();
        Date date=new Date(cur);
        System.out.println(date);
        System.out.println(date.getTime());
    }   


}
9003228-3f5142b96ee9447b.png
image.png

以后的代码编写过程中,依然需要以上转换操作,尤其是getTime方法。

猜你喜欢

转载自blog.csdn.net/weixin_33919950/article/details/87523559