java计算时间

java获取时间到毫秒
<wbr><span style="font-family:宋体">Calendar CD = Calendar.getInstance();<br><wbr><wbr><br><wbr><wbr>int YY = CD.get(Calendar.YEAR) ;<br><wbr><wbr>int MM = CD.get(Calendar.MONTH)+1;<br><wbr><wbr>int DD = CD.get(Calendar.DATE);<br><wbr><wbr>int HH = CD.get(Calendar.HOUR_OF_DAY);<br><wbr><wbr>int NN = CD.get(Calendar.MINUTE);<br><wbr><wbr>int SS = CD.get(Calendar.SECOND);<br><wbr><wbr>int MI = CD.get(Calendar.MILLISECOND);<br><wbr><wbr><br><wbr><wbr>String timeNow = YY + "/" + MM + "/" + DD + " " + HH + "." + NN + "." + SS + "." + MI;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></wbr>
你可以用时间差来计算啊!
把代码块放在其中就是了.
线程的运行的时间也是类似的
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 staticString getBeforeDay(String dateTime) {
Calendar now = Calendar.getInstance();
SimpleDateFormat simpledate =
newSimpleDateFormat("yyyy-MM-dd");
Date date =
null;
try{
date = simpledate.parse(dateTime);
}
catch(ParseException ex) {
System.
out.println("日期格式不符合要求:"+ ex.getMessage());
returnnull;
}
now.setTime(date);
intyear = now.get(Calendar.YEAR);
intmonth = now.get(Calendar.MONTH);
intday = now.get(Calendar.DAY_OF_MONTH) - 1;
now.set(year, month, day);
String time = simpledate.format(now.getTime());
returntime;
}


/**
* 计算指定日期的下一天
*
* @param dateTime
* 日期,格式为:yyyy-MM-dd
* @return
*/

public staticString getNextDay(String dateTime) {
Calendar now = Calendar.getInstance();
SimpleDateFormat simpledate =newSimpleDateFormat("yyyy-MM-dd");
Date date =null;
try{
date = simpledate.parse(dateTime);
}catch(ParseException ex) {
System.out.println("日期格式不符合要求:"+ ex.getMessage());
returnnull;
}
now.setTime(date);
intyear = now.get(Calendar.YEAR);
intmonth = now.get(Calendar.MONTH);
intday = now.get(Calendar.DAY_OF_MONTH) + 1;
now.set(year, month, day);
String time = simpledate.format(now.getTime());
returntime;
}

/**
* 得到指定月的天数
* @param _year
* @param _month
* @return
*/

public static intgetMaxDayOfMonth(int_year,int_month){
Calendar now = Calendar.getInstance();
intyear = 0;
intmonth = 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);

returnnow.getActualMaximum(Calendar.DATE);
}

/**
* 计算时间差
*
* @param beginTime
* 开始时间,格式:yyyy-MM-dd HH:mm:ss
* @param endTime
* 结束时间,格式:yyyy-MM-dd HH:mm:ss
* @return 从开始时间到结束时间之间的时间差(秒)
*/

public static longgetTimeDifference(String beginTime, String endTime) {
longbetween = 0;
SimpleDateFormat sdf =newSimpleDateFormat("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是为了转换成秒

returnbetween;
}

/**
* 计算时间差
*
* @param time
* 指定的时间,格式为:yyyy-MM-dd HH:mm:ss
* @return 当前时间和指定时间的时间差(秒)
*/

public static longgetTimeDifference(String time) {
longbetween = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String systemTime = sdf.format(newDate()).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是为了转换成秒

returnbetween;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值