Java computing time

Source: Internet
Author: User
Java acquisition time to milliseconds
 
  Calendar Cd = calendar. getinstance ();
   
    Int YY = CD. Get (calendar. year );
    Int Mm = CD. Get (calendar. month) + 1;
    Int dd = CD. Get (calendar. date );
    Int HH = CD. Get (calendar. hour_of_day );
    Int nn = CD. Get (calendar. Minute );
    Int Ss = CD. Get (calendar. Second );
    Int MI = CD. Get (calendar. millisecond );
   
    String timenow = YY + "/" + mm + "/" + dd + "" + HH + "." + nn + "." + SS + "." + Mi;You can calculate the time difference!
Put the code block in it.
The thread running time is similar.
Public static void testrunningtime (){
Long T1 = system. currenttimemillis ();
// Invoke your program here
Long T2 = system. currenttimemillis ();
System. Out. println ("your program has executed"
+ (INT) (t2-t1)/1000) + "seconds"
+ (T2-t1) % 1000) + "micro seconds ");
}
/**
* Calculate the previous day of the specified date
*
* @ Param datetime
* Date, in the format of yyyy-mm-dd
* @ Return
*/
Public static string getbeforeday (string datetime ){
Calendar now = calendar.Getinstance();
Simpledateformat simpledate = new simpledateformat ("yyyy-mm-dd ");
Date = NULL;
Try{
Date = simpledate. parse (datetime );
} Catch (parseexception ex ){
System.Out. Println ("the date format does not meet the requirements:" + ex. getmessage ());
Return NULL;
}
Now. settime (date );
Int year = now. Get (calendar. year );
Int month = now. Get (calendar.Month );
Int day = now. Get (calendar.Day_of_month)-1;
Now. Set (year, month, day );
String time = simpledate. Format (now. gettime ());
ReturnTime;
}


/**
* Calculate the next day of the specified date
*
* @ Param datetime
* Date, in the format of yyyy-mm-dd
* @ Return
*/
Public staticString getnextday (string datetime ){
Calendar now = calendar.Getinstance();
Simpledateformat simpledate =NewSimpledateformat ("yyyy-mm-dd ");
Date =Null;
Try{
Date = simpledate. parse (datetime );
}Catch(Parseexception ex ){
System.Out. Println ("the date format does not meet the requirements:" + ex. getmessage ());
Return Null;
}
Now. settime (date );
IntYear = now. Get (calendar.Year);
IntMonth = now. Get (calendar.Month);
IntDay = now. Get (calendar.Day_of_month) + 1;
Now. Set (year, month, day );
String time = simpledate. Format (now. gettime ());
ReturnTime;
}

/**
* Get the number of days in the specified month.
* @ Param _ year
* @ Param _ month
* @ Return
*/
Public static intGetmaxdayofmonth (Int_ Year,Int_ Month ){
Calendar now = calendar.Getinstance();
IntYear = 0;
IntMonth = 0;
If(_ Month = 1 ){
Year = _ year-1;
Month = 12;
}Else{
Year = _ year;
Month = _ month-1;
}

Now. Set (calendar.Year, Year );
Now. Set (calendar.Month, Month );

ReturnNow. getactualmaximum (calendar.Date);
}

/**
* Calculation time difference
*
* @ Param begintime
* Start Time. Format: yyyy-mm-dd hh: mm: SS
* @ Param endtime
* End Time. Format: yyyy-mm-dd hh: mm: SS
* @ Return the time difference between the start time and the end time (in seconds)
*/
Public static longGettimedifference (string begintime, string endtime ){
LongBetween = 0;
Simpledateformat SDF =NewSimpledateformat ("yyyy-mm-dd hh: mm: SS ");

Date end =Null;
Date begin =Null;
Try{// Convert the intercepted time string to a Time Format String
End = SDF. parse (endtime );
Begin = SDF. parse (begintime );
}Catch(Parseexception e ){
E. printstacktrace ();
}

Between = (end. gettime ()-begin. gettime ()/1000; // divide by 1000 to convert to seconds

ReturnBetween;
}

/**
* Calculation time difference
*
* @ Param time
* The specified time. Format: yyyy-mm-dd hh: mm: SS
* @ Return the time difference between the current time and the specified time (in seconds)
*/
Public static longGettimedifference (string time ){
LongBetween = 0;
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
String systemtime = SDF. Format (NewDate (). tostring ();

Date end =Null;
Date begin =Null;
Try{// Convert the intercepted time string to a Time Format String
End = SDF. parse (time );
Begin = SDF. parse (systemtime );
}Catch(Parseexception e ){
E. printstacktrace ();
}

Between = math.ABS(End. gettime ()-begin. gettime ()/1000; // divide by 1000 to convert to seconds.

ReturnBetween;
}

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.