`

DateUtils

阅读更多
DateUtils




import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;


public class DateUtils {
	private static Map<String, DateFormat> formaters = null;
	private static final int YEARS = 365;
	private static final int TWODAYS = 2;
        // yyyy-mm-dd hh:mm:ss 格式化
	private static DateFormat getDateFormater(String format) {
		if (formaters == null) {
			formaters = new HashMap<String, DateFormat>();
		}
		DateFormat formater = formaters.get(format);
		if (formater == null) {
			formater = new SimpleDateFormat(format);
			formaters.put(format, formater);
		}
		return formater;
	}

	public static Date parse(String strDate, String format) {
		try {
			return getDateFormater(format).parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String format(Date date, String format) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		return getDateFormater(format).format(date);
	}

	public static String format(String strDate, String srcFormat, String destFormat) {
		Date date = parse(strDate, srcFormat);
		return format(date, destFormat);
	}

	public static java.sql.Date createSqlDate(int year, int month, int date) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, date);
		return new java.sql.Date(calendar.getTimeInMillis());
	}

	public static Date createSqlDate(Date date) {
		return new java.sql.Date(date.getTime());
	}

	public static Date toStartOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Date toEndOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}
	
	public static Date toMidOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 12);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}
	
	public static Date getDateOfeveryTime(int hour, int minute, int second,
			int milliSecond) {

		Calendar cal = Calendar.getInstance();

		cal.set(Calendar.HOUR_OF_DAY, hour);

		cal.set(Calendar.SECOND, second);

		cal.set(Calendar.MINUTE, minute);

		cal.set(Calendar.MILLISECOND, milliSecond);

		return cal.getTime();

	}

	public static boolean compareDate(Date date1, Date date2) {
		int n = date1.compareTo(date2);
		if (n < 0) {
			return true;
		} else {
			return false;
		}
	}

	public static Date addDays(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, days);
		return cal.getTime();
	}
	/**
	 * 计算两个时间相差的月份
	 * @param start 开始时间
	 * @param end 结束时间
	 * @return
	 */
	public static double calculateMonthIn(Date start, Date end) {
		double result = 0.0d;
		if (start.after(end)) {
			return 0;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);
		  
		int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
		int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
		// 如果计息时间不是发标第二天,天数需要修改
		int day = endCalendar.get(Calendar.DAY_OF_MONTH) - startCalendar.get(Calendar.DAY_OF_MONTH);
		// 月间隔计算
		result = year * 12 + month -1;
		// 月精算
		if(day >0) {
			// 如:结束月2014.10.11   开始月2014.8.8  间隔2.1个月
			result = NumberUtils.add(result, 1);
			double resultMonthIn = NumberUtils.div(day, 30);
			result = NumberUtils.add(result, resultMonthIn);
		}else {
			//如:结束月2014.10.8   开始月2014.8.11  间隔1.9个月
			double dayOfMonth = NumberUtils.add(30, day);
			double resultMonthIn = NumberUtils.div(dayOfMonth, 30);
			result = NumberUtils.add(result, resultMonthIn);
		}
		return result;
	}
	/**
	 * 计算两个时间相差的月份,根据天数>=15天,四舍五入
	 * 
	 * @param start 开始时间
	 * @param end 结束时间
	 * @return
	 */
	public static double getMonth(Date start, Date end) {
		if (start == null) {
			start = new Date();
		}
		if (end == null) {
			end = new Date();
		}
		if (start.after(end)) {
			Date t = start;
			start = end;
			end = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);
		Calendar temp = Calendar.getInstance();
		temp.setTime(end);
		temp.add(Calendar.DATE, 1);

		int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
		int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
		int day = endCalendar.get(Calendar.DAY_OF_MONTH) - startCalendar.get(Calendar.DAY_OF_MONTH);

		double result = 0.0d;
		if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
			result = year * 12 + month + 1;
		} else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
			result = year * 12 + month;
		} else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
			result = year * 12 + month;
		} else {
			result = (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
		}

		if (day >= 15) {
			result = result + 1;
		} else if (day > 0 && day < 15) {
			result = result + 0.5;
		}
		return result;
	}

	/**
	 * 计算两个时间相差的年,
	 * 
	 * @param start 开始时间
	 * @param end 结束时间
	 * @return
	 */
	public static int dateDiffYear(Date start, Date end) {
		if (start == null) {
			start = new Date();
		}
		if (end == null) {
			end = new Date();
		}
		if (start.after(end)) {
			Date t = start;
			start = end;
			end = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);

		int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);

		return year + 1;
	}
	
	
	/**
	 * 计算两个时间相差的天,
	 * 
	 * @param start 开始时间
	 * @param end 结束时间
	 * @return
	 */
	public static int dateDiffDay(Date start, Date end) {
		if (start == null) {
			start = new Date();
		}
		if (end == null) {
			end = new Date();
		}
		if (start.after(end)) {
			Date t = start;
			start = end;
			end = t;
		}
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(start);
		long startTime = startCalendar.getTimeInMillis();   
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(end);
		long endTime = endCalendar.getTimeInMillis();   
		long between_days=(endTime-startTime)/(1000*3600*24);  
	   return Integer.parseInt(String.valueOf(between_days));         
	}

	/**
	 * 计算日期相差天数,小时,分
	 * 
	 * @param startTime
	 * @param endTime
	 * @param format
	 * @return
	 */
	public static String dateDiffDay(String startTime, String endTime, String format) {
		String result = "";
		if (StringUtils.isEmpty(endTime)) {
			return "截至日期待定";
		}
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数
		long diff;
		try {
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			long day = diff / nd;// 计算差多少天
			long hour = diff % nd / nh;// 计算差多少小时
			long min = diff % nd % nh / nm;// 计算差多少分钟
			long sec = diff % nd % nh % nm / ns;// 计算差多少秒
			//如果天为负数
			if(day < 0 || hour < 0 || min < 0){
				result = "0天0小时0分";
			}else{
				result = day + "天" + hour + "小时" + min + "分";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 计算日期相差秒
	 * 
	 * @param startTime
	 * @param endTime
	 * @param format
	 * @return
	 */
	public static long dateDiffSec(Date startTime, Date endTime) {
		long ret = 0;
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数
		long diff;
		try {
			diff = endTime.getTime() - startTime.getTime();
			long min = diff % nd % nh / nm;// 计算差多少分钟
			long sec = diff % nd % nh % nm / ns;// 计算差多少秒
			ret = min*60 + sec;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	/**
	 * 计算日期相差小时
	 * 
	 * @param startTime
	 * @param endTime
	 * @param format
	 * @return
	 */
	public static Long dateDiffDayOfHour(String startTime, String endTime, String format) {
		long result = 0;
		
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数
		long diff;
		try {
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			long hour = diff % nd / nh;// 计算差多少小时
			result = hour;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 计算日期相差小时
	 * 
	 * @param startTime
	 * @param endTime
	 * @param format
	 * @return
	 */
	public static Long dateDiffDayOfHour(Date startTime, Date endTime) {
		long result = 0;
		
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long diff;
		try {
			diff = endTime.getTime() - startTime.getTime();
			long hour = diff % nd / nh;// 计算差多少小时
			result = hour;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public static Date createDate(int year) {
		return createDate(year, 1);
	}

	public static Date createDate(int year, int month) {
		return createDate(year, month, 1);
	}

	public static Date createDate(int year, int month, int day) {
		return createDate(year, month, day, 0);
	}

	public static Date createDate(int year, int month, int day, int hour) {
		return createDate(year, month, day, hour, 0);
	}

	public static Date createDate(int year, int month, int day, int hour, int minute) {
		return createDate(year, month, day, hour, minute, 0);
	}

	public static Date createDate(int year, int month, int day, int hour, int minute, int second) {
		return createDate(year, month, day, hour, minute, second, 0);
	}
	
	public static Date createDate(int year, int month, int day, int hour, int minute, int second, int milliSecond) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, milliSecond);
		
		return calendar.getTime();
	}

	/**
	 * 取得当前月的开始时间
	 * 
	 * @return 当前月的开始时间
	 */
	public static Date getBeginDateOfCurrentMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		return calendar.getTime();
	}

	/**
	 * 取得当前月的结束时间
	 * 
	 * @return 当前月的结束时间
	 */
	public static Date getEndDateOfCurrentMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		
		return calendar.getTime();
	}

	/**
	 * 取得下一个月的开始时间
	 * 
	 * @return 下一个月的开始时间
	 */
	public static Date getBeginDateOfNextMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		return calendar.getTime();
	}

	/**
	 * 取得下一个月的结束时间
	 * 
	 * @return 下一个月的结束时间
	 */
	public static Date getEndDateOfNextMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 2);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		
		return calendar.getTime();
	}

	public static Date getBeginDateOfCurrentYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		return calendar.getTime();
	}

	public static Date getEndDateOfCurrentYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, 11);
		calendar.set(Calendar.DAY_OF_MONTH, 31);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		
		return calendar.getTime();
	}
	
	public static List<String> getYearMonthByParam(int count){
		List<String> list = new  ArrayList<String>();
		  SimpleDateFormat matter=new SimpleDateFormat("yyyyMM");
		  Calendar calendar = Calendar.getInstance();
		  list.add(matter.format(calendar.getTime()));
		  for (int i = 1; i < count; i++) {
			  calendar.add(Calendar.MONTH,-1);
			  list.add(matter.format(calendar.getTime()));
		  }
		  Collections.reverse(list);
	      return list;
	}
	/**
	 * DATE 时间转日期 00:00:00.000
	 */
	public static Calendar  getCalendarBydate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}
	
	/**
	 * 获取两个时间之间的天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
//	public static int getDays(Date startDate, Date endDate) {
//		int days = 0;
//		Calendar c1 = Calendar.getInstance();
//		Calendar c2 = Calendar.getInstance();
//		c1.setTime(startDate);
//		c2.setTime(endDate);
//		int years = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
//		if (years > 0) {	
//			days = YEARS * years + c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
//		} else {
//			days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
//		}
//		return days;
//	}	
	public static int getDays(Date startDate, Date endDate) {
		int days = 0;
		Calendar calendarSta = Calendar.getInstance();
		Calendar calendarEnd = Calendar.getInstance();
		calendarSta.setTime(startDate);
		calendarEnd.setTime(endDate);
		// 获取开始时间年份
		int yearSta = calendarSta.get(Calendar.YEAR);
		// 获取结束时间年份
		int yearEnd = calendarEnd.get(Calendar.YEAR);
		int timeDistance = 0;
		// 对比开始时间结束时间是否是在一年内
        if(yearSta != yearEnd){
        	// 判断是否闰年
            for(int i=yearSta; i<yearEnd; i++){
                if((i%4==0 && i%100!=0) || i%400==0){
                	// 闰年
                	timeDistance += 366;
                }else{
                	timeDistance += 365;
                }
            }
            days = timeDistance + (calendarEnd.get(Calendar.DAY_OF_YEAR) - calendarSta.get(Calendar.DAY_OF_YEAR));
        }else{
        	days = calendarEnd.get(Calendar.DAY_OF_YEAR) - calendarSta.get(Calendar.DAY_OF_YEAR);
        }
		return days;
	}
	
	/**
	 * 计算两个日期相隔天数(含闰年测试方法)
	 * @param startDate
	 * @param endDate
	 * @return
	 */
//	public static int getBetweenDays(Date startDate, Date endDate) {
//		int days = 0;
//		Calendar c1 = Calendar.getInstance();
//		Calendar c2 = Calendar.getInstance();
//		c1.setTime(startDate);
//		c2.setTime(endDate);
//		// 本地测试对比之前老方法
//		int years = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
//		if (years > 0) {	
//			days = YEARS * years + c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
//		} else {
//			days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
//		}
//		System.out.println("old方法计算:"+days+"天");
//		int year1 = c1.get(Calendar.YEAR);
//		int year2 = c2.get(Calendar.YEAR);
//		int timeDistance = 0 ;
//        if(year1 != year2){
//            for(int i=year1; i<year2; i++){
//                if((i%4==0 && i%100!=0) || i%400==0){
//                	// 闰年
//                	timeDistance += 366;
//                }else{
//                	timeDistance += 365;
//                }
//            }
//            days = timeDistance + (c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR));
//        }else{
//        	days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
//        }
//		System.out.println("new方法计算:"+days+"天");
//		System.out.println("timeDistance:"+timeDistance);
//		return days;
//	}
	
	/**
	 * 债权下架时间做成
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDaysForAssignment(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, TWODAYS);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		
		return cal.getTime();
	}
	
	/**
	 * 时间判定
	 * 判断15:00之前还是之后  0 之前,1之后
	 * @param date 传入时间
	 * @return
	 */
	public static int timeJudge(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int hour = c.get(Calendar.HOUR_OF_DAY); // 几点投的资
		if(hour < 15){
			return 0;
		}else{
			return 1;
		}
	}

	/**
	 * 时间判定
	 * 判断23:00之前还是之后  0 之前,1之后
	 * @param date 传入时间
	 * @return
	 */
	public static int timeJudge23(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int hour = c.get(Calendar.HOUR_OF_DAY); // 几点提现
		if(hour < 23){
			return 0;
		}else{
			return 1;
		}
	}

	/**
	 * 时间判定
	 * 判断23:00之前还是之后  0 之前,1之后
	 * @param date 传入时间
	 * @return
	 */
	public static int interestTimeJudge(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int hour = c.get(Calendar.HOUR_OF_DAY); // 几点投的资
		if(hour < 23){
			return 0;
		}else{
			return 1;
		}
	}
	public static int interestTimeJudge(Date date,int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int hour = c.get(Calendar.HOUR_OF_DAY); // 几点投的资
		if(hour < day){
			return 0;
		}else{
			return 1;
		}
	}
	/**
	 * 比较两个时间的大小
	 * date1小于date2返回负数
	 * date1等于date2返回0
	 * date1大于date2返回正数
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareTime(Date date1, Date date2){
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date1);
		c2.setTime(date2);
		int result = c1.compareTo(c2);
		return result;
	}
	
	/**
	 * 判断当前时间是否介于Date str1,Date str2之间
	 * 是:返回true;否则:false
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean comp(Date str1,Date str2){
		Date str= new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date=dateFormat.format(str);
		
		try {
			str = dateFormat.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  int i = str.compareTo(str1); 
		  int j=str2.compareTo(str);
		  if(i>=0 && j>=0){
			  return true;
		  }
		return false;
	}
	/**
	 * 日期+1
	 * @param date
	 * @return
	 */
	 public static Date getNextDay(Date date) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
			date = calendar.getTime();
			return date;
		}
	 
	 /**
	  * 
	 * @Description: TODO
	 *
	 * @return Date
	 * @create time 2016年5月4日 下午1:52:41
	  */
	 public static Date firestYear(){
		 Date date = new Date();
		 date.setTime(0);
		 return date;
	 }
	
	public static void main(String[] args) throws ParseException{
//		System.out.println(format(getBeginDateOfCurrentMonth(), "yyyy-MM-dd HH:mm:ss SSS"));
//		System.out.println(format(getEndDateOfCurrentMonth(), "yyyy-MM-dd HH:mm:ss SSS"));
//		System.out.println(format(getBeginDateOfNextMonth(), "yyyy-MM-dd HH:mm:ss SSS"));
//		System.out.println(format(getEndDateOfNextMonth(), "yyyy-MM-dd HH:mm:ss SSS"));
//		System.out.println(format(getBeginDateOfCurrentYear(), "yyyy-MM-dd HH:mm:ss SSS"));
//		System.out.println(format(getEndDateOfCurrentYear(), "yyyy-MM-dd HH:mm:ss SSS"));
//		System.out.println(addDaysForAssignment(new Date()));
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//		String s = "2015-09-18 15:00:00";
//		int i = timeJudge(sdf.parse(s));
//		System.out.println(i);
//		System.out.println("-------------------");
//		String s1="2008-01-29 09:12:11";
//		String s2="2008-01-29 09:12:11";
//		Date d1 = sdf.parse(s1);
//		Date d2 = sdf.parse(s2);
//		System.out.println(compareTime(d1,d2));	
//		System.out.println(toStartOfDay(new Date()));
//		System.out.println(dateDiffDayOfHour(toStartOfDay(new Date()),new Date()));
//		System.out.println(getDateOfeveryTime(10,0,0,0));
//		System.out.println("getYuanNian"+firestYear());
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
//	    Date date = sdf.parse("2016-08-15 00:00:00"); 
//	    Date dates= sdf.parse("2017-08-14 00:00:00");
//	    getBetweenDays(date, dates);
	    
Date currentDate = new Date();
		
int diffSecTime = (int) ((DateUtils.toEndOfDay(currentDate).getTime()-currentDate.getTime() )/1000);
		System.out.println("diffSecTime== "+diffSecTime+"    o=="+60*60*24);
	}
	
	public static String dateToString(Date date,String formatter){
		  SimpleDateFormat matter=new SimpleDateFormat(formatter);
		  return matter.format(date);
	}
	//获取某年后的日期
    public static Date getAddYearDate(Integer count){
    	Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, count);
		return c.getTime();
    }
}


分享到:
评论

相关推荐

    java时间处理工具类--DateUtils

    public class DateUtils { /** * 时间范围:年 */ public static final int YEAR = 1; /** * 时间范围:季度 */ public static final int QUARTER = 2; /** * 时间范围:月 */ public static ...

    自己封装的DateUtils工具类

    在Java编程中,DateUtils工具类是一个非常实用的辅助类,它封装了各种日期和时间处理的方法,大大简化了开发者在处理日期时的工作。这里我们深入探讨一下自定义的DateUtils工具类及其重要功能。 首先,`DateUtils`...

    时间工具类 DateUtils

    本篇文章将详细讲解基于提供的"时间工具类 DateUtils"的知识点,包括DateUtils的主要功能、如何使用以及Lunar.java的相关内容。 首先,`DateUtils` 类通常是一个自定义的时间处理类,它扩展了Java内置的`java.util....

    PyPI 官网下载 | dateutils-0.6.11.tar.gz

    `dateutils` 是一个 Python 库,专门用于增强 Python 的日期和时间处理能力,它提供了许多实用的功能,使得开发者能够更高效地处理日期和时间数据。`dateutils-0.6.11.tar.gz` 是这个库的源码压缩包,可以在 PyPI...

    DateUtils.java 日期处理相关工具类

    * 文件名:DateUtils.java 日期处理相关工具类 * 版本信息:V1.0 * 日期:2013-03-11 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class DateUtils { /**定义常量**/ ...

    DateUtils(日期工具类)

    DateUtils(日期工具类),包含日期格式化,解析等。

    日期时间处理的命令行工具Dateutils.zip

    Dateutils 包含几个命令行下处理日期时间的工具,特别用于处理大量金融数据。 标签:Dateutils

    DateUtils.java

    `DateUtils.java` 是一个Java编程中的自定义工具类,专门用于处理日期和时间相关的操作。在Java开发中,处理日期和时间是非常常见的需求,例如计算两个日期之间的差距、格式化日期显示、获取当前时间等。`DateUtils`...

    DateUtils.java工具类很实用

    DateUtils.java工具类很实用

    PyPI 官网下载 | dateutils-0.6.9-py2.py3-none-any.whl

    资源来自pypi官网。 资源全名:dateutils-0.6.9-py2.py3-none-any.whl

    时间的帮助类DateUtils.zip

    在Java编程语言中,日期和时间的处理是一个常见的任务,而`DateUtils`类通常是为了简化这类操作而自定义的工具类。这个`DateUtils.zip`压缩包包含了一个名为`Time的帮助类DateUtils.txt`的文件,我们可以从中学习到...

    java时间操作工具类 DateUtils

    java中常用的时间操作;如日期,时间戳,日历,字符串相互转化;时间差等常用日期功能。

    DateUtils_日期工具_

    为了解决这个问题,`DateUtils`类通常被用于封装一些常用的日期操作,避免了重复造轮子。`DateUtils`不是Java标准库的一部分,但许多开源框架如Apache Commons Lang提供了这个工具类,它提供了丰富的日期处理函数,...

    DateUtils日期工具类

    一些日期的处理,获取当前时间、date日期和字符串相互转化等

    Java 中DateUtils日期工具类的实例详解

    Java 中DateUtils日期工具类的实例详解 Java 中DateUtils日期工具类是 Java 语言中对日期类型的操作的重要组件,主要用于处理日期类型和字符串类型之间的转换。在 Java 项目中,日期类型的处理往往非常不方便,特别...

    flex 学习项目中总结的时间处理工具类DateUtils

    `DateUtils`类通常是一个自定义的工具类,用于简化JavaScript或Java等语言中的日期和时间操作。在这个名为"flex 学习项目中总结的时间处理工具类DateUtils"的项目中,我们可能找到了一个针对Flex开发环境优化的日期...

    DateUtils.java——日期处理

    属于时间转换工具类文件,其中包含格式转换,时区转换,时间计算等。

Global site tag (gtag.js) - Google Analytics