08-07
28
java中处理时间相加问题
作者:Java伴侣 日期:2008-07-28
方法一:
方法二:
方法三:
最好用java.util.Calendar。
Calendar cl=Calendar.getInstance();
Long clTemp=cl.getTimeInMillis()+7*24*60*60*1000;
cl.setTimeInMillis(clTemp);
/*date即为现在时间加上七天后的时间*/
Date date=cl.getTime();
复制内容到剪贴板 程序代码
package com.blur.bean.time;
public class sortTime {
public static void main(String[] args) {
String time1 = "12:45";
String time2 = "0:1";
String time3 = "";
try {
time3 = testAddTime(time1, time2);
} catch (NumberFormatException ex) {
System.out.println("time format Error!");
return;
}
System.out.println(time3);
}
static String testAddTime(String time1, String time2)
throws NumberFormatException {
if (time1.indexOf(":") < 0 || time2.indexOf(":") < 0) {
throw new NumberFormatException("Time Format Error!");
}
String[] time1s = time1.split(":");
String[] time2s = time2.split(":");
String result = "";
try {
result = Integer.toString(Integer.parseInt(time1s[0])
+ Integer.parseInt(time2s[0])
+ (Integer.parseInt(time1s[1]) + Integer
.parseInt(time2s[1])) / 60);
result += ":"
+ Integer.toString((Integer.parseInt(time1s[1]) + Integer
.parseInt(time2s[1])) % 60);
} catch (NumberFormatException exx) {
throw new NumberFormatException("Time Format Error!");
}
return result;
}
}
public class sortTime {
public static void main(String[] args) {
String time1 = "12:45";
String time2 = "0:1";
String time3 = "";
try {
time3 = testAddTime(time1, time2);
} catch (NumberFormatException ex) {
System.out.println("time format Error!");
return;
}
System.out.println(time3);
}
static String testAddTime(String time1, String time2)
throws NumberFormatException {
if (time1.indexOf(":") < 0 || time2.indexOf(":") < 0) {
throw new NumberFormatException("Time Format Error!");
}
String[] time1s = time1.split(":");
String[] time2s = time2.split(":");
String result = "";
try {
result = Integer.toString(Integer.parseInt(time1s[0])
+ Integer.parseInt(time2s[0])
+ (Integer.parseInt(time1s[1]) + Integer
.parseInt(time2s[1])) / 60);
result += ":"
+ Integer.toString((Integer.parseInt(time1s[1]) + Integer
.parseInt(time2s[1])) % 60);
} catch (NumberFormatException exx) {
throw new NumberFormatException("Time Format Error!");
}
return result;
}
}
方法二:
复制内容到剪贴板 程序代码
Date nextTime=new Date(now.getTime()+(7*24*60*60));
方法三:
最好用java.util.Calendar。
复制内容到剪贴板 程序代码
Calendar cl=Calendar.getInstance();
Long clTemp=cl.getTimeInMillis()+7*24*60*60*1000;
cl.setTimeInMillis(clTemp);
/*date即为现在时间加上七天后的时间*/
Date date=cl.getTime();
评论: 0 | 引用: 0 | 查看次数: 846
发表评论