java获取时间的处理

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;




public class TestCalendar {
	
	public static void main(String[] args) {
		String [] queryMonthTimes = getMonthBenginTimeAndEndTime("2011-12");
		System.out.println(queryMonthTimes[0] + "---------" + queryMonthTimes[1]);
		
		String [] queryYearTimes = getWeekBenginTimeAndEndTime("2011-11",4);
		System.out.println(queryYearTimes[0] +  "---------" + queryYearTimes[1]);
		
		System.out.println(getWeekCount("2011-05"));
		
		Object [] defaultTimes = getBeginTimeAndEndTimeAndDeFaultTime();
		System.out.println(defaultTimes[0]+ "------" + defaultTimes[1] + "-------" + defaultTimes[2] + "-------" + defaultTimes[3]);
	}
	
	
	/**
	 * 获取指定月份的起始时间和结束时间
	 * @param 指定的时间字符串
	 */
	public static String[] getMonthBenginTimeAndEndTime(String queryTime){
		String [] time  = new String [2];
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTimeInMillis(sdf.parse(queryTime).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String benginTime = queryTime + "-" + calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
		String endTime = queryTime + "-" + calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		time[0] = benginTime;
		time[1] = endTime;
		
		return time;
	}
	
	/**
	 * 获取指定年的起始时间和结束时间
	 * @param 指定的时间字符串  格式为:yyyy-MM
	 * @param 周的索引
	 */
	public static String[] getWeekBenginTimeAndEndTime(String queryTime,Integer weekIndex){
		String [] time  = new String [2];
		
		SimpleDateFormat weekFormat = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		try {
			calendar.setTimeInMillis(weekFormat.parse(queryTime).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.set(Calendar.DAY_OF_MONTH,1);
		
		//获取查询月的第一个星期一的时间
		if(calendar.get(Calendar.DAY_OF_WEEK) == 1){
			calendar.add(Calendar.DAY_OF_MONTH, 1);
		}
		else if(calendar.get(Calendar.DAY_OF_WEEK) != 2){
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			calendar.add(Calendar.DAY_OF_MONTH, 9-dayOfWeek);
		}
		
		//根据查询的周数进行日期的7的倍数的回滚以至于得到查询周数的起始时间
		calendar.add(Calendar.DAY_OF_MONTH, (weekIndex-1)*7);
		String beginTime = dayFormat.format(calendar.getTime());
		
		//查询周数的结束时间
		calendar.add(Calendar.DAY_OF_MONTH, 6);
		String endTime = dayFormat.format(calendar.getTime());


		time[0] = beginTime;
		time[1] = endTime;
		
		return time;
	}
	
	/**
	 * 根据指定的时间获取当前月份有多少周
	 * @param 指定的时间字符串 格式:yyyy-MM
	 */
	public static int getWeekCount(String queryTime){
		SimpleDateFormat weekFormat = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		try {
			calendar.setTimeInMillis(weekFormat.parse(queryTime).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.set(Calendar.DAY_OF_MONTH,1);
		int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		int dayOfWeek = 0;
		//获取当前月份的第一天是星期几
		if(2 != calendar.get(Calendar.DAY_OF_WEEK)){
			dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) -1;
		}
		dayOfMonth = dayOfMonth - dayOfWeek;
		//获取当前月的最后一天的日期
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		if(1 != calendar.get(Calendar.WEEK_OF_YEAR)){
			dayOfWeek = 8-calendar.get(Calendar.DAY_OF_WEEK);
		}
		dayOfMonth = dayOfMonth + dayOfWeek;
		
		return dayOfMonth/7;
	}
	
	/**
	 * 获取当前的时间的周的开始时间和结束时间,以及月份的默认时间
	 * @return 时间的数组:第一个默认年月、第二个当前周数、第三个周的开始时间、第四个周的结束时间
	 */
	public static Object[] getBeginTimeAndEndTimeAndDeFaultTime(){
		Object[] time = new Object[4];
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 31);
		
		//获取当前时间在本月处于第几周
		int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
		SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
		String queryTimes[] = null;
		String queryTime = null;
		if(weekOfMonth == 1){
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
			if(dayOfWeek != 1){
				//获取上个月的周数
				calendar.add(Calendar.DAY_OF_MONTH, -1);
				queryTime = monthFormat.format(calendar.getTime());
				weekOfMonth = getWeekCount(queryTime);
				queryTimes = getWeekBenginTimeAndEndTime(queryTime,weekOfMonth);
			}
			else{
				queryTime = monthFormat.format(calendar.getTime());
				queryTimes = getWeekBenginTimeAndEndTime(queryTime,1);
			}
		}
		else{
			queryTime = monthFormat.format(calendar.getTime());
			queryTimes = getWeekBenginTimeAndEndTime(queryTime,weekOfMonth);
			
		}
		time[0] = queryTime;
		time[1] = weekOfMonth;
		time[2] = queryTimes[0];
		time[3] = queryTimes[1];
		return time;
	}
	
	
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值