时间Date、Long、String

// String to Long
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Long time = 0L;
        try {
            time = format.parse(str).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }


// String to Date
        DateFormat format  = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

// Long to Date
        Date date1 = new Date(1547136000000L);

// Date to Long
        Date date2 = new Date();
        Long time = date2.getTime();

// Long to String
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(date);
        
// Date to String
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(time);

        
        
//获取现在Date:Fri Jan 11 15:22:50 CST 2019
1、 Date currentTime = new Date();

2、 long time = System.currentTimeMillis();
    Date date = new Date(time);
    
    
    /**
     * 计算两个时间相差的天数
     */
    public static int differentDay(Long startTime, Long endTime) {
        return (int) ((endTime - startTime) / (24 * 60 * 60 * 1000L));
    }

    public static int differentDay(Date date1, Date date2) {
        return (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000L));
    }

    public static int differentDay(String str1, String str2, String pattern) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return (int) ((format.parse(str2).getTime() - format.parse(str1).getTime()) / (24 * 60 * 60 * 1000L));
    }

    /**
     * 获取两个日期之间的的所有日期 数组
     */
    public static List<Long> listBetweenDays(Long startTime, Long endTime) {
        int count = differentDay(startTime, endTime) + 1;
        List<Long> list = new ArrayList<>();
        Long time = startTime;
        for (int i = 0; i < count; i++) {
            list.add(time);
            time += (24*60*60*1000L);
        }
        return list;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值