java获取当前时间的前一天时间

public static void main(String[] args) throws Exception
    {
        Date dateNow=new Date();
        Date dateBefore=new Date();
        Calendar cal=Calendar.getInstance();
        cal.setTime(dateNow);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        dateBefore=cal.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String time1=sdf.format(dateBefore)+" 00:00:00";
        Date d1=sdf.parse(time1);
        long startTime=d1.getTime()/1000;
        long endTime=startTime+86399;
        System.out.println("startTime="+startTime);
        System.out.println("endTime="+endTime);
    }
    控制台打印:
    startTime=1497283200
    endTime=1497369599

猜你喜欢

转载自blog.csdn.net/shannon8/article/details/73223820
今日推荐