JAVA 常用时间日期计算转换方法

<span style="font-family: Arial, Helvetica, sans-serif;">1指定 日期 格式 yyyyMMdd  指定日期与当前相差的天数</span>
2加减天数与当前日期加减后的日期
3指定日期格式 yyyyMMdd  加减天数  指定日期加减后的日期
4指定 日期1 格式 yyyyMMdd 指定 日期2 格式 yyyyMMdd     指定日期1与日期2相差的天数
5>把LONG类型转换成字符串日期
 
package Time.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ZyfTime {

	/**
	 @param day 指定 日期 格式 yyyyMMdd                                 
	 *  @return 指定日期与当前相差的天数           </span></span><span style="font-size:14px;"><span style="color:#000099;">
         * @throws ParseException 
	 */</span>
	
	  public static int getDays(String days) throws ParseException{
	    	SimpleDateFormat sf1 = 
				new SimpleDateFormat("yyyyMMdd");
			Date d1 = new Date();
			Date d2 = sf1.parse(days);
			Calendar cc = Calendar.getInstance();
			cc.setTime(d1);
			d1 = sf1.parse(sf1.format(cc.getTime()));
			int day =(int)((d2.getTime()-d1.getTime())
					      /(86400*1000));
			return day;
	    }
	
	
	/**
	 * @param dayCount 加减天数
	 * @return与当前日期加减后的日期
	 */</span>
	public String getDay(int dayCount){
		Date today=new Date();
		Calendar c=Calendar.getInstance();  
		c.setTime(today);
		c.add(Calendar.DAY_OF_MONTH, dayCount);
		SimpleDateFormat sf1=new SimpleDateFormat("yyyyMMdd");
	
		return sf1.format(c.getTime());
			
	}
	
	<span style="color:#000099;">/**
	 * @param day 指定 日期 格式 yyyyMMdd
	 * @param count 加减天数
	 * @return 指定日期加减后的日期
	 * @throws ParseException 
	 */</span>
	public String getDay(String day,int Count) throws ParseException{
		
		
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		
		Calendar c=Calendar.getInstance();  
		c.setTime(sf.parse(day));
		c.add(Calendar.DAY_OF_MONTH, Count);
		return sf.format(c.getTime());
		
	
	}
	/**
	 * @param day1 指定 日期1 格式 yyyyMMdd
	 * @param day2 指定 日期2 格式 yyyyMMdd
	 * @return 指定日期1与日期2相差的天数
	 * @throws ParseException 
	 */</span>
	public int getDays(String day1,String day2) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		Date d1=sdf.parse(day1);
		Date d2=sdf.parse(day2);
		int count=(int) ((d1.getTime()-d2.getTime())/86400000);
		return count;
		
	}
 /**
     * 把LONG类型转换成字符串日期
     * @param times
     * @param format
     * @return
     */</span>
    public String getDate(long times,String format){
    	SimpleDateFormat sf=new SimpleDateFormat(format);
    	String date=sf.format(new Date(times));
    	return date;
    }
}
<span style="font-family: Arial, Helvetica, sans-serif;">调用程序及结果图</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值