Several useful small util functions for Java (date processing and HTTP)

Source: Internet
Author: User

/**
* Returns the current date according to the date is the day ordinal of a year
* @param date
* @return
*/
public static int orderDate (String datestr) {

if (Datestr = = NULL | | Datestr.trim (). Length () = = 0) return 0;

int datesum = 0;
int year = integer.valueof (datestr.substring (0, 4));
int month = integer.valueof (datestr.substring (5, 7));
int day = integer.valueof (datestr.substring (8, 10));
for (int i = 1; i < month; i++) {
switch (i) {
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
Datesum + = 31;
Break
Case 4:
Case 6:
Case 9:
Case 11:
Datesum + = 30;
Break
Case 2:
if (((year% 4 = = 0) & (year% 100! = 0)) | (Year% 400 = = 0))
Datesum + = 29;
Else
Datesum + = 28;
}
}
return datesum = Datesum + day;

}


/**
* @ Get a formatted date (days) collection between two dates
*/
public static list<string> Getmmddlist (string start, String end) {
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
SimpleDateFormat sdfout = new SimpleDateFormat ("MMdd");
list<string> list = new arraylist<string> ();
try {
Date Date_start = Sdf.parse (start);
Date date_end = Sdf.parse (end);
Date date = Date_start;
Calendar cd = Calendar.getinstance ();


while (Date.gettime () <= date_end.gettime ()) {
List.add (Sdfout.format (date));
Cd.settime (date);
Cd.add (calendar.date, 1);//Add a day
Date = Cd.gettime ();
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return list;
}


/**
*
* @ Method Name: testwsdlconnection
* @ Function Description: Test WebService address is available
* @return
*/
public static Boolean testwsdlconnection (String address) {
Boolean flag = false;
try {
URL urlobj = new URL (address);
HttpURLConnection OC = (httpurlconnection) urlobj.openconnection ();
Oc.setusecaches (FALSE);
Oc.setconnecttimeout (5000);Set timeout time 5s
int status = Oc.getresponsecode ();//Request Status
if (= = status) {
return true;
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}catch (Exception e) {
E.printstacktrace ();
}
return flag;
}

Several useful small util functions for Java (date processing and HTTP)

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.