java 根据当天时间 获取前7天之间的时间 和后多少天的查询时间,获娶近几周 周一的日期

java 根据当天时间 获取前7天之间的时间  和后多少天的查询时间

package com.kugou.schedu.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kugou.schedu.dao.SongHotDao;


@Component
public class SongHotService {

	

	public static void main(String[] args) throws ParseException {
		getDayBetween(7, 7);
	}

	/**
	 * 根据当前时间 获取 开始时间 到结束时间
	 * 
	 * @param day
	 *            获取几天之间的时间
	 * @return 开始时间 and 结束时间
	 */
	public static Date[] getDayBetween(Integer day) {

		// 今天整天日期
		Date startDt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(startDt.getTime());
		try {
			startDt = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = new GregorianCalendar();
		calendar.setTime(new Date(startDt.getTime() - 1));
		calendar.add(calendar.DATE, day);

		// Long endDt = 604800L + startDt.getTime();

		System.out.println(startDt);
		System.out.println(calendar.getTime());
		return new Date[] { startDt, calendar.getTime() };
	}

	/**
	 * 根据当前时间 获取 开始时间 到结束时间
	 * 
	 * @param day
	 *            获取几天之间的时间
	 * 
	 * @param otherDay
	 *            从前几天 开始
	 * 
	 * @return 开始时间 and 结束时间
	 */
	public static Date[] getDayBetween(Integer day, Integer otherDay) {

		// 今天整天日期
		Date startDt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(startDt.getTime());
		try {
			startDt = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar calendarStart = new GregorianCalendar();
		calendarStart.setTime(startDt);
		calendarStart.add(calendarStart.DATE, -7);
		startDt=calendarStart.getTime();

		Calendar calendar = new GregorianCalendar();
		calendar.setTime(new Date(startDt.getTime() - 1));
		calendar.add(calendar.DATE, 7);

		System.out.println(startDt);
		System.out.println(calendar.getTime());
		return new Date[] {startDt,calendar.getTime()};
	}

}
/**
 * output:
   Fri Apr 17 00:00:00 CST 2015
   Thu Apr 23 23:59:59 CST 2015
 */

/**
	 * 获娶近几周 周一的日期
	 * @param weekj
	 * @return
	 * @throws ParseException
	 */
	public static long getWeekDayTime(int weekj) throws ParseException{
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
		long now=System.currentTimeMillis();
		Calendar cal = Calendar.getInstance();
		int a = cal.get(Calendar.DAY_OF_WEEK) - 2;//-1
		int today = weekj * 7 + a;// 前三周的时间点
		long xx=today*86400000L;
	
		String strDate=sdf2.format(new Date(now-xx));
		Date date=sdf2.parse(strDate);
		return date.getTime();
		
		//return getHoruTime(weekj);
	}
	
	/**
	 * 当前小时
	 * 获娶近几小时  测试用    
	 * @param weekj
	 * @return
	 * @throws ParseException
	 */
	public static long getHoruTime(int hour) throws ParseException{
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH");
		long now=System.currentTimeMillis();
		String strDate=sdf2.format(new Date(now));
		Date date=sdf2.parse(strDate);
		long chour= date.getTime();
		hour=hour*60*60*1000;
		return chour-hour;
	}
	
	public static void main(String[] args) throws ParseException {
		
		long cuh=getWeekDayTime(3);
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(cuh);
        
        sdf2.format(cuh);
        System.out.println(sdf2.format(cuh));
		
	}


你可能感兴趣的:(工具)