java 日历工具类(计算周的开始日期和结束日期,以及包好的日期)分析

java哥 阅读:286 2021-03-31 21:13:21 评论:0
package com.zxy.product.training.web.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class WeekUtil {
/** 
     * 获取指定年份日历
     * @param year 
     * @return 
     */  
    private static Calendar getCalendarFormYear(int year){  
        Calendar cal = Calendar.getInstance();  
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);        
        cal.set(Calendar.YEAR, year);  
        return cal;  
    }  
/** 
     * 获取指定年指定周的开始日期
     * @param year 
     * @param weekNo 
     * @return 
     */  
    public static String getStartDayOfWeekNo(int year,int weekNo){  
        Calendar cal = getCalendarFormYear(year);  
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String monthStr = null;
        String dayStr = null;
        StringBuilder monthBuilder = new StringBuilder();
        StringBuilder dayBuilder = new StringBuilder();
        if(month < 10){
        monthStr = monthBuilder.append("0").append(month).toString();
        } else {
        monthStr = monthBuilder.append(month).toString();
        }
        if(day < 10) {
        dayStr = dayBuilder.append("0").append(day).toString();
        } else {
        dayStr = dayBuilder.append(day).toString();
        }
        return cal.get(Calendar.YEAR) + "-" + monthStr + "-" + dayStr;     
          
    }  
      
    /** 
     * 获取指定年指定周的结束日期
     * @param year 
     * @param weekNo 
     * @return 
     */  
    public static String getEndDayOfWeekNo(int year,int weekNo){  
        Calendar cal = getCalendarFormYear(year);  
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);  
        cal.add(Calendar.DAY_OF_WEEK, 6); 
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String monthStr = null;
        String dayStr = null;
        StringBuilder monthBuilder = new StringBuilder();
        StringBuilder dayBuilder = new StringBuilder();
        if(month < 10){
        monthStr = monthBuilder.append("0").append(month).toString();
        } else {
        monthStr = monthBuilder.append(month).toString();
        }
        if(day < 10) {
        dayStr = dayBuilder.append("0").append(day).toString();
        } else {
        dayStr = dayBuilder.append(day).toString();
        }
        return cal.get(Calendar.YEAR) + "-" + monthStr + "-" +  dayStr;      
    }  


    //获取 某日是某年的第几周
    public static int getWeekByDay(String day) throws ParseException {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         Calendar calendar = Calendar.getInstance();
         calendar.setFirstDayOfWeek(Calendar.SUNDAY);
         calendar.setTime(format.parse(day));
        return calendar.get(Calendar.WEEK_OF_YEAR);
  }
    
    //获取 指定开始日期和结束日期,包含日期列表。
   public static List<String> getDays(String beginDate, String endDate) throws ParseException {
  List<String> result = new ArrayList<String>();
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       Calendar calendar = Calendar.getInstance();  
       calendar.setTime(format.parse(beginDate));  
 
       for (long  time= calendar.getTimeInMillis(); time <= format.parse(endDate).getTime(); time = getNextDay(calendar)) {  
      result.add(format.format(time));  
       }  
  return result;
   }
   
   public static long getNextDay(Calendar calendar) {  
  calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);  
       return calendar.getTimeInMillis();  
   } 
    
  /**
 * @param args
 * @throws ParseException
 */
public static void main(String[] args) throws ParseException{
 String day = "2016-12-31";
int week = WeekUtil.getWeekByDay(day);

System.out.println("第"+week+"周");
System.out.println("第"+week+"周开始日期:"+WeekUtil.getStartDayOfWeekNo(2016, week));
System.out.println("第"+week+"周结束日期:"+WeekUtil.getEndDayOfWeekNo(2016, week));

String beginDate = WeekUtil.getStartDayOfWeekNo(2016, week);
String endDate = WeekUtil.getEndDayOfWeekNo(2016, week);

WeekUtil.getDays(beginDate,endDate).stream().forEach(item -> System.out.println("日期:"+item));


  }


}
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号