码迷,mamicode.com
首页 > 其他好文 > 详细

获取当前一周的时间戳范围

时间:2017-11-04 11:10:15      阅读:1097      评论:0      收藏:0      [点我收藏+]

标签:str   time   parse   cal   stack   ace   out   --   ring   

    private static Long getCurrentMonday() {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        String time=dateFormat.format(monday);
        System.out.println(time);
        Date date = null;
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }  
        return date.getTime()/1000+1;
    }

 

// 获得当前周--周日的日期

    private static Long getPreviousSunday() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        Date sunday = currentDate.getTime();
        String time= dateFormat.format(sunday);
        System.out.println(time);
        Date date = null;
        try {
            date = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }  
        return date.getTime()/1000+86399;
    }
    
    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天,星期日是第一天,星期一是第二天
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
       if (dayOfWeek == 1) {
            return -6;
        } else {
            return  2- dayOfWeek;
        }
    }

 

获取当前一周的时间戳范围

标签:str   time   parse   cal   stack   ace   out   --   ring   

原文地址:http://www.cnblogs.com/nhz-M/p/7781226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!