import java.util.*;
/** * 处理日期相关功能的类 * <p>Copyright: 2004</p> * @author Filippo Guan * @version 1.1 */ public class DateFormat implements Comparable{ //年 protected String year; //月 protected String month; //日 protected String day; //小时 protected String hour; //分钟 protected String minute; //秒钟 protected String second;
//------------------------------------------------------------------------------ /** * 构造一个DateFormat,值为当前系统日期/时间 */ public DateFormat(){ // Date date = new Date(); Calendar date = Calendar.getInstance(); year = String.valueOf(date.get(Calendar.YEAR)); month = String.valueOf(date.get(Calendar.MONTH) + 1); day = String.valueOf(date.get(Calendar.DATE));
year = StringFunction.updateZero(year, 4); month = StringFunction.updateZero(month, 2); day = StringFunction.updateZero(day, 2);
hour = String.valueOf(date.get(Calendar.HOUR_OF_DAY)); minute = String.valueOf(date.get(Calendar.MINUTE)); second = String.valueOf(date.get(Calendar.SECOND));
hour = StringFunction.updateZero(hour, 2); minute = StringFunction.updateZero(minute, 2); second = StringFunction.updateZero(second, 2); }
/** * 根据指定日期构造一个DateFormat,如果指定的日期不合法,则抛出一个错误信息 * @param _date 指定的日期 */ public DateFormat(String _date){ this(_date, false); }
public DateFormat(String _date, boolean _timeFlag){ // this(); if (_date == null) { throw new NullPointerException(); } String inputDate; if (_timeFlag) { inputDate = StringFunction.getDateTimeFormat(_date); if (!checkDateTime(inputDate)) throw new Error("日期不合法"); } else { inputDate = StringFunction.getDateFormat(_date); if (!checkDate(inputDate)) throw new Error("日期不合法"); }
year = inputDate.substring(0, 4); month = inputDate.substring(4, 6); day = inputDate.substring(6, 8); if (_timeFlag) { hour = inputDate.substring(8, 10); minute = inputDate.substring(10, 12); second = inputDate.substring(12, 14); } else { hour = "00"; minute = "00"; second = "00"; } } /** * 根据指定的日期和时间构造一个DateFormat, * 如果指定的日期和时间不合法,则抛出一个错误信息 * @param _date 指定的日期 * @param _ho 指定的小时 * @param _mi 指定的分钟 * @param _se 指定的秒钟 */ public DateFormat(String _date, String _ho, String _mi, String _se){ this(); if (!checkDate(_date)) throw new NullPointerException("日期不合法"); if (!checkTime(_ho, _mi, _se)) throw new NullPointerException("时间不合法"); year = _date.substring(0, 4); month = _date.substring(4, 6); day = _date.substring(6, 8); hour = _ho; minute = _mi; second = _se; }
public DateFormat(Date d){ Calendar date = Calendar.getInstance(); date.setTime(d); year = String.valueOf(date.get(Calendar.YEAR)); month = String.valueOf(date.get(Calendar.MONTH) + 1); day = String.valueOf(date.get(Calendar.DATE));
year = StringFunction.updateZero(year, 4); month = StringFunction.updateZero(month, 2); day = StringFunction.updateZero(day, 2);
hour = String.valueOf(date.get(Calendar.HOUR_OF_DAY)); minute = String.valueOf(date.get(Calendar.MINUTE)); second = String.valueOf(date.get(Calendar.SECOND));
hour = StringFunction.updateZero(hour, 2); minute = StringFunction.updateZero(minute, 2); second = StringFunction.updateZero(second, 2); } //------------------------------------------------------------------------------ /** * 获取当前系统日期/时间 * @return 当前系统日期/时间 */ public synchronized static DateFormat getCurrentDate(){ return new DateFormat(); }
//------------------------------------------------------------------------------ /** * 设定日期, * 如果设定的日期不合法,则抛出一个出错信息 * @param _date 设定的日期值 */ public void setDate(String _date){ if (!checkDate(_date)) throw new NullPointerException("日期不合法"); year = _date.substring(0, 4); month = _date.substring(4, 6); day = _date.substring(6, 8); }
/** * 设定日期, * 如果设定的日期不合法,则抛出一个错误信息 * @param _yyyy 年 * @param _mm 月 * @param _dd 日 */ public void setDate(int _yyyy, int _mm, int _dd){ String yyyy = StringFunction.updateZero(String.valueOf( _yyyy), 4); String mm = StringFunction.updateZero(String.valueOf(_mm), 2); String dd = StringFunction.updateZero(String.valueOf(_dd), 2); setDate(yyyy + mm + dd); }
/** * 设定时间, * 如果设定的时间不合法,则抛出一个错误信息 * @param _ho 小时 * @param _mi 分钟 * @param _se 秒钟 */ public void setTime(String _ho, String _mi, String _se){ if (!checkTime(_ho, _mi, _se)) return; hour = _ho; minute = _mi; second = _se; }
//------------------------------------------------------------------------------ //取出成员变量的值/以规范输出的方式取出成员变量的值 public String getYear(){ return year; }
public String getMonth(){ return month; }
public String getDay(){ return day; }
public String getMonthDate(){ return year + month; }
public String getDate(){ return year + month + day; }
public String getHour(){ return hour; }
public String getMinute(){ return minute; }
public String getSecond(){ return second; }
public String getTime(){ return hour + minute + second; }
public String getPrintMonth(){ return year + "-" + month; }
public String getPrintDate(){ return year + "-" + month + "-" + day; }
public String getPrintTime(){ return hour + ":" + minute + ":" + second; }
public String getPrintDateTime(){ return getPrintDate() + " " + getPrintTime(); }
public String getSQLDate() { return "TO_DATE('" + getDate() + "','YYYYMMDD')"; }
//------------------------------------------------------------------------------ // /** * 检查日期是否合法 * @param _date 需要检查的日期 * @return 如果日期合法则为true,否则为false */ public synchronized static boolean checkDate(String _date){ if (_date == null) { return false; } if (_date.length() < 8) return false; String yy = _date.substring(0, 4); String mm = _date.substring(4, 6); String dd = _date.substring(6, 8);
try { int iyy = Integer.parseInt(yy); int imm = Integer.parseInt(mm); int idd = Integer.parseInt(dd);
if (iyy > 2050 || iyy < 1950){ return false; } if (imm < 1 || imm > 12) return false; int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm)); if (idd < 1 || idd > iMaxMonthDay) return false; return true; } catch (Exception e){ System.err.println(e.getMessage()); return false; } }
/** * 检查日期是否合法 * @param _date 需要检查的日期 * @return 如果日期合法则为true,否则为false */ public synchronized static boolean checkDateTime(String _date){ if (_date == null) { return false; } if (_date.length() < 14) return false; String yy = _date.substring(0, 4); String mm = _date.substring(4, 6); String dd = _date.substring(6, 8); String ho = _date.substring(8, 10); String mi = _date.substring(10, 12); String se = _date.substring(12, 14);
try { int iyy = Integer.parseInt(yy); int imm = Integer.parseInt(mm); int idd = Integer.parseInt(dd);
if (iyy > 2050 || iyy < 1950){ return false; } if (imm < 1 || imm > 12) return false; int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm)); if (idd < 1 || idd > iMaxMonthDay) return false; int iho = Integer.parseInt(ho); int imi = Integer.parseInt(mi); int ise = Integer.parseInt(se);
if (iho < 0 || iho > 24) return false; if (imi < 0 || imi > 60) return false; if (ise < 0 || ise > 60) return false; return true; } catch (Exception e){ System.err.println(e.getMessage()); return false; } }
/** * 检查时间是否合法 * @param _ho 小时 * @param _mi 分钟 * @param _se 秒钟 * @return 如果合法则为true,否则为false */ public static synchronized boolean checkTime(String _ho, String _mi, String _se){ try { int iho = Integer.parseInt(_ho); int imi = Integer.parseInt(_mi); int ise = Integer.parseInt(_se);
if (iho < 0 || iho > 24) return false; if (imi < 0 || imi > 60) return false; if (ise < 0 || ise > 60) return false; return true; } catch (Exception e){ System.err.println(e.getMessage()); return false; } } //------------------------------------------------------------------------------ // /** * 获得某月的最大的天数 * @param yy 年 * @param mm 月 * @return 该月的最大天数 */ public static synchronized String getMaxMonthDay(String yy,String mm){
Calendar cal = Calendar.getInstance(); try{ int y = Integer.parseInt(yy); int m = Integer.parseInt(mm); cal.set(y, m-1, 1); } catch(Exception e){ System.err.println( " DataFunction Exception " + e.getMessage()); } String maxDay = String.valueOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return maxDay; }
/** * 获得某月的最大天数 * @param _yyyymm 年月 * @return 该月的最大天数 */ public static synchronized String getMaxMonthDay(String _yyyymm){ String yy = _yyyymm.substring(0, 4); String mm = _yyyymm.substring(4, 6); return getMaxMonthDay(yy, mm); } //------------------------------------------------------------------------------ // /** * 得到过去的某个月 * @param _mm 向前推算的月数 * @return 该月的日期 */ public DateFormat getPreviousMonth(int _mm){ if (_mm < 0) return getNextMonth(-_mm); int iyy = Integer.parseInt(year); int imm = Integer.parseInt(month); while (imm - _mm <= 0) { _mm -= 12; iyy--; } imm -= _mm;
String yy = String.valueOf(iyy); String mm = String.valueOf(imm);
yy = StringFunction.updateZero(yy, 4); mm = StringFunction.updateZero(mm, 2);
DateFormat df = new DateFormat(yy + mm + "01"); return df; }
/** * 得到将来的某个月 * @param _mm 向后推算的月数 * @return 该月的日期 */ public DateFormat getNextMonth(int _mm){ if (_mm < 0) return getPreviousMonth(-_mm); int iyy = Integer.parseInt(year); int imm = Integer.parseInt(month); while (_mm + imm > 12) { _mm -= 12; iyy++; } imm += _mm;
String yy = String.valueOf(iyy); String mm = String.valueOf(imm);
yy = StringFunction.updateZero(yy, 4); mm = StringFunction.updateZero(mm, 2);
DateFormat df = new DateFormat(yy + mm + "01"); return df; }
/** * 得到上个月 * @return 该月的日期 */ public DateFormat getPreviousMonth(){ return getPreviousMonth(1); }
/** * 得到下个月 * @return 该月的日期 */ public DateFormat getNextMonth(){ return getNextMonth(1); } //------------------------------------------------------------------------------ // /** * 得到过去的某日 * @param _day 向前推算的天数 * @return 该日的日期 */ public DateFormat getPreviousDay(int _day){ if (_day < 0) return getNextDay(-_day); int iyy = Integer.parseInt(year); int imm = Integer.parseInt(month); int idd = Integer.parseInt(day); DateFormat df = new DateFormat(); df.setDate(iyy, imm, idd);
if (idd - _day > 0){ idd -= _day; df.setDate(iyy, imm, idd); } else { _day -= idd; df = df.getPreviousMonth(); df.setDate(df.getMonthDate() + getMaxMonthDay(df.getMonthDate())); df = df.getPreviousDay(_day); } return df; }
/** * 得到将来的某日 * @param _day 向后推算的天数 * @return 该日的日期 */ public DateFormat getNextDay(int _day){ if (_day < 0) return getPreviousDay(-_day); int iyy = Integer.parseInt(year); int imm = Integer.parseInt(month); int idd = Integer.parseInt(day); DateFormat df = new DateFormat(); df.setDate(iyy, imm, idd);
if (idd + _day <= Integer.parseInt(df.getMaxMonthDay(df.getMonthDate()))){ idd += _day; df.setDate(iyy, imm, idd); } else { _day -= Integer.parseInt(df.getMaxMonthDay(df.getMonthDate())) - idd + 1; df = df.getNextMonth(); df.setDate(df.getMonthDate() + "01"); df = df.getNextDay(_day); } return df; } //------------------------------------------------------------------------------ // /** * 获得某日的过去几个月的日期 * @param _date 指定的日期 * @param _month 向前推算的月数 * @return 结果的日期 */ public synchronized static DateFormat getPreviousMonth(String _date, int _month){ if (!DateFormat.checkDate(_date)) { throw new NullPointerException("日期不正确"); } return new DateFormat(_date).getPreviousMonth(_month); }
/** * 获得某日的将来几个月的日期 * @param _date 指定的日期 * @param _month 向后推算的日期 * @return 结果的日期 */ public synchronized static DateFormat getNextMonth(String _date, int _month){ if (!DateFormat.checkDate(_date)) { throw new NullPointerException("日期不正确"); } return new DateFormat(_date).getNextMonth(_month); }
/** * 获得某日的过去若干天的日期 * @param _date 指定的日期 * @param _day 向前推算的日期 * @return 结果的日期 */ public synchronized static DateFormat getPreviousDay(String _date, int _day){ if (!DateFormat.checkDate(_date)) { throw new NullPointerException("日期不正确"); } return new DateFormat(_date).getPreviousDay(_day); }
/** * 获得某日的将来若干天的日期 * @param _date 指定的日期 * @param _day 向后推算的日期 * @return 结果的日期 */ public synchronized static DateFormat getNextDay(String _date, int _day){ return new DateFormat(_date).getNextDay(_day); } //------------------------------------------------------------------------------ // /** * 比较两个日期的前后 * @param _date1 日期1 * @param _date2 日期2 * @return 比较的结果, * 如果>0,则_date1>_date2;=0,两个日期相等;<0,则_date1<_date2 */ public synchronized static int compare(String _date1, String _date2){ if (!checkDate(_date1)) { throw new NullPointerException("日期不正确"); } if (!checkDate(_date2)) { throw new NullPointerException("日期不正确"); } int nDate1 = Integer.parseInt(_date1); int nDate2 = Integer.parseInt(_date2); return nDate1 - nDate2; } //------------------------------------------------------------------------------ public String toString(){ if (getHour() == null || getMinute() == null || getSecond() == null) { return getDate(); } return getDate() + getTime(); } //------------------------------------------------------------------------------ public boolean equals(Object o){ if (o instanceof DateFormat) { return toString().equals(o.toString()); } else { DateFormat obj = new DateFormat(o.toString(), true); return equals(obj); } } //------------------------------------------------------------------------------ /** * 比较大小 * @param o * @return */ public int compareTo(Object o){ if (o instanceof DateFormat) { return toString().compareTo(o.toString()); } else { DateFormat obj = new DateFormat(o.toString(), true); return compareTo(obj); } }
//------------------------------------------------------------------------------ /** * 返回两个日期的距离天数 * @param startDate 开始日期 * @param endDate 结束日期 * @return */ public static long getPeriodDayCount(String startDate, String endDate){ Date d1 = new DateFormat(startDate).toDate(); Date d2 = new DateFormat(endDate).toDate(); long periodTime = d2.getTime() - d1.getTime(); return periodTime/24/60/60/1000; } //------------------------------------------------------------------------------ public Date toDate(){ Calendar c = Calendar.getInstance(); int iy = Integer.parseInt(getYear()); c.set(Calendar.YEAR, Integer.parseInt(getYear())); c.set(Calendar.MONTH, Integer.parseInt(getMonth()) -1); c.set(Calendar.DATE, Integer.parseInt(getDay())); c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(getHour())); c.set(Calendar.MINUTE, Integer.parseInt(getMinute())); c.set(Calendar.SECOND, Integer.parseInt(getSecond()));
return c.getTime(); } //------------------------------------------------------------------------------ public static void main(String[] args) { try { System.out.println(DateFormat.getPeriodDayCount("2003-06-01","2003-06-07")); } catch (Exception e){ e.printStackTrace(); } } }
需要用到的StringFunction类
import java.util.*;
/** * 处理字符串相关功能的类 * <p>Copyright: 2004</p> * @author Filippo Guan * @version 1.1 */ public class StringFunction { private String str;
public StringFunction(String _Str) { str = _Str; }
/** * 判别两个字符串是否匹配,其中'*'代表任意长的字符串(包括0长字符串),'?'代表单字节字符 * @param _Str 传入的字串 * @return 若不匹配,则返回false */ public boolean like(String _Str) { /* if (_Str.length() == 0) { return false; } int i = 0; int j = 0; while (j < _Str.length()) { if (i >= this.str.length()) { return false; } char ch = _Str.charAt(j); if (ch == '*') { do { j++; if (j >= _Str.length()) { return true; } } while (_Str.charAt(j) == '*' || _Str.charAt(j) == '?'); i = this.str.indexOf(_Str.charAt(j), i); if (i == -1) { return false; } else { i++; j++; } continue; } else if (ch == '?') { i++; j++; continue; } else if (ch == this.str.charAt(i)) { i++; j++; } else { return false; } } return true; */ return str.matches(".*" + _Str + ".*"); }
public synchronized static boolean like(String str1, String str2) { return new StringFunction(str1).like(str2); }
//------------------------------------------------------------------------------ // /** * 将字符串扩充到长度为num,如果超过长度则去除超过的部分,否则则在前补0 * @param num 指定扩充到的长度 * @return 返回的字串 */ public String updateZero(int num) { return updateChar(num, '0'); }
public synchronized static String updateZero(String str, int num) { return new StringFunction(str).updateZero(num); }
//------------------------------------------------------------------------------ // /** * 将字符串扩充到长度为num,如果超过长度则去除超过的部分, * 否则则在前面不上某个字符 * @param num 指定扩充到的长度 * @param c 补上的字符 * @return 返回的字串 */ public String updateChar(int num, char c) { if (str.length() == num) { return str; } else if (str.length() < num) { for (int i = str.length(); i < num; i++) { str = c + str; } return str; } else { return str.substring(str.length() - num); } }
//------------------------------------------------------------------------------ // /** * 如果字符串不为null,那么就在两面加上"", * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代 * @param s 传入的字串 * @return 返回的字串 */ public synchronized static String setNull(String s) { if (s != null) { if (!s.equals("")) { s = "'" + s + "'"; } else { s = null; } } return s; }
/** * 如果字符串为"",那么就把它设为null。 * 该方法一般用于书写Sql语句中处理日期时, * 其功能已被SqlExpression中的类似方法所取代 * @param s 传入的字串 * @return 返回的字串 */ public synchronized static String setDateNull(String s) { if (s != null) { if (s.equals("")) { s = null; } } return s; }
/** * 将数字转换为字符串,并在的两边加上引号, * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代 * @param s 传入的数字 * @return 返回的字串 */ public synchronized static String setNull(long s) { return "'" + String.valueOf(s) + "'"; }
/** * 将数字转换为字符串,并在的两边加上引号, * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代 * @param s 传入的数字 * @return 返回的字串 */ public synchronized static String setNull(double s) { return "'" + String.valueOf(s) + "'"; }
/** * 在字符串的末尾加上换行的符号 * @param s 传入的字串 * @return 返回的字串 */ public synchronized static String addReturn(String s) { return s + "\n"; }
/** * 如果字符串为空,则返回一个" "符号 * @param s 传入的字串 * @return 返回的字串 */ public synchronized static String getHtmlFormat(String s) { if (s != null) { if (s.trim().equals("")) { return " "; } else { return s.trim(); } } else { return " "; } }
//------------------------------------------------------------------------------ // /** * 如果传入的字符串为null,则返回一个"" * @param s 传入的字串 * @return 返回的字串 */ public synchronized static String getStringFormat(Object o) { if (o == null) { return ""; } else { String s = o.toString(); return s.trim(); } }
//------------------------------------------------------------------------------ // /** * 保证字符串可以转化为数值型,若不能则返回"0" * @param s 传入的字串 * @return 返回的字串 */ public synchronized static String getNumberFormat(Object o) { if (o == null) { return "0"; } String s = o.toString(); try { double dou = Double.parseDouble(s); } catch (Exception e) { return "0"; } return s; }
public static double getDouble(Object o) { return Double.parseDouble(getNumberFormat(o)); } public static long getLong(Object o) { return (long) getDouble(o); } public static int getInt(Object o) { return (int) getDouble(o); } public static float getFloat(Object o) { return (float) getDouble(o); } //------------------------------------------------------------------------------ // /** * 把一个字符串转换成字符串YYYYMMDD的形式 * @param _date 传入的字串 * @return 返回的字串 */ public static synchronized String getDateFormat(String _date) { if (_date == null) { return ""; } if (_date.length() == 8) { return _date; } if (_date.length() > 8) { return _date.substring(0, 4) + _date.substring(5, 7) + _date.substring(8, 10); } return ""; } //------------------------------------------------------------------------------ // /** * 把一个字符串转换成字符串YYYYMMDDHHMISS的形式 * @param _date 传入的字串 * @return 返回的字串 */ public static synchronized String getDateTimeFormat(String _date) { if (_date == null) { return ""; } if (_date.length() == 8) { _date += "000000"; } if (_date.length() == 14) { return _date; } if (_date.length() > 14) { return _date.substring(0, 4) + _date.substring(5, 7) + _date.substring(8, 10) + _date.substring(11, 13) + _date.substring(14, 16) + _date.substring(17, 19); } return ""; }
//------------------------------------------------------------------------------ // /** * 获取一个字符串的绝对长度(如果遇到汉字字符则算两个) * @param s 传入的字串 * @return 字串的绝对长度 */ public static int absoluteLength(String s) { if (s == null) { return 0; } try { return new String(s.getBytes("GB2312"), "8859_1").length(); } catch (Exception e) { return s.length(); } }
//------------------------------------------------------------------------------ // /** * 对一个字符串的绝对长度进行拆解(如果遇到汉字字符会把它当作两个字符处理) * @param s 传入的字串 * @param start 起始绝对位置 * @param end 终止绝对位置 * @return 返回的字串 */ public static String absoluteSubstring(String s, int start, int end) { if (s == null) { return null; } try { String s2 = new String(s.getBytes("GB2312"), "8859_1"); s2 = s2.substring(start, end); return new String(s2.getBytes("8859_1"), "GB2312"); } catch (Exception e) { return s.substring(start, end); } }
/** * 在一个字符串两边加上"-"号 * @param s 传入的字串 * @param num 加上"-"号的个数 * @return 返回的字串 */ public static String addLine(String s, int num) { for (int i = 0; i < num; i++) { s = "-" + s + "-"; } return s; }
/** * 扩充一个字串,使其绝对长度为指定的长度,如果过长就截断,过短就补充指定的字串 * @param str 传入的字串 * @param updateStr 填充的字串 * @param num 指定的长度 * @param flag 填补字符串的位置:true的话在前面填补、false在后面填补 * @return 返回的字串 */ public static String updateAbsoluteLength( String str, String updateStr, int num, boolean flag) { if (updateStr == null) { return str; } if (str == null) { str = ""; for (int i = 0; i < num; i++) { str += updateStr; } return str; } if (absoluteLength(str) == num) { return str; } else if (absoluteLength(str) < num) { for (int i = absoluteLength(str); i < num; i++) { if (flag) { str = updateStr + str; } else { str = str + updateStr; } } return str; } else { return absoluteSubstring(str, 0, absoluteLength(str) - num); } }
public static String updateAbsoluteLength( String str, String updateStr, int num) { return updateAbsoluteLength(str, updateStr, num, true); }
//------------------------------------------------------------------------------ public static void main(String[] args) { String c = new String("管振宇"); String c2 = new String("刘蓓丽"); String c3 = new String("administrator"); System.out.println(StringFunction.updateAbsoluteLength(c, "& ", 28)); System.out.println(StringFunction.updateAbsoluteLength(c2, "& ", 28)); System.out.println(StringFunction.updateAbsoluteLength(c3, "& ", 28)); }
}

|