How to calculate the time difference under JSP

Time:2021-10-23

<%@ page contentType=”text/html;charset=gb2312″%> 

<%@ page import=”java.text.*”%> 

<%@ page import=”java.util.*”%> 

<% 

//Convert string to time type (string can be any type, as long as it is consistent with the format in 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/>”); 

//Current time  

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/>”); 

//Date 1 year ago  

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/>”); 

//Tomorrow date  

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/>”); 

//Number of days between two times  

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/>”); 

//Plus half an hour  

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/>”); 

//Year month week date  

SimpleDateFormat formatter2 = new SimpleDateFormat(“yyyy-MM F E”); 

java.util.Date   date2=   formatter2.parse(“2003-05   five   Friday “);  

SimpleDateFormat formatter3 = new SimpleDateFormat(“yyyy-MM-dd”); 

String mydate2=formatter3.format(date2); 

out.println(mydate2); 

out.println(“<br/>”); 

//What day is it  

mydate= myFormatter.parse(“2001-1-1”); 

SimpleDateFormat formatter4 = new SimpleDateFormat(“E”); 

String mydate3=formatter4.format(mydate); 

out.println(mydate3); 

out.println(“<br/>”); 

%> 
===========================
Another
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();// Convert string to time
  long l2=df.parse(str2).getTime();
   long   l3=0;// time interval
   If (L1 > L2) / / judge the time sequence
  {
   l3=l1-l2;
  }else
  {
   l3=l2-l1;
  }
  l3=l3/(60*60*24*1000);
   System. Out. Println (STR1 + “and” + STR2 + “apart” + L3 + “days!”);   
 }