java获取两个时间的相隔时间,包括年、月、日、时、分、秒
public static final int YEAR_RETURN = 0;
public static final int MONTH_RETURN = 1;
public static final int DAY_RETURN = 2;
public static final int HOUR_RETURN= 3;
public static final int MINUTE_RETURN = 4;
public static final int SECOND_RETURN = 5;
public static final String YYYY = "yyyy";
public static final String YYYYMM = "yyyy-MM";
public static final String YYYYMMDD = "yyyy-MM-dd";
public static final String YYYYMMDDHH= "yyyy-MM-dd HH";
public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
public static long getBetween(String beginTime, String endTime, String formatPattern, int returnPattern) throws ParseException{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
Date beginDate = simpleDateFormat.parse(beginTime);
Date endDate = simpleDateFormat.parse(endTime);
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);
endCalendar.setTime(endDate);
switch (returnPattern) {
case YEAR_RETURN:
return GgczUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR);
case MONTH_RETURN:
return GgczUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR)*12 + GgczUtil.getByField(beginCalendar, endCalendar, Calendar.MONTH);
case DAY_RETURN:
return GgczUtil.getTime(beginDate, endDate)/(24*60*60*1000);
case HOUR_RETURN:
return GgczUtil.getTime(beginDate, endDate)/(60*60*1000);
case MINUTE_RETURN:
return GgczUtil.getTime(beginDate, endDate)/(60*1000);
case SECOND_RETURN:
return GgczUtil.getTime(beginDate, endDate)/1000;
default:
return 0;
}
}
private static long getByField(Calendar beginCalendar, Calendar endCalendar, int calendarField){
return endCalendar.get(calendarField) - beginCalendar.get(calendarField);
}
private static long getTime(Date beginDate, Date endDate){
return endDate.getTime() - beginDate.getTime();
}
public static void main(String[] args) {
Date start = new Date("2008/05/00");
Date end = new Date("2009/06/00");
System.out.println("###start:==" + start);
System.out.println("###end:==" + end);
getMonth(start, end);
System.out.println("###getMonth():=" + getMonth(start, end));
try {
System.out.println(GgczUtil.getBetween("2013-05-02", "2013-05-05", GgczUtil.YYYYMMDD, GgczUtil.MONTH_RETURN));
} catch (ParseException e) {
e.printStackTrace();
}
}
java获取两个时间的相隔时间,包括年、月、日、时、分、秒的更多相关文章
- mysql 获取系统时间的下一天 年-月-日 时:分:秒
DAY) as date
- ASP.NET C# 获取当前日期 时间 年 月 日 时 分 秒
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...
- ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换(转自happymagic的专栏)
在平时编码中,经常要把日期转换成各种各样的形式输出或保持,今天专门做了个测试,发现DateTime的ToString()方法居然有这么多的表现形式,和大家一起分享. DateTime time=Dat ...
- ASP.NET 日期 时间 年 月 日 时 分 秒 格式及转换
在平时编码中,经常要把日期转换成各种各样的形式输出或保持,今天专门做了个测试,发现DateTime的ToString()方法居然有这么多的表现形式,和大家一起分享. DateTime time=Dat ...
- [Windows]获取当前时间(年/月/日/时/分/秒)
struct tm* GetCurTime(time_t inTime) { struct tm* curTime = localtime(&inTime); curTime->tm_y ...
- Java 获取年 月 日 时 分 秒
/** * 英文简写(默认)如:2010-12-01 */ public static String FORMAT_SHORT = "yyyy-MM-dd"; /** * 英文全称 ...
- oracle截取时间的年/月/日/时/分/秒
修改日期格式为年月日时分秒: alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';select to_char(sysdate,'yyy ...
- C#WinForm中显示实时时间:年/月/日 时/分/秒 星期X
//加载窗体时 string weekstr = ""; private void Form22_Load(object sender, EventArgs e) { this.t ...
- js将当前时间格式化为年-月-日 时:分:秒
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WeUI移动页面实现时间选择器(年-月-日-时-分)
在做微信公众号的时候,使用的WeUI样式,有一个需求是用户选择一个预约时间,需要年月日并精确到小时和分钟. 但是WeUI的picker组件不支持直接显示5列,根据WeUI.js作者的建议,是将日期和时 ...
随机推荐
- MyEclipse 快捷键
MyEclipse 快捷键1(CTRL) Ctrl+1 快速修复Ctrl+D: 删除当前行Ctrl+Q 定位到最后编辑的地方Ctrl+L 定位在某行Ctrl+O 快速显示 OutLineCtrl+T ...
- 关于iOS热修复(HotPatch)技术的使用总结
苹果做了非常多的努力来建造和维持一个健康并且干净的应用环境.其中对现在的现状起到很大作用的部分就是苹果APP STORE,它是被一个十分周密的对所有提交的应用进行检查的审批程序所保护的.尽管这个程序是 ...
- 《javascript高级程序设计》第三章学习笔记
Undefined类型 该类型只有一个值,即undefined. 对未初始化的变量和未定义的变量,用typeof检测,都会返回'undefined' Null类型 该类型只有一个值,null.并且从逻 ...
- macOS sierra 10.12 Cocoapods 私有库
使用Cocoapods创建私有podspec 见文章:http://www.cocoachina.com/ios/20150228/11206.html 或http://blog.wtlucky.co ...
- Python 2.7 - CentOS 7 - ImportError: No module named Tkinter
It's simple. sudo yum -y install tkinter Just want to say, "I'm back".
- [原创]如何在Parcelable中使用泛型
[原创]如何在Parcelable中使用泛型 实体类在实现Parcelable接口时,除了要实现它的几个方法之外,还另外要定义一个静态常量CREATOR,如下例所示: public static cl ...
- MyBatis操作指南-配置使用Provider动态生成SQL
- Redis: OOM command not allowed when used memory > ‘maxmemory’
现象 日志里出现异常: OOM command not allowed when used memory > 'maxmemory' 原因 内存已满,不允许再存数据了,可以通过redis-cli ...
- 从 IClassFactory 为 CLSID 为 {00024500-0000-0000-C000-000000000046} 的 COM 组件创建实例失败,原因是出现以下错误: 8001010a解决办法
1.在命令行中输入:dcomcnfg,会显示出“组件服务”管理器 2.打开“组件服务->计算机->我的电脑->DCOM 配置”,找到“Microsoft Word文档”,单击右键,选 ...
- VBA控制outlook自动发送邮件(转)
使用Excel VBA实现Outlook自动发送邮件 | 在工作上我们都会遇到批量发送邮件的情况,面对重复而规律性的工作,可以使用Excel的VBA实现自动批量化发送邮件.大大减小工作时间,提升工作效 ...