久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Date類型的轉(zhuǎn)換和運(yùn)算

 ankle 2006-09-10
字符串轉(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());
  }
  
 }
}


3:)得到2個(gè)月后的日期:
 import java.util.*;
 import java.text.DateFormat;
 public class test2
 {
  public static void main(String args[]) throws Exception
  {
   String date="2001/11/30";
   
   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()));  
  }
 }

日期加上天數(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é)果為:
Cyc starts at 1054275312432
Cyc ends at 1054275312442
This run took 10ms.

其它:如何格式化小數(shù)

<%@ page import="java.text.*"%>
<%
DecimalFormat df = new DecimalFormat(",###.00");
double aNumber = 33665448856.6568975;
String result = df.format(aNumber);
out.println(result);
%>

輸出結(jié)果為:
33,665,448,856.66

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多