当前位置 : 主页 > 编程语言 > c++ >

查询两个时间之间的 月份,日期,小时,分钟,秒(我这里用的是String类型接

来源:互联网 收集:自由互联 发布时间:2021-07-03
查询两个时间之间的 日期 /** * 获取两个日期之间的所有日期(yyyy-MM-dd) * @Description TODO * @param begin * @param end * @return * @author XuJD * @date 2017-4-1 */private static List getBetweenDates(Date begin, Dat
查询两个时间之间的 日期
/**
	 * 获取两个日期之间的所有日期(yyyy-MM-dd)
	 * @Description TODO
	 * @param begin
	 * @param end
	 * @return
	 * @author XuJD
	 * @date 2017-4-1
	 */
	private static List
 
   getBetweenDates(Date begin, Date end) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List
  
    result = new ArrayList
   
    (); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(begin); /* Calendar tempEnd = Calendar.getInstance(); tempStart.add(Calendar.DAY_OF_YEAR, 1); tempEnd.setTime(end); while (tempStart.before(tempEnd)) { result.add(tempStart.getTime()); tempStart.add(Calendar.DAY_OF_YEAR, 1); }*/ while(begin.getTime()<=end.getTime()){ result.add(sdf.format(tempStart.getTime())); tempStart.add(Calendar.DAY_OF_YEAR, 1); begin = tempStart.getTime(); } return result; }
   
  
 
查询两个时间之间的 月份
private static List
 
   getMonthBetween(String minDate, String maxDate) throws ParseException {
	    ArrayList
  
    result = new ArrayList
   
    (); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; while (curr.before(max)) { result.add(sdf.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; }
   
  
 
查询两个时间之间的小时
/**
	 * 获取两个日期之间的有几个小时
	 * @Description TODO
	 * @param begin
	 * @param end
	 * @return
	 * @author XuJD
	 * @date 2017-4-1
	 */
	private static List
 
   getBetweenHours(Date begin, Date end) {
		List
  
    result = new ArrayList
   
    (); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(begin); while(begin.getTime()<=end.getTime()){ result.add(sdft.format(tempStart.getTime()).substring(0,2)); tempStart.add(Calendar.HOUR_OF_DAY, 1); begin = tempStart.getTime(); } return result; }
   
  
 
查询两个时间之间的分钟
/**
	 * 获取两个日期之间有几分钟
	 * @Description TODO
	 * @param begin
	 * @param end
	 * @return
	 * @author XuJD
	 * @date 2017-4-1
	 */
	private static List
 
   getBetweenMinutes(Date begin, Date end) {
		List
  
    result = new ArrayList
   
    (); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(begin); while(begin.getTime()<=end.getTime()){ result.add(sdft.format(tempStart.getTime()).substring(0,5)); tempStart.add(Calendar.MINUTE, 1); begin = tempStart.getTime(); } return result; }
   
  
 
查询两个时间之间的秒钟
/**
	 * 获取两个日期之间有几秒钟
	 * @Description TODO
	 * @param begin
	 * @param end
	 * @return
	 * @author XuJD
	 * @date 2017-4-1
	 */
	private static List
 
   getBetweenSeconds(Date begin, Date end) {
		List
  
    result = new ArrayList
   
    (); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(begin); while(begin.getTime()<=end.getTime()){ result.add(sdft.format(tempStart.getTime())); tempStart.add(Calendar.SECOND, 1); begin = tempStart.getTime(); } return result; }
   
  
 
网友评论