码迷,mamicode.com
首页 > 编程语言 > 详细

Java的几个有用小Util函数(日期处理和http)

时间:2017-06-18 19:01:29      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:http   超时   gettime   int   lex   lag   substring   ddr   false   

    /**
     * 依据日期返回当前日期是一年的第几天
     * @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;

}


/**
* @ 获得两个日期之间的 格式化的日期(天)集合
*/
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);// 添加一天
date = cd.getTime();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}


    /**

* @方法名:testWsdlConnection
* @功能说明:測试webservice地址是否可用
* @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); //设置超时时间5s
int status = oc.getResponseCode();//请求状态
if(200 == status){
return true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return flag;
}    

Java的几个有用小Util函数(日期处理和http)

标签:http   超时   gettime   int   lex   lag   substring   ddr   false   

原文地址:http://www.cnblogs.com/zhchoutai/p/7044907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!