java如何获取当前月份有几个周,以及每个周开始和结束的时间

Jzap 2017-11-24 09:02:48
java如何获取当前月份有几个周,以及每个周开始和结束的时间。如果是跨月份的周也算为一周(比如当月1号为周三,也算为一周)。小弟第一次发帖,因为比较着急,有没有大牛给看一下,感谢
...全文
2039 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
繁华终归落尽 2017-11-24
  • 打赏
  • 举报
回复

第1周,开始日期:2017-11-01,结束日期:2017-11-05
第2周,开始日期:2017-11-06,结束日期:2017-11-12
第3周,开始日期:2017-11-13,结束日期:2017-11-19
第4周,开始日期:2017-11-20,结束日期:2017-11-26
第5周,开始日期:2017-11-27,结束日期:2017-11-30
繁华终归落尽 2017-11-24
  • 打赏
  • 举报
回复

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class DateTest {
	private static DateFormat datafFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	public static void main(String[] args) {
		
		Date first = firstMonthDate(new Date());
		Map<Integer,WeekRange> maps = new HashMap<Integer, WeekRange>();
		getWeekBeginAndEnd(1,first,maps);
		
		Set<Integer> set = maps.keySet();
		for(Integer key : set){
			WeekRange range = maps.get(key);
			System.out.println(String.format("第%d周,开始日期:%s,结束日期:%s", key,format(range.getBegin()),format(range.getEnd())));
		}
	}
	
	// 月初
	public static Date firstMonthDate(Date date){
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}
	
	// 月末
	public static Date lastMonthDate(Date date){
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}
	
	// 星期几
	public static int week(Date date){
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK) - 1;
	}
	
	// 下一天
	public static Date nextDate(Date date){
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	
	// 每周开始结束时间
	public static void getWeekBeginAndEnd(int index,Date currentDate,Map<Integer,WeekRange> maps){
		//月末
		Date lastMonthDate = lastMonthDate(currentDate);
		int week = week(currentDate);
		if(null == maps){
			WeekRange range = new WeekRange(currentDate, currentDate);
			maps = new HashMap<Integer, WeekRange>();
			maps.put(index,range);
		}else{
			WeekRange range = maps.get(index);
			if(null == range){
				range = new WeekRange(currentDate);
			}
			range.setEnd(currentDate);
			maps.put(index,range);
		}
		
		if(currentDate.equals(lastMonthDate)){
			return;
		}
		
		if(week == 0){
			index++;
		}
		
		getWeekBeginAndEnd(index,nextDate(currentDate),maps);
	}
	
	public static String format(Date date){
		return datafFormat.format(date);
	}
}

class WeekRange{
	Date begin;//周开始日
	Date end;//周结束日
	public WeekRange(Date begin) {
		super();
		this.begin = begin;
	}
	public WeekRange(Date begin, Date end) {
		super();
		this.begin = begin;
		this.end = end;
	}
	public Date getBegin() {
		return begin;
	}
	public void setBegin(Date begin) {
		this.begin = begin;
	}
	public Date getEnd() {
		return end;
	}
	public void setEnd(Date end) {
		this.end = end;
	}
}

自由自在_Yu 2017-11-24
  • 打赏
  • 举报
回复
研究一下日历就行了,
引用 3 楼 u012934325 的回复:
一个月份五周是死的吧
不是吧?第一天是周日的话,一个月30天,最后一天就是第六周的周一了
墨笙弘一 2017-11-24
  • 打赏
  • 举报
回复
引用 4 楼 qq_39731973 的回复:
[quote=引用 3 楼 u012934325 的回复:] 一个月份五周是死的吧
嗯..可以把几个周写死,那现在就只需要获取每个周的开始时间以及结束时间[/quote] 你的开始时间和结束时间指的是啥啊,开始几号还是周几还是什么鬼
Jzap 2017-11-24
  • 打赏
  • 举报
回复
引用 3 楼 u012934325 的回复:
一个月份五周是死的吧
嗯..可以把几个周写死,那现在就只需要获取每个周的开始时间以及结束时间
墨笙弘一 2017-11-24
  • 打赏
  • 举报
回复
一个月份五周是死的吧

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧