Java (JavaScript) processing time (including the month, year, and day)

Source: Internet
Author: User

This is mainly because of the difference in the month, the year, or even the difference between the day and the following, (the day and its next, you can use the gettime () method to directly deal with it, then it is the calculation of time. For example, gettime ()/1000/60/60/24 is the second, minute, hour, day ,)

 

Let's talk about the requirement. The first step is to facilitate database query. Because the log table is one day or multiple days, we need to calculate the table name here. Here we will use a month table as an example, that is, write a log table every month. therefore, the first step is to determine the table. check the data, maybe with little experience. in some background Java processing, Oracle does not know whether there is a better way to handle it, so this stupid method is used. directly look up the table, where the table is the most important. Let's first know the table.

 

It is possible that this write is not very complete, saying that there are months, years, days, but may not be so full, but the processing of the month is actually integrated with the content that many people have used, the summary here is not very good, mainly because of continuous improvement and deep learning in the work.

But I 'd like to share with you a little bit of experience, although it is a basic problem!

 

The following figure shows the number of months. Two time parameters are used to calculate the number of months. There is still a problem with the time format. Please define the time according to your needs! (I will use the following .)

 

// Time processing, total number of months of time. <br/> date ST = new simpledateformat ("yyyy-mm-dd hh: mm: SS "). parse (starttime); <br/> date ET = new simpledateformat ("yyyy-mm-dd hh: mm: SS "). parse (endtime); </P> <p> calendar CA1 = calendar. getinstance (); <br/> calendar Ca 2 = calendar. getinstance (); <br/> ca1.settime (ST); <br/> ca2.settime (ET); </P> <p> int ca1year = ca1.get (calendar. year); <br/> int ca1month = ca1.get (calendar. month); </P> <p> int ca2year = ca2.get (calendar. year); <br/> int ca2month = ca2.get (calendar. month); <br/> int countmonth = 0; // This is an integer used to calculate the number of months, <br/> If (ca1year = ca2year) {<br/> countmonth = ca2month-ca1month; <br/>}else {<br/> countmonth = (ca2year-ca1year) * 12 + (ca2month-ca1month ); <br/>}< br/> // The number of years here. You can see the corresponding number of years. You can subtract the number directly, so we should be able to understand. <br/>

 

The number of months is obtained, followed by the processing.
Countmonth is used for several months.

// Process and obtain the time table. <br/> // set the table name <br/> // number of months to be obtained in a loop. <br/> for (int count = 0; count <= countmonth; count ++) {<br/> string tblafter = dateadd (count, starttime); <br/> // system. out. println ("-----> month:" + tblafter); <br/> string sttime = "user_oper_log" + tblafter; <br/> If (checktblname (sttime )) {<br/> If (COUNT = 0) {</P> <p>} else if (COUNT = countmonth) {</P> <p >}else {</P> <p >}< br/> // a method is used for processing. obtain the month. <br/> Public String dateadd (INT count, string starttime) {<br/> string returnstr = ""; <br/> try {<br/> calendar CA = calendar. getinstance (); <br/> date = new simpledateformat ("yyyy-mm-dd hh: mm: SS "). parse (starttime); <br/> Ca. settime (date); <br/> Ca. add (calendar. month, count); <br/> int month = Ca. get (calendar. month) + 1; <br/> If (month <10) {<br/> returnstr = string. valueof (ca. get (calendar. year) + "0" + String. valueof (month); <br/>}else {<br/> returnstr = string. valueof (ca. get (calendar. year) + String. valueof (month); <br/>}</P> <p >}catch (exception e) {<br/> system. out. println ("table name setting method exception:" + E. getmessage (); <br/>}< br/> return returnstr; <br/>}< br/> 

 

It is almost the same here, because the time is obtained, the table is also obtained, and then the data is processed and the query statement is enough.
 
Javascript is similar to Java at the beginning. It is similar to Java at the beginning, although it is very simple.


I can only write so much for the time being. Maybe these two days are quite busy, so I am so confused to write it. Wait for some time to handle it. Let's just talk about it. Haha!

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.