JAVA 日期处理

// 文件路径 D:ApacheServerweb_javaHelloWorldsrccom	estTestDate.java
package com.test;

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

public class TestDate {

    public void testfun(){
        
        Date date = null;
        // 构造函数初始化当前日期和时间的对象
        date = new Date();
        // 或者构造指定日期时间的对象。参数为毫秒时间戳,示例时间为 2019/8/18 16:12:56
        date = new Date(1566115976000L);
        // date 对应的时间戳大于 after() 的参数所对应时间戳则返回 true,否则返回 false。这里返回 false
        if(date.after(new Date())) {
            System.out.println("date对应时间大于 after 参数对应时间");
        }else {
            System.out.println("date对应时间小于 after 参数对应时间");
        }
        // 判断方式与 date.after() 相反
        boolean isBefore = date.before(new Date());
        // 克隆一个时间对象,但返回值类型为 Object
        Object objDate = date.clone();
        // 如果两个 date 对象时间戳相等,返回0,如果 date 大于 compareTo() 里参数的时间,则返回正数,否则返回负数,这里结果为 -1
        int compare = date.compareTo(new Date());
        System.out.println("对比时间结果为 : " + compare);
        // 两日期对象对应时间戳相等返回 true,否则返回 false,这里返回 true
        boolean equals = date.equals(new Date(1566115976000L));
        // 获取当前 date 对应时间戳
        long stampLong = date.getTime();
        System.out.println("当前 date 时间戳为 : " + stampLong);
        // 返回该日期对象的哈希码值
        int hashCode = date.hashCode();
        // 设置 date 对象对应的时间戳
        date.setTime(1566119343000L);
        // 将 date 对象转换为字符串,这里返回为 Sun Aug 18 17:09:03 CST 2019
        String strDate = date.toString();
        System.out.println("当前 date 字符串为 : " + strDate);
        // 创建日期格式化对象,y 年,M 月,d 月中第几日,H 小时(24制),h 小时(12制), m 分,s 秒,S 毫秒,E 星期几,D 年中第几日,F 月中第几周,w 年中第几周,W 月中的第几周,a 上午下午,k 天中第几小时,K 带有 A.M./P.M. 的小时(0~11),z 时区
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        String strFormat = dateFormat.format(date);
        //这里输出日期为 2019-08-18 17:09:03
        System.out.println("格式化后时间为 : " + strFormat);
        
        try {
            //获取指定日期时间格式字符串的毫秒时间戳
            stampLong = dateFormat.parse("2020-04-30 23:59:59").getTime();
            //打印内容 2020-04-30 23:59:59毫秒时间戳为 : 1588262399000
            System.out.println("2020-04-30 23:59:59毫秒时间戳为 : " + stampLong);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
        
        
        
        //获取加一天时间对象(即日期加一天的时间对象)
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        date = calendar.getTime();
        //打印内容为 隔天 date 字符串为 : Mon Aug 19 17:09:03 CST 2019
        System.out.println("隔天 date 字符串为 : " + date.toString());
        
        //当天0点时间戳
        calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        //第一个 getTime()方法为Calendar对象所有,获取Date对象,第二个getTime()为Date对象所有,获取对应时间戳
        stampLong = calendar.getTime().getTime();
        //打印内容 当日0点时间戳为 : 1566144000000
        System.out.println("当日0点时间戳为 : " + stampLong);
    }
}
原文地址:https://www.cnblogs.com/dreamhome/p/11486685.html