积累的操作时间的工具类dateutil

dateutil之获取两个时间相差多少天和指定时间的前后N天日期

获取两个字符串类型的日期相差多少天,第一个时间比第二个时间小N天

public static int daysBetween(String smdate,String bdate) throws ParseException, java.text.ParseException
	{
    	SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_SHORT);
        Calendar cal = Calendar.getInstance();  
        cal.setTime(sdf.parse(smdate));  
        long time1 = cal.getTimeInMillis();               
        cal.setTime(sdf.parse(bdate));  
        long time2 = cal.getTimeInMillis();       
        long between_days=(time2-time1)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));   
	}

获取两个date类型的日期相差多少天,第一个时间比第二个时间小N天

/** 
     * 计算两个日期之间相差的天数 
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
	 * @throws ParseException 
	 * @throws java.text.ParseException 
     */  
    public static int daysBetween(Date smdate,Date bdate) throws ParseException, java.text.ParseException  
    {  
    	SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_SHORT);
    	smdate=sdf.parse(sdf.format(smdate));
    	bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();  
        cal.setTime(smdate);  
        long time1 = cal.getTimeInMillis();               
        cal.setTime(bdate);  
        long time2 = cal.getTimeInMillis();       
        long between_days=(time2-time1)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));         
    }  

得到指定日期的前N天或者后N天  前N个月或者后N个月  前N秒或者后N秒   前N小时或者后N小时的字符串日期

/**
	 * 得到指定日期的前后几天日期
	 * @param format  格式
	 * @return  字符串日期
	 */
	public static String beforeAfterNDaysDate(Date dt,int type,Integer day,String format) {
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dt);// 把当前时间赋给日历
		calendar.add(type, day); // 设置为前后N天
		dBefore = calendar.getTime(); // 得到前一天的时间
		SimpleDateFormat sdf = new SimpleDateFormat(format); // 设置时间格式
		String timed = sdf.format(dBefore); // 格式化前一天
		return timed;
	}

得到指定日期的最后一天

//得到指定日期的最后一天
	public static Date getLastDayOfMonth(Date dt) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dt);// 把当前时间赋给日历
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		return calendar.getTime();
	}


字符串转日期

/**
	 * 字符串转日期
	 * @return Date
	 */
	public static Date parseStringToDate(String thedate, String format) throws java.text.ParseException {
		DateFormat sdf = new SimpleDateFormat(format);
		Date dd1 = null;
	    if(thedate != null && !thedate.equals("")){
			dd1 = sdf.parse(thedate);
	    }
		return dd1;
	}

日期转字符串

/**
	 * 日期转字符串
	 * 
	 * @return String
	 */
	public static String parseDateToString(Date thedate, String format) {
		DateFormat df = new SimpleDateFormat(format);
		if (thedate != null) {
			return df.format(thedate.getTime());
		}
		return "";
	}

将时间戳转化为时间

/**
	 * 将时间戳转化为时间
	 * @param seconds
	 * @param format
	 * @return
	 * @throws java.text.ParseException 
	 */
	public static String timeStamp2Date(String seconds, String format) throws Exception {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String sd = sdf.format(new Date(Long.parseLong(seconds)));
        return sd;
    }





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值