获取当天开始和结束时间戳

/**
     * 获取今天开始时间戳
     *
     * @return
     */
    public Long getNowDayStartTime() {
        String startTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 获取当天结束时间
     *
     * @return
     */
    public Long getNowDayEndTime() {
        String endTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:59");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

public static void main(String[] args)
            throws ParseException{
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        Date parse = df.parse(df.format(calendar.getTime()));
        System.out.println("获取Parse:" + parse);
        System.out.println("明天的日期为:" + df.format(calendar.getTime()));
        calendar.add(Calendar.DATE, -2);
        System.out.println("昨天的日期为:" + df.format(calendar.getTime()));
    }

你可能感兴趣的:(java,java,开发语言)