Java date Summary

Source: Internet
Author: User

 1. How to calculate the interval between two time ranges 16: 281. How to calculate the interval between two time ranges? <Br/> interval = date1.gettime ()-date2.gettime (); the number of milliseconds is obtained. <br/> Except 1000, the value is second, and 60 is minute, in addition, 60 is the hour .............................. </P> <p> remember that all the time classes in the java standard library are converted based on this, but he has written some <br/> conversion methods for you to use. however, this number of milliseconds is indispensable. </P> <p> 2. T = calendar. getinstance (); M = T. get (T. month) + 1; why add one here? </P> <p> in Java, the value range of month of date is: 0 ~ 11. It is 1 different from the natural expression of people. </P> <p> 3. What is the difference between the system time and the current date? </P> <p> the exact system time is <br/> system. currenttimemillis (); <br/> new date () is the current date, although it gettime (); and system. currenttimemillis (); <br/> same, but system. currenttimemillis (); </P> <p> 4. how do I calculate the number of days between two dates? </P> <p> long begintime = begindate. gettime (); <br/> long endtime2 = enddate. gettime (); <br/> long betweendays = (long) (endtime-begintime)/(1000*60*60*24) + 0.5 ); </P> <p> 5. how to compare the date and time? </P> <p> Method 1: <br/> Use Calendar Object to compare <br/> JAVA. util. calendar class can be used to compare date. in order to do this, <br/> you guy shoshould parse that string into int year, month, day and construct a <br/> Calendar Object, and then do comparison. </P> <p> below is a sample </P> <p> stringtokenizer token = new stringtokenizer (your string ,"-"); <br/> int year = integer. parseint (token. nexttoken () ); <Br/> int month = integer. parseint (token. nexttoken (); <br/> int day = integer. parseint (token. nexttoken (); <br/> calendar date = calendar. getinstance (); <br/> date. set (year, month, day); <br/> calendar today = calendar. getinstacne (); <br/> If (date. after (today) {<br/> //...... <br/>}< br/> method 2 <br/> date nowdate = new date (); // current time r <br/> long nowtime = nowdate. gettime; <br/> long lasttime = usertime. longvalue (); // Previous time r <br/> long time = nowtime-lasttime; // time subtraction comparison. <Br/> If (Time> (long) 60000) // 1 minute {}</P> <p> For more information, see <br/> timestamp, date. gettime () can change the current time to a timestamp. <br/> Use compareto (); <br/> use before (), after (), equals (); </P> <p> 6. date formatting problem r </P> <p> purpose: <br/> calculate the date for the first time <br/> JAVA. text. simpledateformat formatter = new Java. text. simpledateformat ("yyyy-mm-dd"); <br/> string riqi = formatter. format (currenttime_1); <br/> second time evaluate the time r <br/> JAVA. text. dateformat format1 = new Java. text. simpledateforma T ("hhmmss"); <br/> JAVA. util. date currenttime_2 = new Java. util. date (); <br/> string Shijian = format1.format (currenttime_2); <br/> the result is <br/> and 115324) </P> <p> Implementation: <br/> JAVA. text. simpledateformat formatter = new Java. text. simpledateformat ("yyyy-mm-dd-H-mm-SS"); <br/> JAVA. util. date currenttime_1 = new Java. util. date (); <br/> string str_date = formatter. format (currenttime_1); <br/> stri Ngtokenizer token = new stringtokenizer (str_date, "-"); <br/> string year = token. nexttoken (); <br/> string month = token. nexttoken (); <br/> string day = token. nexttoken (); <br/> string HH = token. nexttoken (); <br/> string Mm = token. nexttoken (); <br/> string Ss = token. nexttoken (); <br/> string riqi = year + "year/" + month + "month" + day + "day" + "+ HH +" point/"+ mm +" points "+ SS +" seconds /"; <br/> string newdir = year + month + day; <br/> string wenj Ian = HH + mm + SS; </P> <p> 7. How do I get the number of days in a month? </P> <p> JAVA. util. calendar date = Java. util. calendar. getinstance (); <br/> system. out. println (date. getactualmaximum (date. day_of_month ));

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.