Java比较两个时间相差多少天,多少个月,多少年
来源:互联网 发布:微信猪软件 编辑:程序博客网 时间:2024/04/28 15:30
/** * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12 * @param date2 被比较的时间 为空(null)则为当前时间 * @param stype 返回值类型 0为多少天,1为多少个月,2为多少年 * @return * 举例: * compareDate("2009-09-12", null, 0);//比较天 * compareDate("2009-09-12", null, 1);//比较月 * compareDate("2009-09-12", null, 2);//比较年 */ public static int compareDate(String startDay,String endDay,int stype){ int n = 0; String[] u = {"天","月","年"}; String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd"; endDay = endDay==null?getCurrentDate("yyyy-MM-dd"):endDay; DateFormat df = new SimpleDateFormat(formatStyle); Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(df.parse(startDay)); c2.setTime(df.parse(endDay)); } catch (Exception e3) { System.out.println("wrong occured"); } //List list = new ArrayList(); while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果 //list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来 n++; if(stype==1){ c1.add(Calendar.MONTH, 1); // 比较月份,月份+1 } else{ c1.add(Calendar.DATE, 1); // 比较天数,日期+1 } } n = n-1; if(stype==2){ n = (int)n/365; } System.out.println(startDay+" -- "+endDay+" 相差多少"+u[stype]+":"+n); return n; } public static String getCurrentDate(String format){Calendar day=Calendar.getInstance(); day.add(Calendar.DATE,0); SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"String date = sdf.format(day.getTime());return date;}
0 0
- Java比较两个时间相差多少天,多少个月,多少年
- Java比较两个时间相差多少天,多少个月,多少年
- Java比较两个时间相差多少天,多少个月,多少年
- java计算2个时间相差多少年,多少个月,多少天
- 计算格式为YYYY-MM-DD HH:ii:ss的两个时间,相差多少年、多少月、多少日、多少小时、多少分钟、多少秒?
- ios 两个时间之间的比较,相差多少天多少小时多少分多少秒
- 两个时间相差多少天
- 计算两个日期相隔多少年,多少月,多少天
- 判断两个时间相差多少天多少分多少秒
- 比较两个日期相差多少天
- java判断2个时间相差多少天、多少小时、多少分
- java判断2个时间相差多少天、多少小时、多少分
- java实现计算两个日期相差多少月、比较两个日期大小 等常用日期操作
- java实现计算两个日期相差多少月、比较两个日期大小 等常用日期操作
- 两个时间之间 相差多少
- 【Java】获取2个日历类型的时间相差多少年、月、天、小时、分钟、秒
- c#中,比较两个时间相差多少分钟
- js比较两个时间相差多少分钟(笔记)
- myeclipse去除jsp校验错误
- css html js
- Javascript中最常用的55个经典技巧
- 用spring简单实现发送邮件
- 属性不匹配。存在属性(Directory, Archive),包括属性(0),不包括属性(Archive, Compressed, Encrypted)
- Java比较两个时间相差多少天,多少个月,多少年
- Anaconda 安装 tensorflow 环境下的 import 问题
- 基于jQuery的二级联动<select>
- 深入理解HTTP协议、HTTP协议原理分析
- Logback的使用
- Java移位操作
- jQuery
- Python Bug 整理
- jQuery插件ThickBox