How to calculate time difference _jsp programming under the JSP

Source: Internet
Author: User
Tags date1 dateformat time interval
<%@ page contenttype= "text/html;charset=gb2312"%>

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

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

<%

The string is converted to a time type (the 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 A E F");

String Mdatetime=formatter.format (Cal.gettime ());

Out.println (Mdatetime);

Out.println ("<br/>");

Date 1 years 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's 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/>");


Date of month and week

SimpleDateFormat formatter2 = new SimpleDateFormat ("yyyy-mm F E");

Java.util.Date date2= formatter2.parse ("2003-05 5 Friday");

SimpleDateFormat Formatter3 = new SimpleDateFormat ("Yyyy-mm-dd");

String Mydate2=formatter3.format (DATE2);

Out.println (MYDATE2);

Out.println ("<br/>");

The day of the week

Mydate= myformatter.parse ("2001-1-1");

SimpleDateFormat formatter4 = new SimpleDateFormat ("E");

String Mydate3=formatter4.format (mydate);

Out.println (Mydate3);

Out.println ("<br/>");

%>
===========================
Another kind
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&GT;L2)//Judgment time successively
{
L3=L1-L2;
}else
{
L3=L2-L1;
}
l3=l3/(60*60*24*1000);
System.out.println (str1+ "and" +str2+ "separated" +l3+ "Days!");
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.