工具類中常要用到的方法,字符,日期,數(shù)據(jù)轉(zhuǎn)換,日期比較等等 |
ninjar 原創(chuàng) 更新:2006-04-30 10:30:37 版本: 1.0 |
|
/** * 比較當(dāng)前日期和指定日期 return boolean * 如果當(dāng)前日期在指定日期之前返回true否則返回flase */ public static boolean dateCompare(String str){ boolean bea = false; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String isDate = sdf.format(new java.util.Date()); java.util.Date date1; java.util.Date date0; try { date1 = sdf.parse(str); date0= sdf.parse(isDate); if(date0.after(date1)){ bea = true; } } catch (ParseException e) { bea = false; } /** * 將字符串轉(zhuǎn)換成日期 * @param str * @return */ public static Timestamp getStrToDate(String str){ Date date = null; try{ date = java.sql.Date.valueOf(str); }catch(Exception e){ } return new Timestamp(date.getTime()); } /** * 輸出字符串 * @param obj * @return */ public static String getStr(Object obj){ if(obj==null) return ""; return obj.toString(); } /** * 將日期轉(zhuǎn)換成字符串 * @param date * @return */ public static String getDateToStr(Date date) { if(date == null){ return ""; } SimpleDateFormat sdate= new SimpleDateFormat ("yyyy-MM-dd"); return sdate.format(date); } /** * @return Timestamp */ public static Timestamp now() { java.util.Date now = new java.util.Date(); String year = ""+(now.getYear()+1900); String month = "0"+(now.getMonth()+1); String day = "0"+now.getDate(); String hour = "0"+now.getHours(); String minute= "0"+now.getMinutes(); String second = "0"+now.getSeconds(); month = month.substring(month.length()-2,month.length()); day = day.substring(day.length()-2,day.length()); hour = hour.substring(hour.length()-2,hour.length()); minute = minute.substring(minute.length()-2,minute.length()); second = second.substring(second.length()-2,second.length()); try { return datetimeToTimestamp(""+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second); } catch(GeneralException e) { e.printError(); return null; } } /** * 將字符串轉(zhuǎn)換成日期 * @param str * @return */ public static Timestamp getStrToDate(String str){ Date date = null; try{ date = java.sql.Date.valueOf(str); }catch(Exception e){ } return new Timestamp(date.getTime()); } /** * 比較當(dāng)前日期和指定日期 return boolean * 如果當(dāng)前日期在指定日期之前返回true否則返回flase */ public static boolean dateCompare(String str){ boolean bea = false; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String isDate = sdf.format(new java.util.Date()); java.util.Date date1; java.util.Date date0; try { date1 = sdf.parse(str); date0= sdf.parse(isDate); if(date0.after(date1)){ bea = true; } } catch (ParseException e) { bea = false; } return bea; }
|
|
|