【代码积累】Date split

来源:互联网 发布:数据库事务是什么 编辑:程序博客网 时间:2024/06/05 23:42
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;}}

原创粉丝点击