计算日期方法
2015-12-30 09:52
387 查看
/**
* 计算两个日期相差的天数
* @param s 开始时间
* @param e 结束时间
* @return
* @throws Exception
*/
public int daysBetween(String s,String e) throws Exception{
Calendar cal = Calendar.getInstance();
cal.setTime(Date.valueOf(s));
long time1 = cal.getTimeInMillis();
cal.setTime(Date.valueOf(e));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 计算两个日期相差的月数
* @param s 开始时间
* @param e 结束时间
* @return
*/
public int getMonthDiffer(String s,String e) throws Exception{
//GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。
//GregorianCalendar 是一种混合日历
Calendar cal1 = new GregorianCalendar();
cal1.setTime(Date.valueOf(s));
Calendar cal2 = new GregorianCalendar();
cal2.setTime(Date.valueOf(e));
//获取相差月数
int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 +
cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
//如果结果=0就返回1,否则就取结果的绝对值
return c == 0 ? 1 : Math.abs(c);
}
/**
* 时间戳加上秒数
* @param date 日期时间戳
* @param second 秒数
* @return 返回一个时间戳
*/
public String addDate(String date, long second) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(date);
long seconds = dt1.getTime() + second * 1000L;
Date d = new Date(seconds);
return df.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 两个时间比较大小
* @param DATE1 时间1
* @param DATE2 时间2
* @return
*/
public boolean compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
return true;
} else {
return false;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}
* 计算两个日期相差的天数
* @param s 开始时间
* @param e 结束时间
* @return
* @throws Exception
*/
public int daysBetween(String s,String e) throws Exception{
Calendar cal = Calendar.getInstance();
cal.setTime(Date.valueOf(s));
long time1 = cal.getTimeInMillis();
cal.setTime(Date.valueOf(e));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 计算两个日期相差的月数
* @param s 开始时间
* @param e 结束时间
* @return
*/
public int getMonthDiffer(String s,String e) throws Exception{
//GregorianCalendar 是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。
//GregorianCalendar 是一种混合日历
Calendar cal1 = new GregorianCalendar();
cal1.setTime(Date.valueOf(s));
Calendar cal2 = new GregorianCalendar();
cal2.setTime(Date.valueOf(e));
//获取相差月数
int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 +
cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
//如果结果=0就返回1,否则就取结果的绝对值
return c == 0 ? 1 : Math.abs(c);
}
/**
* 时间戳加上秒数
* @param date 日期时间戳
* @param second 秒数
* @return 返回一个时间戳
*/
public String addDate(String date, long second) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(date);
long seconds = dt1.getTime() + second * 1000L;
Date d = new Date(seconds);
return df.format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 两个时间比较大小
* @param DATE1 时间1
* @param DATE2 时间2
* @return
*/
public boolean compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
return true;
} else {
return false;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}
相关文章推荐
- Gallery与ImageSwitch的使用
- Tornado,了解一下
- 强大的 ant scp 和 sshexec
- 实战申请Let's Encrypt永久免费SSL证书过程教程及常见问题
- 试图搞懂MDK程序下载到flash--MDK配置选项Linker、Target、Debug的理解分析
- Number of actions are more than display limit 25.
- dojo入门教程
- rsyslog的入库
- Best Practices for Speeding Up Your Web Site--如何加速你的网站访问性能
- 归档模式下redo丢失或损坏(active状态)
- 【学习】Android代码混淆与反编译
- 23123
- php中计算二维数组中某一元素之和
- 让旧浏览器支持HTML5新增标签
- [ActionScript 3.0] AS3 实现XML转换成JSON
- Flume HDFS Sink使用及源码分析
- [转]#ifdef中的逻辑与或操作
- CC2530 wireshark抓包 sniffer分析6lowpan RPL路由神器
- ARM嵌入式系统开发(在线视频)
- iOS开发笔记8:Remote Notification远程消息推送处理