package time.consume;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class countConsumeTime {
// 起始时间
public Date startTime = null;
// 终止时间
public Date endTime = null;
// 消耗时间
public long timeSpan = 0;
// 设置日期格式
public SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
// 获取时间格式
public SimpleDateFormat getDateFormat() {
return dateFormat;
}
// 设置时间格式
public void setDateFormat(SimpleDateFormat df) {
this.dateFormat = df;
}
// 获取时间跨度 毫秒计
public long getTimeSpan() {
timeSpan = endTime.getTime() - startTime.getTime();
return timeSpan;
}
// 时间转换成时分秒
public String getTimeSpanByFormat() {
String formatTimeSpan = "0秒";
long temp = timeSpan/1000;
String format;
Object[] array;
Integer hours = (int) (temp / (60 * 60));
Integer minutes = (int) (temp / 60 - hours * 60);
Integer seconds = (int) (temp - minutes * 60 - hours * 60 * 60);
if (hours > 0) {
format = "%1$,d时%2$,d分%3$,d秒";
array = new Object[] { hours, minutes, seconds };
} else if (minutes > 0) {
format = "%1$,d分%2$,d秒";
array = new Object[] { minutes, seconds };
} else {
format = "%1$,d秒";
array = new Object[] { seconds };
}
formatTimeSpan = String.format(format, array);
return formatTimeSpan;
}
public Date getStartTime() {
return startTime;
}
// 当前时间
public void setStartTimeByCurrentTime() {
this.startTime = new Date();
}
// 指定时间
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
// 当前时间
public void setEndTimeByCurrentTime() {
this.endTime = new Date();
}
// 指定时间
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public static void main(String[] args) {
countConsumeTime countConsumeTime = new countConsumeTime();
Date starttime = null;
try {
starttime = countConsumeTime.getDateFormat().parse(
"2014-10-15 19:03:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
countConsumeTime.setStartTime(starttime);
countConsumeTime.setEndTimeByCurrentTime();
System.out.println(countConsumeTime.getTimeSpan());
System.out.println(countConsumeTime.getTimeSpanByFormat());
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
耗时统计.rar (5个子文件)
耗时统计
TimeConsumeCount
.project 392B
src
time
consume
countConsumeTime.java 2KB
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 302B
bin
time
consume
countConsumeTime.class 3KB
共 5 条
- 1
资源评论
梅馨嫣栀
- 粉丝: 31
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功