Several Issues about Date and Time Processing in JSP

Source: Internet
Author: User
Several Issues about Date and Time Processing in JSP

1. How to calculate the interval between two times?
Interval = date1.gettime ()-date2.gettime (); the number of milliseconds is obtained.
Except 1000, it is second, and 60 is minute, in addition, 60 is the hour ..............................

Remember that all the time classes in the java standard library are converted based on this, but he has written some
The conversion method is only available for you, but it is essential that the number of milliseconds is sufficient.

2. t = calendar. getinstance (); M = T. Get (T. month) + 1; why should I add one here?

In Java, the value range of month of date is: 0 ~ 11. It is 1 different from the natural expression of people.

3. What is the difference between the system time and the current date?

The exact system time is
System. currenttimemillis ();
New Date () is the current date, although it gettime (); and system. currenttimemillis ();
Same, but system. currenttimemillis ();

4. How do I calculate the day difference between two dates?

Long begintime = begindate. gettime ();
Long endtime2 = enddate. gettime ();
Long betweendays = (long) (endtime-begintime)/(1000*60*60*24) + 0.5 );

5. How to compare the date and time size?

Method 1:
Use Calendar Object to compare
Java. util. Calendar class can be used to compare date. In order to do this,
You guy shoshould parse that string into int year, month, day and construct
Calendar Object, and then do comparison.

Below is a sample

Stringtokenizer token = new stringtokenizer (your string ,"-");
Int year = integer. parseint (token. nexttoken ());
Int month = integer. parseint (token. nexttoken ());
Int day = integer. parseint (token. nexttoken ());
Calendar date = calendar. getinstance ();
Date. Set (year, month, day );
Calendar today = calendar. getinstacne ();
If (date. After (today )){
//......
}
Method 2
Date nowdate = new date (); // current time/R
Long nowtime = nowdate. gettime;
Long lasttime = usertime. longvalue (); // previous time/R
Long time = nowtime-lasttime; // time subtraction comparison.
If (Time> (long) 60000) // 1 minute {}

For more information, see
Use the timestamp, date. gettime () to change the current time to the timestamp,
Use compareto ();
Use before (), after (), equals ();

6. Date formatting problem/R

Purpose:
First Request date
Java. Text. simpledateformat formatter = new java. Text. simpledateformat ("yyyy-mm-dd ");
String riqi = formatter. Format (currenttime_1 );
Second time/R
Java. Text. dateformat format1 = new java. Text. simpledateformat ("hhmmss ");
Java. util. Date currenttime_2 = new java. util. Date ();
String Shijian = format1.format (currenttime_2 );
The result is:
2002-02-19 and 115324 (11: 53: 24 seconds)

Implementation:
Java. Text. simpledateformat formatter = new java. Text. simpledateformat ("yyyy-mm-dd-H-mm-SS ");
Java. util. Date currenttime_1 = new java. util. Date ();
String str_date = formatter. Format (currenttime_1 );
Stringtokenizer token = new stringtokenizer (str_date ,"-");
String year = token. nexttoken ();
String month = token. nexttoken ();
String day = token. nexttoken ();
String HH = token. nexttoken ();
String Mm = token. nexttoken ();
String Ss = token. nexttoken ();
String riqi = year + "year //" + month + "month" + day + "day" + "" + HH + "Point //" + mm + "points" + SS + "seconds //";
String newdir = year + month + Day;
String wenjian = HH + mm + SS;

7. How can I get the number of days in a month?

Java. util. Calendar date = java. util. Calendar. getinstance ();
System. Out. println (date. getactualmaximum (date. day_of_month ));

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.