日期处理工具类 -【二】

1、返回本周的第一天(周日为每周第一天)

 1 /**
 2  * 返回本周的第一天(周日为每周第一天)
 3  * @return
 4  */
 5 public static String getTheFirstDayOfThisWeek(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
 9     cal.add(Calendar.WEEK_OF_YEAR, 0);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

2、返回上周的第一天(周日为每周第一天)

 1 /**
 2  * 返回上周的第一天(周日为每周第一天)
 3  * @return
 4  */
 5 public static String getTheFirstDayOfLastWeek(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
 9     cal.add(Calendar.WEEK_OF_YEAR, -1);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

 3、返回上周的最后一天(周六为每周的最后一天)

 1 /**
 2  * 返回上周的最后一天(周六为每周的最后一天)
 3  * @return
 4  */
 5 public static String getTheLastDayOfLastWeek(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
 9     cal.add(Calendar.WEEK_OF_YEAR, -1);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

4、返回本月的第一天

 1 /**
 2  * 返回本月的第一天
 3  * @return
 4  */
 5 public static String getTheFirstDayOfThisMonth(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cal = Calendar.getInstance();
 8     cal.add(Calendar.MONTH, 0);
 9     cal.set(Calendar.DAY_OF_MONTH, 1);
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

5、返回上个月第一天

 1 /**
 2  * 返回上个月第一天
 3  * @return
 4  */
 5 public static String getTheFirstDayOfLastMonth(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
 7     Calendar cal = Calendar.getInstance();//获取当前日期 
 8     cal.add(Calendar.MONTH, -1);
 9     cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期即为本月第一天 
10     String firstDay = format.format(cal.getTime());
11     return firstDay;
12 }

6、返回上个月最后一天

 1 /**
 2  * 返回上个月最后一天
 3  * @return
 4  */
 5 public static String getTheLastDayOfLastMonth(){
 6     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 7     Calendar cale = Calendar.getInstance();   
 8     cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天 
 9     String lastDay = format.format(cale.getTime());
10     return lastDay;
11 }

7、返回今年的第一天(即当前年份的1月1日)

 1 /**
 2  * 返回今年的第一天(即当前年份的1月1日)
 3  * @return
 4  */
 5 public static String getTheFirstDayOfThisYear(){
 6     Calendar calendar = Calendar.getInstance();  
 7     calendar.clear();
 8     String year = toString(new Date(), "yyyy");
 9     calendar.set(Calendar.YEAR, Integer.valueOf(year));  
10     Date currYearFirst = calendar.getTime();
11     String firstDay = toString(currYearFirst, "yyyy-MM-dd");
12     return firstDay;
13 }

8、返回昨天的日期

 1 /**
 2  * 返回昨天的日期
 3  * @return
 4  */
 5 public static String getYesterdayDate(){
 6     Calendar cal = Calendar.getInstance();
 7     cal.add(Calendar.DATE, -1);
 8     String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
 9     return yesterday;
10 }
posted @ 2016-05-17 15:25  MR.Huangjw  阅读(177)  评论(0编辑  收藏  举报