java计算时间
来源:互联网 发布:dz论坛seo入门教程 编辑:程序博客网 时间:2024/06/05 19:27
把代码块放在其中就是了.
线程的运行的时间也是类似的
public static void testRunningTime() {
long t1 = System.currentTimeMillis();
// invoke your program here
long t2 = System.currentTimeMillis();
System.out.println("Your program has executed for "
+ (int)((t2-t1)/1000)+ " seconds "
+ ((t2-t1) % 1000) + " micro seconds");
}
* 计算指定日期的上一天
*
* @param dateTime
* 日期,格式为:yyyy-MM-dd
* @return
*/
public static String getBeforeDay(String dateTime) {
Calendar now = Calendar.getInstance();
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simpledate.parse(dateTime);
} catch (ParseException ex) {
System.out.println("日期格式不符合要求:" + ex.getMessage());
return null;
}
now.setTime(date);
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH) - 1;
now.set(year, month, day);
String time = simpledate.format(now.getTime());
return time;
}
/**
* 计算指定日期的下一天
*
* @param dateTime
* 日期,格式为:yyyy-MM-dd
* @return
*/
public static String getNextDay(String dateTime) {
Calendar now = Calendar.getInstance();
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simpledate.parse(dateTime);
} catch (ParseException ex) {
System.out.println("日期格式不符合要求:" + ex.getMessage());
return null;
}
now.setTime(date);
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH) + 1;
now.set(year, month, day);
String time = simpledate.format(now.getTime());
return time;
}
/**
* 得到指定月的天数
* @param _year
* @param _month
* @return
*/
public static int getMaxDayOfMonth(int _year, int _month){
Calendar now = Calendar.getInstance();
int year = 0;
int month = 0;
if(_month==1){
year = _year - 1;
month = 12;
}else{
year = _year;
month = _month - 1;
}
now.set(Calendar.YEAR, year);
now.set(Calendar.MONTH, month);
return now.getActualMaximum(Calendar.DATE);
}
/**
* 计算时间差
*
* @param beginTime
* 开始时间,格式:yyyy-MM-dd HH:mm:ss
* @param endTime
* 结束时间,格式:yyyy-MM-dd HH:mm:ss
* @return 从开始时间到结束时间之间的时间差(秒)
*/
public static long getTimeDifference(String beginTime, String endTime) {
long between = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date end = null;
Date begin = null;
try {// 将截取到的时间字符串转化为时间格式的字符串
end = sdf.parse(endTime);
begin = sdf.parse(beginTime);
} catch (ParseException e) {
e.printStackTrace();
}
between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
return between;
}
/**
* 计算时间差
*
* @param time
* 指定的时间,格式为:yyyy-MM-dd HH:mm:ss
* @return 当前时间和指定时间的时间差(秒)
*/
public static long getTimeDifference(String time) {
long between = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String systemTime = sdf.format(new Date()).toString();
Date end = null;
Date begin = null;
try {// 将截取到的时间字符串转化为时间格式的字符串
end = sdf.parse(time);
begin = sdf.parse(systemTime);
} catch (ParseException e) {
e.printStackTrace();
}
between = Math.abs(end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
return between;
}
- java计算时间
- Java时间计算陷阱
- java 计算时间
- java计算时间
- Java计算时间
- java 时间 日期计算
- java时间计算
- java 时间比较计算
- java计算时间
- java时间计算
- java里的计算时间
- JAVA日期时间的计算
- JAVA对时间的计算
- 计算Java程序运行时间
- Java计算程序运行时间
- 计算Java程序运行时间
- 【Java】 时间比较--时间差计算
- JAVA程序运行时间计算
- MinGW编译ffmpeg的静态库
- oracle startup mount nomount 区别
- URAL 1017
- js bind()函数 使用闭包保存执行上下文
- JProfiler与Eclipse 的整合使用
- java计算时间
- HDU 4699 Editor
- Android Phone模块 二
- spring mvc 导出excel
- 2013多校第10场第9题--HDU4704
- ffmpeg在windows下的编译
- WebService基于SoapHeader实现安全认证
- 我读Mongoose源码----程序框架
- Hanoi塔