按周期获取时间范围(按年、按月、按天、按小时)
package ham; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TimeTest { /** * @param args * @throws ParseException */ public static void main(String[]args)throws ParseException { getLevelTime("2012-01-01 10:43:07", "2012-01-02 05:23:56", "hour"); } public static void getLevelTime(String startTimeStr, String endTimeStr, String type)throws ParseException { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startTime = dateFormat.parse(startTimeStr); Date endTime = dateFormat.parse(endTimeStr); Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.setTime(startTime); endCal.setTime(endTime); if ("year".equals(type)) { //判断是否在同一年 if (startCal.get(Calendar.YEAR) == endCal.get(Calendar.YEAR)) { System.out.println("同一年内"); } else { //最开始一年最后时刻,月份比正常认知少一个数 Calendar startCal_ = Calendar.getInstance(); startCal_.set(startCal.get(Calendar.YEAR), 11, 31, 23, 59, 59); //最后一年开始时刻 Calendar endCal_ = Calendar.getInstance(); endCal_.set(endCal.get(Calendar.YEAR), 0, 1, 0, 0, 0); //开始时间 System.out.print(dateFormat.format(startCal.getTime()) + "---"); //最开始一年 末尾 System.out.println(dateFormat.format(startCal_.getTime())); //中间年数 int count = (int)(((endCal_.getTime().getTime() - startCal_.getTime().getTime()) / (24 * 60 * 60 * 1000)) / 365); for (int j = 0; j < count; j++) { //第n年 起始 startCal_.set(Calendar.SECOND, 60); System.out.print(dateFormat.format(startCal_.getTime()) + "---"); //第n年 末尾 startCal_.set(Calendar.YEAR, startCal_.get(Calendar.YEAR)); startCal_.set(Calendar.MONTH, 11); // ***月份设置*** startCal_.set(Calendar.DATE, 31); startCal_.set(Calendar.HOUR, 23); startCal_.set(Calendar.MINUTE, 59); startCal_.set(Calendar.SECOND, 59); System.out.println(dateFormat.format(startCal_.getTime())); } //最后一年 开始 System.out.print(dateFormat.format(endCal_.getTime()) + "---"); //结束时间 System.out.println(dateFormat.format(endCal.getTime())); } } if ("month".equals(type)) { //判断是否在同一年 if (startCal.get(Calendar.YEAR) == endCal.get(Calendar.YEAR) && startCal.get(Calendar.MONTH) == endCal.get(Calendar.MONTH)) { System.out.println("同一月内"); } else { //最开始一个月最后时刻,重点在于设置一个月中的天数(每个月的天数不同) Calendar startCal_ = Calendar.getInstance(); startCal_.set(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH), startCal.getActualMaximum(Calendar.DATE), 23, 59, 59); //最后一个月开始时刻 Calendar endCal_ = Calendar.getInstance(); endCal_.set(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH), 1, 0, 0, 0); //开始时间 System.out.print(dateFormat.format(startCal.getTime()) + "---"); //最开始一个月 末尾 System.out.println(dateFormat.format(startCal_.getTime())); //中间月数 int count = (int)(((endCal_.getTime().getTime() - startCal_.getTime().getTime()) / (24 * 60 * 60 * 1000)) / 30); for (int j = 0; j < count; j++) { //第n个月 起始 startCal_.set(Calendar.SECOND, 60); System.out.print(dateFormat.format(startCal_.getTime()) + "---"); //第n个月 末尾 startCal_.set(Calendar.MONTH, startCal_.get(Calendar.MONTH)); //根据具体时间获取其所在月的天数 *** startCal_.set(Calendar.DATE, startCal_.getActualMaximum(Calendar.DATE)); startCal_.set(Calendar.HOUR, 23); startCal_.set(Calendar.MINUTE, 59); startCal_.set(Calendar.SECOND, 59); System.out.println(dateFormat.format(startCal_.getTime())); } //最后一个月 开始 System.out.print(dateFormat.format(endCal_.getTime()) + "---"); //结束时间 System.out.println(dateFormat.format(endCal.getTime())); } } else if ("day".equals(type)) { //判断是否在同一天 if (startCal.get(Calendar.YEAR) == endCal.get(Calendar.YEAR) && startCal.get(Calendar.MONTH) == endCal.get(Calendar.MONTH) && startCal.get(Calendar.DATE) == endCal.get(Calendar.DATE)) { System.out.println("同一天内"); } else { //第一天最后时刻 Calendar startCal_ = Calendar.getInstance(); startCal_.set(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH), startCal.get(Calendar.DATE), 23, 59, 59); //最后一天开始时刻 Calendar endCal_ = Calendar.getInstance(); endCal_.set(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH), endCal.get(Calendar.DATE), 0, 0, 0); //开始时间 System.out.print(dateFormat.format(startCal.getTime()) + "---"); //第一天 末尾 System.out.println(dateFormat.format(startCal_.getTime())); //中间天数 int count = (int)((endCal_.getTime().getTime() - startCal_.getTime().getTime()) / (24 * 60 * 60 * 1000)); for (int j = 0; j < count; j++) { //第n天 起始 startCal_.set(Calendar.SECOND, 60); System.out.print(dateFormat.format(startCal_.getTime()) + "---"); //第n天 末尾 startCal_.set(Calendar.DATE, startCal_.get(Calendar.DATE)); startCal_.set(Calendar.HOUR, 23); startCal_.set(Calendar.MINUTE, 59); startCal_.set(Calendar.SECOND, 59); System.out.println(dateFormat.format(startCal_.getTime())); } //最后一天 开始 System.out.print(dateFormat.format(endCal_.getTime()) + "---"); //结束时间 System.out.println(dateFormat.format(endCal.getTime())); } } else if ("hour".equals(type)) { //判断是否在同一小时 if (startCal.get(Calendar.YEAR) == endCal.get(Calendar.YEAR) && startCal.get(Calendar.MONTH) == endCal.get(Calendar.MONTH) && startCal.get(Calendar.DATE) == endCal.get(Calendar.DATE) && startCal.get(Calendar.HOUR) == endCal.get(Calendar.HOUR)) { System.out.println("同一小时内"); } else { //最开始一小时最后时刻 Calendar startCal_ = Calendar.getInstance(); startCal_.set(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH), startCal.get(Calendar.DATE), startCal.get(Calendar.HOUR_OF_DAY), 59, 59); //最后一小时开始时刻 Calendar endCal_ = Calendar.getInstance(); endCal_.set(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH), endCal.get(Calendar.DATE), endCal.get(Calendar.HOUR_OF_DAY), 0, 0); //开始时间 System.out.print(dateFormat.format(startCal.getTime()) + "---"); //最开始一小时 末尾 System.out.println(dateFormat.format(startCal_.getTime())); //中间小时数 int count = (int)((endCal_.getTime().getTime() - startCal_.getTime().getTime()) / (60 * 60 * 1000)); for (int j = 0; j < count; j++) { //第n个小时 起始 startCal_.set(Calendar.SECOND, 60); System.out.print(dateFormat.format(startCal_.getTime()) + "---"); //第n个小时 末尾 startCal_.set(Calendar.HOUR, startCal_.get(Calendar.HOUR)); startCal_.set(Calendar.MINUTE, 59); startCal_.set(Calendar.SECOND, 59); System.out.println(dateFormat.format(startCal_.getTime())); } //最后一小时 开始 System.out.print(dateFormat.format(endCal_.getTime()) + "---"); //结束时间 System.out.println(dateFormat.format(endCal.getTime())); } } } }
** 几个重点注意的地方:
1.月份设置(根据实际时间所在的月份设置该月的天数)
startCal.getActualMaximum(Calendar.DATE)
2.得到24小时制的小时数值
Calendar date = Calendar.getInstance();
date.get(Calendar.HOUR_OF_DAY );//得到24小时机制的
date.get(Calendar.HOUR);// 得到12小时机制的