java获取昨天0点时间和今天0点时间

之前timeStamp / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 3 * 60 * 60 * 1000;根据评论中反馈,0-1获取时间有问题,还有时区问题,目前使用Calendar修改,获取今天零点至昨日零点时间,已封装在方法中

 public static Map<String, String> ZeroTime() {
        SimpleDateFormat Spdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        Date zero = cal.getTime();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)-1, 0, 0, 0);
        Date yzero  = cal.getTime();
        String today =Spdate.format(zero);
        String yesterday = Spdate.format(yzero);
        Map<String, String> map = new HashMap<>();
        map.put("today", today);
        map.put("yesterday", yesterday);
        return map;
    }

日期格式为字符串

2020-5-20 00:00:00
2020-5-21 00:00:00

当mybatis操作数据库时
数据库时间格式为datetime类型等可以直接传入使用

你可能感兴趣的:(Java)