<%@pagecontentType="text/html;charset=gb2312"%>
<%@pageimport="java.text.*"%>
<%@pageimport="java.util.*"%>
<%
//字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可)
java.text.SimpleDateFormatsdf=newjava.text.SimpleDateFormat("M/dd/yyyyhh:mm:ssa",java.util.Locale.US);
java.util.Dated=sdf.parse("5/13/200310:31:37AM");
out.println(d);
out.println("<br/>");
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
StringmDateTime1=formatter.format(d);
out.println(mDateTime1);
out.println("<br/>");
out.println(d.getTime());
out.println("<br/>");
//当前时间
Calendarcal=Calendar.getInstance();
//SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-ddHH:mm:ssGEDFwWaEF");
StringmDateTime=formatter.format(cal.getTime());
out.println(mDateTime);
out.println("<br/>");
//1年前日期
java.util.DatemyDate=newjava.util.Date();
longmyTime=(myDate.getTime()/1000)-60*60*24*365;
myDate.setTime(myTime*1000);
StringmDate=formatter.format(myDate);
out.println(mDate);
out.println("<br/>");
//明天日期
myDate=newjava.util.Date();
myTime=(myDate.getTime()/1000)+60*60*24;
myDate.setTime(myTime*1000);
mDate=formatter.format(myDate);
out.println(mDate);
out.println("<br/>");
//两个时间之间的天数
SimpleDateFormatmyFormatter=newSimpleDateFormat("yyyy-MM-dd");
java.util.Datedate=myFormatter.parse("2003-05-1");
java.util.Datemydate=myFormatter.parse("1899-12-30");
longday=(date.getTime()-mydate.getTime())/(24*60*60*1000);
out.println(day);
out.println("<br/>");
//加半小时
SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
java.util.Datedate1=format.parse("2002-02-2823:16:00");
longTime=(date1.getTime()/1000)+60*30;
date1.setTime(Time*1000);
Stringmydate1=formatter.format(date1);
out.println(mydate1);
out.println("<br/>");
//年月周求日期
SimpleDateFormatformatter2=newSimpleDateFormat("yyyy-MMFE");
java.util.Datedate2=formatter2.parse("2003-055星期五");
SimpleDateFormatformatter3=newSimpleDateFormat("yyyy-MM-dd");
Stringmydate2=formatter3.format(date2);
out.println(mydate2);
out.println("<br/>");
//求是星期几
mydate=myFormatter.parse("2001-1-1");
SimpleDateFormatformatter4=newSimpleDateFormat("E");
Stringmydate3=formatter4.format(mydate);
out.println(mydate3);
out.println("<br/>");
%>
===========================
另一种
importjava.text.DateFormat;
importjava.text.ParseException;
importjava.util.Date;
classtest
{
publicstaticvoidmain(String[]args)throwsParseException
{
DateFormatdf=DateFormat.getDateInstance();
Stringstr1="2002-3-4";
Stringstr2="2002-7-12";
longl1=df.parse(str1).getTime();//把字符串转化为时间
longl2=df.parse(str2).getTime();
longl3=0;//时间间隔
if(l1>l2)//判断时间先后
{
l3=l1-l2;
}else
{
l3=l2-l1;
}
l3=l3/(60*60*24*1000);
System.out.println(str1+"与"+str2+"相隔"+l3+"天!");
}
}