字符串轉(zhuǎn)換成時(shí)間及時(shí)間相減: 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:) 兩個(gè)日期相減 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()); } } }
grc.add(GregorianCalendar.MONTH,2); System.out.println("grc="+dateFormat.format(grc.getTime())); } } 日期加上天數(shù)的運(yùn)算: 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、計(jì)算日期之間的間隔
<%@ page import="java.util.Date"%> <%@ page import="java.text.DateFormat"%> <% String input = "2003-05-01"; 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."); %> 輸出結(jié)果為: Difference is 29 days. 8,、日期的加減運(yùn)算 方法:用Calendar類的add()方法 <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <% Calendar now = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd ‘a(chǎn)t‘ 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())); %> 輸出結(jié)果為: 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("2000-01-01"); Date d2 = df.parse("1999-12-31"); 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); %> 輸出結(jié)果為: Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999 10、記錄一件事所花費(fèi)的時(shí)間 方法:調(diào)用兩次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."); %> 輸出結(jié)果為: |
|
來(lái)自: ankle > 《我的圖書(shū)館》