How to calculate time difference in JSP

Source: Internet
Author: User
Tags date1 dateformat

<% @ Page contentType = "text/html; charset = gb2312" %>

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

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

<%

// Convert a string to a time type (the string can be of 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'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/> ");

// Add half an hour

SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");

Java. util. Date date1 = format. parse ("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/> ");

// Calculate the date of year, month, and week

SimpleDateFormat formatter2 = new SimpleDateFormat ("yyyy-mm f e ");

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

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
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 (); // converts a string to a time
Long l2 = df. parse (str2). getTime ();
Long l3 = 0; // Time Interval
If (l1> l2) // determine the time sequence
{
L3 = L1-L2;
} Else
{
L3 = l2-l1;
}
L3 = l3/(60*60*24*1000 );
System. out. println (str1 + "and" + str2 + "separated by" + 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.