在JSP下如何计算时间差
程序员文章站
2023-12-10 14:46:34
<%@ page contenttype="text/html;charset=gb2312"%> <%@ pa...
<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%
//字符串转化成时间类型(字符串可以是任意类型,只要和simpledateformat中的格式一致即可)
java.text.simpledateformat sdf = new java.text.simpledateformat("m/dd/yyyy hh:mm:ss a",java.util.locale.us);
java.util.date d = sdf.parse("5/13/2003 10:31:37 am");
out.println(d);
out.println("<br/>");
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string mdatetime1=formatter.format(d);
out.println(mdatetime1);
out.println("<br/>");
out.println(d.gettime());
out.println("<br/>");
//当前时间
calendar cal = calendar.getinstance();
// simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss g e d f w w a e f");
string mdatetime=formatter.format(cal.gettime());
out.println(mdatetime);
out.println("<br/>");
//1年前日期
java.util.date mydate=new java.util.date();
long mytime=(mydate.gettime()/1000)-60*60*24*365;
mydate.settime(mytime*1000);
string mdate=formatter.format(mydate);
out.println(mdate);
out.println("<br/>");
//明天日期
mydate=new java.util.date();
mytime=(mydate.gettime()/1000)+60*60*24;
mydate.settime(mytime*1000);
mdate=formatter.format(mydate);
out.println(mdate);
out.println("<br/>");
//两个时间之间的天数
simpledateformat myformatter = new simpledateformat("yyyy-mm-dd");
java.util.date date= myformatter.parse("2003-05-1");
java.util.date mydate= myformatter.parse("1899-12-30");
long day=(date.gettime()-mydate.gettime())/(24*60*60*1000);
out.println(day);
out.println("<br/>");
//加半小时
simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss");
java.util.date date1 = format.parse("2002-02-28 23:16:00");
long time=(date1.gettime()/1000)+60*30;
date1.settime(time*1000);
string mydate1=formatter.format(date1);
out.println(mydate1);
out.println("<br/>");
//年月周求日期
simpledateformat formatter2 = new simpledateformat("yyyy-mm f e");
java.util.date date2= formatter2.parse("2003-05 5 星期五");
simpledateformat formatter3 = new simpledateformat("yyyy-mm-dd");
string mydate2=formatter3.format(date2);
out.println(mydate2);
out.println("<br/>");
//求是星期几
mydate= myformatter.parse("2001-1-1");
simpledateformat formatter4 = new simpledateformat("e");
string mydate3=formatter4.format(mydate);
out.println(mydate3);
out.println("<br/>");
%>
===========================
另一种
import java.text.dateformat;
import java.text.parseexception;
import java.util.date;
class test
{
public static void main(string[] args) throws parseexception
{
dateformat df=dateformat.getdateinstance();
string str1="2002-3-4";
string str2="2002-7-12";
long l1=df.parse(str1).gettime();//把字符串转化为时间
long l2=df.parse(str2).gettime();
long l3=0;//时间间隔
if(l1>l2)//判断时间先后
{
l3=l1-l2;
}else
{
l3=l2-l1;
}
l3=l3/(60*60*24*1000);
system.out.println(str1+"与"+str2+"相隔"+l3+"天!");
}
}
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%
//字符串转化成时间类型(字符串可以是任意类型,只要和simpledateformat中的格式一致即可)
java.text.simpledateformat sdf = new java.text.simpledateformat("m/dd/yyyy hh:mm:ss a",java.util.locale.us);
java.util.date d = sdf.parse("5/13/2003 10:31:37 am");
out.println(d);
out.println("<br/>");
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string mdatetime1=formatter.format(d);
out.println(mdatetime1);
out.println("<br/>");
out.println(d.gettime());
out.println("<br/>");
//当前时间
calendar cal = calendar.getinstance();
// simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss g e d f w w a e f");
string mdatetime=formatter.format(cal.gettime());
out.println(mdatetime);
out.println("<br/>");
//1年前日期
java.util.date mydate=new java.util.date();
long mytime=(mydate.gettime()/1000)-60*60*24*365;
mydate.settime(mytime*1000);
string mdate=formatter.format(mydate);
out.println(mdate);
out.println("<br/>");
//明天日期
mydate=new java.util.date();
mytime=(mydate.gettime()/1000)+60*60*24;
mydate.settime(mytime*1000);
mdate=formatter.format(mydate);
out.println(mdate);
out.println("<br/>");
//两个时间之间的天数
simpledateformat myformatter = new simpledateformat("yyyy-mm-dd");
java.util.date date= myformatter.parse("2003-05-1");
java.util.date mydate= myformatter.parse("1899-12-30");
long day=(date.gettime()-mydate.gettime())/(24*60*60*1000);
out.println(day);
out.println("<br/>");
//加半小时
simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss");
java.util.date date1 = format.parse("2002-02-28 23:16:00");
long time=(date1.gettime()/1000)+60*30;
date1.settime(time*1000);
string mydate1=formatter.format(date1);
out.println(mydate1);
out.println("<br/>");
//年月周求日期
simpledateformat formatter2 = new simpledateformat("yyyy-mm f e");
java.util.date date2= formatter2.parse("2003-05 5 星期五");
simpledateformat formatter3 = new simpledateformat("yyyy-mm-dd");
string mydate2=formatter3.format(date2);
out.println(mydate2);
out.println("<br/>");
//求是星期几
mydate= myformatter.parse("2001-1-1");
simpledateformat formatter4 = new simpledateformat("e");
string mydate3=formatter4.format(mydate);
out.println(mydate3);
out.println("<br/>");
%>
===========================
另一种
import java.text.dateformat;
import java.text.parseexception;
import java.util.date;
class test
{
public static void main(string[] args) throws parseexception
{
dateformat df=dateformat.getdateinstance();
string str1="2002-3-4";
string str2="2002-7-12";
long l1=df.parse(str1).gettime();//把字符串转化为时间
long l2=df.parse(str2).gettime();
long l3=0;//时间间隔
if(l1>l2)//判断时间先后
{
l3=l1-l2;
}else
{
l3=l2-l1;
}
l3=l3/(60*60*24*1000);
system.out.println(str1+"与"+str2+"相隔"+l3+"天!");
}
}