日期相关

本文详细介绍了Java中日期和时间的处理方法,包括使用Date类获取当前系统时间、比较日期前后,以及使用SimpleDateFormat进行日期格式化。同时,还探讨了Calendar类的使用,如获取年月日等信息。
package myutil;



import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
  *@auther WangZhi
  *@date 2019/8/18 21:56
  */
  
public class TestDate {

    public static void main(String[] args) {
        //返回当前系统时间与计算机元年之间的毫秒差
        //1970-1-1
        long time = System.currentTimeMillis();
        System.out.println(time);//1566137168796

        /*
         日期相关
            Date类
            1.通常使用的是 java.Util包
            2.导包 直接使用 构建对象
            3.通常使用无参数的构造方法 或者带long构造方法
            4.Date类中常用的方法
                before(); after();
                setTime(); getTime();
                compareTo();
            5.处理Date日期格式
                DateFormat
                1.java.text包
                2.抽象类 不能创建对象 子类来使用
                3.SimpleDateFormat类 是DateFormat的子类
         **/
        Date date = new Date();//默认当前系统时间
        Date date1 = new Date(1566137168796L);
        System.out.println(date);
        Boolean b = date.before(date1);
        System.out.println(b);//false


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String s = df.format(date);
        System.out.println(s);//2019-08-18 10:26:33


        /*
         Calendar类
            1.java.util包
            2.有构造方法 用protected修饰的 通常访问不到 通常会调用默认的
                getInstance();
         **/
        Calendar calendar = Calendar.getInstance();//系统当前时间的calendar对象
        System.out.println(calendar.after(date1));//false
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值