获取两个时间之间的差值
来源:互联网 发布:silverlight 调用js 编辑:程序博客网 时间:2024/05/08 21:11
/*
* 计算两个时间的时间差值
* startTime:开始时间
* endTime:结束时间
* format:转变格式
* str:输出日期样式(d:消失,h:小时,m:分钟,s:秒)
* */
public Long dateDiff(String startTime, String endTime,
String format, String str) {
// 按照传入的格式生成一个simpledateformate对象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
long nh = 1000 * 60 * 60;// 一小时的毫秒数
long nm = 1000 * 60;// 一分钟的毫秒数
long ns = 1000;// 一秒钟的毫秒数
long diff;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
long timeDiffer = 0;
// 获得两个时间的毫秒时间差异
try {
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
day = diff / nd;// 计算差多少天
hour = diff % nd / nh + day * 24;// 计算差多少小时
min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
sec = diff % nd % nh % nm / ns;// 计算差多少秒
// 输出结果
System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时"
+ (min - day * 24 * 60) + "分钟" + sec + "秒。");
System.out.println("hour=" + hour + ",min=" + min);
if (str.equalsIgnoreCase("h"))
{
return hour;
}
else if(str.equalsIgnoreCase("d"))
{
return day;
}
else if(str.equalsIgnoreCase("m"))
{
return min;
}
else
{
return sec;
}
} catch (ParseException e) {
e.printStackTrace();
return timeDiffer;
}
}
* 计算两个时间的时间差值
* startTime:开始时间
* endTime:结束时间
* format:转变格式
* str:输出日期样式(d:消失,h:小时,m:分钟,s:秒)
* */
public Long dateDiff(String startTime, String endTime,
String format, String str) {
// 按照传入的格式生成一个simpledateformate对象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
long nh = 1000 * 60 * 60;// 一小时的毫秒数
long nm = 1000 * 60;// 一分钟的毫秒数
long ns = 1000;// 一秒钟的毫秒数
long diff;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
long timeDiffer = 0;
// 获得两个时间的毫秒时间差异
try {
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
day = diff / nd;// 计算差多少天
hour = diff % nd / nh + day * 24;// 计算差多少小时
min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
sec = diff % nd % nh % nm / ns;// 计算差多少秒
// 输出结果
System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时"
+ (min - day * 24 * 60) + "分钟" + sec + "秒。");
System.out.println("hour=" + hour + ",min=" + min);
if (str.equalsIgnoreCase("h"))
{
return hour;
}
else if(str.equalsIgnoreCase("d"))
{
return day;
}
else if(str.equalsIgnoreCase("m"))
{
return min;
}
else
{
return sec;
}
} catch (ParseException e) {
e.printStackTrace();
return timeDiffer;
}
}
0 0
- 获取两个时间之间的差值
- Java获取两个时间之间的天数差值
- C#两个时间之间的差值
- 两个相邻时间之间的差值计算
- 通过Calendar 来计算两个时间之间的差值
- 计算两个时间的差值
- 获取两个时间之间的随机时间
- 计算SharePoint两个日期和时间字段之间的时间差值
- mysql时间列之间的时间差值
- 获取两个时间之间的天数
- 怎么计算两个日期之间的差值
- 两个dateTimePicker控件之间的差值
- asp.net计算两个日期时间之间的差值(天数、小时、分、秒等)
- Android中根据时间来计算两个值之间的差值
- oracle计算两个日期之间时间差值sql
- 计算两个时间点的差值
- c#计算两个时间的差值
- php计算两个时间的差值
- unity3d 跨行业宣传片!Unity 不只是游戏!【狗刨学习网】
- 【iOS开发-118】AVFoundation框架:AudioServices...音效,AVAudioPlayer音乐,AVAudioRecord录音
- Android将Activity打成jar包供第三方调用(解决资源文件不能打包的问题)
- POJ 3267-The Cow Lexicon(dp_字符串)
- 如何在发型不乱的前提下应对单日十亿计Web请求
- 获取两个时间之间的差值
- php 5X 使用自带的sqlite3链接数据库,PDO链接sqlite(低成本开发)(一)
- UML笔记2-类的关系
- 生活垃圾分类处理(1)
- hdu——5166
- 专业词汇整理
- Android开源项目分类汇总(转)
- cell 复用问题 cell中控件单选,多选,带标签图的不可选中
- Redis在多实例下对数据分区