java获取日期相差天数_java 获取日期相差天数 月数 获得服务器时间

package com.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /**  * @author zhangfj @email yjia2009@yahoo.cn  * @date 2012-4-19 下午10:01:13   * 对日期进行处理  */ public class DateUtil {  public static Date getSystemTime(){  return getSystemTime("1");  }  public static String getStrSystemTime(){  return getStrSystemTime("1");  } /** * 获取系统时间 * @param type: 1 获取日期  2 获取详细时间 * @return 日期类型时间 * @throws ParseException  */  public static Date getSystemTime(String type) {  String Pattern ="yyyy-MM-dd";  if(type.equals("1")){  Pattern ="yyyy-MM-dd";  }  if(type.equals("2")){  Pattern ="yyyy-MM-dd HH:mm:ss";  }  SimpleDateFormat sdf = new SimpleDateFormat(Pattern);  Calendar calendar = Calendar.getInstance();  sdf.format(calendar.getTime());  Date date = null; try { date = sdf.parse(sdf.format(calendar.getTime())); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }  return date;  }    /** * 获取系统时间 * @param type: 1 获取日期  2 获取详细时间 * @return 字符串类型时间 * @throws ParseException  */  public static String getStrSystemTime(String type) {  String Pattern ="yyyy-MM-dd";  if(type.equals("1")){  Pattern ="yyyy-MM-dd";  }  if(type.equals("2")){  Pattern ="yyyy-MM-dd HH:mm:ss";  }  SimpleDateFormat formatter = new SimpleDateFormat(Pattern);  Calendar calendar = Calendar.getInstance();  formatter.format(calendar.getTime());  String strdate = null;  strdate = formatter.format(calendar.getTime());  return strdate;  }  /** *  获取日期相差天数 * @param  * @return 日期类型时间 * @throws ParseException  */  public static Long getDiffDay(String beginDate,String endDate) {  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  Long checkday=0l; //开始结束相差天数  try { checkday = (formatter.parse(endDate).getTime()- formatter.parse(beginDate).getTime())/(1000*24*60*60); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); checkday=null; }  return checkday;  }    public static Long getDiffDay(Date beginDate,Date endDate) {  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  String strBeginDate = format.format(beginDate);    String strEndDate = format.format(endDate);  return getDiffDay(strBeginDate,strEndDate);  }    /** *  获取日期相月数 * @param  * @return 日期类型时间 * @throws ParseException  */  public static int getDiffMonth(String beginDate,String endDate) {  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  Date dbeginDate = null;  Date dendDate = null; try { dbeginDate = formatter.parse(beginDate); dendDate = formatter.parse(endDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }  return getDiffMonth(dbeginDate,dendDate);  }  public static int getDiffMonth(Date beginDate,Date endDate) {  Calendar calbegin = Calendar.getInstance();  Calendar calend = Calendar.getInstance();  calbegin.setTime(beginDate);  calend.setTime(endDate);  int m_begin = calbegin.get(Calendar.MONTH)+1; //获得合同开始日期月份  int m_end = calend.get(Calendar.MONTH)+1;   //获得合同结束日期月份  int checkmonth = m_end-m_begin+(calend.get(Calendar.YEAR)-calbegin.get(Calendar.YEAR))*12; //获得合同结束日期于开始的相差月份  return checkmonth;  } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值