【代码积累】Date split

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


public class Test <T>{
	public void  test()
	{
		/*指定一个日期,获取其UTC时间,然后通过显示出来.
		 * * @param   year    the year minus 1900.
     * @param   month   the month between 0-11.
     * @param   date    the day of the month between 1-31.
     * @param   hrs     the hours between 0-23.
     * @param   min     the minutes between 0-59.
     * @param   sec     the seconds between 0-59.
     * @see     java.util.Calendar
     * @deprecated As of JDK version 1.1,
     * replaced by <code>Calendar.set(year + 1900, month, date,
     * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
     * month, date, hrs, min, sec)</code>.
     * */
		
//		Calendar cal = Calendar.getInstance();
//		cal.set(2016, 8, 25, 15, 51, 55);  /*注意month,是从0开始的,值范围为[0,11]*/
//		
//		long currentsystime = System.currentTimeMillis()/1000;
//		long specifiedtime = cal.getTimeInMillis()/1000;
//		System.out.println("Current system time = "+(int)currentsystime);
//		System.out.println("Sepecified time = "+(int)specifiedtime);
//		
//		/*指定了一个日期,然后根据UTC time,反向获取年月日*/
//		int year = cal.get(Calendar.YEAR);
//		int month = cal.get(Calendar.MONTH);
//		int day = cal.get(Calendar.DAY_OF_MONTH);
//		System.out.println(year+"-"+month+"-"+day);
		
		/*============*/
		long starttime = 0;
		long stoptime = 0;
		List<Timescale> list = null;
		
		Calendar cal = Calendar.getInstance();
		cal.set(2015, 9, 10, 20, 18, 38);
		starttime = cal.getTimeInMillis()/1000;
		
		cal.set(2015, 10, 11, 19, 10, 12);
		stoptime = cal.getTimeInMillis()/1000;
				
		list = timesplit(starttime,stoptime);
		
		System.out.println("list size = "+list.size());
		System.out.println("**********************************");
		System.out.println("**********************************");
		if( 0 < list.size() )
		{
			Iterator<Timescale> it = list.iterator();
			while( it.hasNext() )
			{
				Timescale scale = (Timescale)it.next();
				System.out.println("start time = "+(int)scale.begintime);
				System.out.println("stop time = "+(int)scale.endtime);
				System.out.println("**********************************");
			}
		}
		
//		long tmp = 1444492799;
//		cal.setTimeInMillis(tmp*1000);
//		int year = cal.get(Calendar.YEAR);
//		int month = cal.get(Calendar.MONTH);
//		int day = cal.get(Calendar.DAY_OF_MONTH);
//		int hour = cal.get(Calendar.HOUR_OF_DAY);
//		int minite = cal.get(Calendar.MINUTE);
//		int second = cal.get(Calendar.SECOND);
//		System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minite+":"+second);
	}
	
	private void timesplitImpl(long starttime,long stoptime,List<Timescale> list)
	{	
		/*
		 * 1、将UTC转成日期,取出 2015-9-10 23:59:59:000 的UTC时间,daytime
		   2、比较上述daytime与 stoptime的大小
       stoptime 大:跨天
					则 [starttime,daytime] 作为第一包的起止时间,将[daytime+1,stoptime] 作为下一组入参,		
	   
	   stoptime 小:不跨天,直接下发一包请求*/
		
		Calendar cal = Calendar.getInstance();
		
		cal.setTimeInMillis(starttime*1000);
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		
		/*get the day time*/
		cal.set(year, month, day, 23, 59, 59);  /*取当天结束时刻的UTC time*/
		long daytime = cal.getTimeInMillis()/1000; /*转换成秒*/
		
		/*compare daytime and stoptime*/
		if( daytime < stoptime )
		{
			list.add(new Timescale(starttime,daytime));
			timesplitImpl(daytime+1,stoptime,list);
		}
		else
		{
			list.add(new Timescale(starttime,stoptime));
		}
		
		return;
	}
	
	private List<Timescale> timesplit(long starttime,long stoptime)
	{
		List<Timescale> list = new ArrayList<Timescale>();
		timesplitImpl(starttime,stoptime,list);
		return list;
	}
	
	private class Timescale
	{
		public Timescale(long begintime,long endtime)
		{
			this.begintime = begintime;
			this.endtime = endtime;
		}
		
		long begintime = 0;
		long endtime = 0;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值