| | |
| | | return format.format(ca.getTime());
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 指定日期加上天数后的日期
|
| | | * |
| | | * @param num
|
| | | * 增加的天数
|
| | | * @param nowDate
|
| | | * 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static String plusDay(int num, Date currdate) throws ParseException {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | |
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, num);
|
| | | |
| | | return format.format(ca.getTime());
|
| | | }
|
| | | |
| | | /**
|
| | | * 指定日期减去天数后的日期
|
| | | *
|
| | |
| | | ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
|
| | |
|
| | | return format.format(ca.getTime());
|
| | | }
|
| | | |
| | | /**
|
| | | * 计算两个日期之间相差的天数 |
| | | * @param smdate 较小的时间 |
| | | * @param bdate 较大的时间 |
| | | * @return 相差天数 |
| | | * @throws ParseException |
| | | * calendar 对日期进行时间操作
|
| | | * getTimeInMillis() 获取日期的毫秒显示形式
|
| | | */
|
| | | public static int daysBetween(Date smdate,Date bdate) throws ParseException |
| | | { |
| | | 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)); |
| | | }
|
| | | |
| | | /**
|
| | | * 字符串日期格式的计算
|
| | | * @param smdate
|
| | | * @param bdate
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static int daysBetween(String smdate,String bdate) throws ParseException{ |
| | | SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); |
| | | 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)); |
| | | }
|
| | |
|
| | | /**
|
| | |
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | * @param args
|
| | | * @throws ParseException
|
| | | * format() 对日期进行格式化处理
|
| | | * parse() 将日期设置为date类型
|
| | | */
|
| | | public static void main(String[] args) throws ParseException { |
| | | // TODO Auto-generated method stub |
| | | SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date d1=sdf.parse("2016-09-08 00:00:00"); |
| | | Date d2=sdf.parse("2016-09-08 00:00:00");
|
| | | |
| | | System.out.println(daysBetween(d1,d2)); |
| | | System.out.println(daysBetween("2016-09-08 10:10:10","2016-09-29 00:00:00")); |
| | | } |
| | |
|
| | | |
| | | } |