字符串轉換成時間及時間相減:
1:) SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.MM.dd");
//假定像2002.07.04的是合法日期其餘都非法。
String str="2002.07.04";
ParsePosition pos = new ParsePosition(0);
Date dt=formatter.parse(str,pos);
if(dt!=null)
{
//是合法日期
}
else
{
//非法日期
}
2:)
兩個日期相減
import java.util.*;
import java.text.*;
class a
{
public static void main(String[] args)
{
String s1 = "2003/08/15 17:15:30";
String s2 = "2002/09/14 14:18:37";
try{
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
ParsePosition pos1 = new ParsePosition(0);
Date dt1=formatter.parse(s1,pos);
Date dt2=formatter.parse(s2,pos1);
System.out.println("dt1="+dt1);
System.out.println("dt2="+dt2);
long l = dt1.getTime() - dt2.getTime();
System.out.println("Hello World!="+l);
}catch(Exception e){
System.out.println("exception"+e.toString());
}
}
}
3:)獲得2個月後的日期:
import java.util.*;
import java.text.DateFormat;
public class test2
{
public static void main(String args[]) throws Exception
{
String date="
DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.MEDIUM);
GregorianCalendar grc=new GregorianCalendar();
grc.setTime(new Date(date));
grc.add(GregorianCalendar.MONTH,2);
System.out.println("grc="+dateFormat.format(grc.getTime()));
}
}
日期加上天數的運算:
import java.util.*;
.....
java.sql.Date d;
.....
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, 3); // 加3天
java.util.Date t = c.getTime();
d = new java.sql.Date(t.getTime());
7、計算日期之間的間隔
<%@ page import="java.util.Date"%>
<%@ page import="java.text.DateFormat"%>
<%
String input = "
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = null;
try{
d1 = formatter.parse(input);
}catch(ParseException e){
out.println("unparseable using " + formatter);
}
Date d2 = new Date();
long diff = d2.getTime() - d1.getTime();
out.println("Difference is " + (diff/(1000*60*60*24)) + " days.");
%>
輸出結果爲:
Difference is 29 days.
8、日期的加減運算
方法:用Calendar類的add()方法
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd ‘at‘ hh:mm:ss a zzz");
out.println("It is now " + formatter.format(now.getTime()));
now.add(Calendar.DAY_OF_YEAR,-(365*2));
out.println("<br>");
out.println("Two years ago was " + formatter.format(now.getTime()));
%>
輸出結果爲:
It is now 星期五 2003.05.30 at 01:45:32 下午 CST
Two years ago was 星期三 2001.05.30 at 01:45:32 下午 CST
9、比較日期
方法:用equals()、before()、after()方法
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%
DateFormat df = new SimpleDateFormat("yyy-MM-dd");
Date d1 = df.parse("
Date d2 = df.parse("
String relation = null;
if(d1.equals(d2))
relation = "the same date as";
else if(d1.before(d2))
relation = "before";
else
relation = "after";
out.println(d1 +" is " + relation + ‘ ‘ + d2);
%>
輸出結果爲:
Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999
10、記錄一件事所花費的時間
方法:調用兩次System.getTimeMillis()方法,求差值
<%@ page import="java.text.*"%>
<%
long t0,t1;
t0 = System.currentTimeMillis();
out.println("Cyc starts at " + t0);
int k = 0;
for(int i =0;i<100000;i++){
k += i;
}
t1 = System.currentTimeMillis();
out.println("<br>");
out.println("Cyc ends at " + t1);
out.println("<br>");
out.println("This run took " + (t1-t0) + "ms.");
%>
輸出結果爲:
Cyc starts at 1054275312432
Cyc ends at 1054275312442
This run took 10ms.
其它:如何格式化小數
<%@ page import="java.text.*"%>
<%
DecimalFormat df = new DecimalFormat(",###.00");
double aNumber = 33665448856.6568975;
String result = df.format(aNumber);
out.println(result);
%>
輸出結果爲:
33,665,448,856.66ci