Java tutorial
/** * Copyright (c) 2014 http://www.lushapp.wang * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.lushapp.common.utils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.apache.commons.lang3.StringUtils; /** * ?. * * @author honey.zhao@aliyun.com * @date 2011-12-30?2:29:07 */ @SuppressWarnings("static-access") public final class DateUtil { public static String DATE_FORMAT = "yyyy-MM-dd"; public static final String TIME_FORMAT = "HH:mm:ss"; public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.S"; public static final String TIMEZONE = "GMT+08:00"; /** * ? ?2009-08-01 */ public static String monthFist() { Calendar localTime = Calendar.getInstance(); String strY = null;// int x = localTime.get(Calendar.YEAR); // int y = localTime.get(Calendar.MONTH) + 1; // strY = y >= 10 ? String.valueOf(y) : ("0" + y); // ? return x + "-" + strY + "-01"; // ???yyyy-mm-dd? } /** * ?2009-08-01 */ public static String beforeMonth() { Calendar localTime = Calendar.getInstance(); localTime.add(Calendar.MONTH, -1); // ???? String strz = null; int x = localTime.get(Calendar.YEAR); // int y = localTime.get(Calendar.MONTH) + 1; // strz = y >= 10 ? String.valueOf(y) : ("0" + y); return x + "-" + strz + "-01"; } /** * ? ?2009-08-01 */ public static String curDate() { // ??????? Calendar localTime = Calendar.getInstance(); int x = localTime.get(Calendar.YEAR); int y = localTime.get(Calendar.MONTH) + 1; int z = localTime.get(Calendar.DAY_OF_MONTH); return x + "-" + y + "-" + z; } /** * ?2009-08-01 */ public static String addMonth(String strdate) { Date date = new Date(); // ?? String dateresult = null; // // ?? SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // ? GregorianCalendar gc = new GregorianCalendar(); try { date = df.parse(strdate); // ? } catch (ParseException e) { e.printStackTrace(); } gc.setTime(date); // gc? gc.add(2, 1); // 2?1?(,) // ? gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE)); // ? dateresult = df.format(gc.getTime()); return dateresult; } /** * date1?date2?date1date2true date1date2?2009-08-01 */ public static boolean isDate10Before(String date1, String date2) { try { DateFormat df = DateFormat.getDateInstance(); return df.parse(date1).before(df.parse(date2)); } catch (ParseException e) { e.printStackTrace(); return false; } } /** * ? ?2009-08-01 */ public static String subMonth(String strdate) { Date date = new Date(); // ?? String dateresult = null; // // ?? SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // ? GregorianCalendar gc = new GregorianCalendar(); try { date = df.parse(strdate); // ? } catch (ParseException e) { e.printStackTrace(); } gc.setTime(date); // gc? gc.add(2, -1); // 2?1?(,) // ? gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE)); // ? dateresult = df.format(gc.getTime()); return dateresult; } /** * ? ?2009-08-01 */ public static String subDay(String strdate) { Date date = new Date(); // ?? String dateresult = null; // // ?? SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // ? GregorianCalendar gc = new GregorianCalendar(); try { date = df.parse(strdate); // ? } catch (ParseException e) { e.printStackTrace(); } gc.setTime(date); // gc? gc.add(5, -1); // 2?1?(....5) // ? gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE)); // ? dateresult = df.format(gc.getTime()); return dateresult; } /** * ?2009-08-01 */ public static String addDay(String strdate) { Date date = new Date(); // ?? String dateresult = null; // // ?? SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // ? GregorianCalendar gc = new GregorianCalendar(); try { date = df.parse(strdate); // ? } catch (ParseException e) { e.printStackTrace(); } gc.setTime(date); // gc? gc.add(5, 1); // 2?1?(....5) // ? gc.set(gc.get(gc.YEAR), gc.get(gc.MONTH), gc.get(gc.DATE)); // ? dateresult = df.format(gc.getTime()); return dateresult; } /** * ? ?2009-08-01 */ public static String giveMonthFist(String strdate) { // ?? String strArray[] = strdate.split("-"); String tempyear = strArray[0]; // String tempmonth = strArray[1]; // // ? return tempyear + "-" + tempmonth + "-01"; } /** * ?2009-08-01 */ public static String giveMonthLast(String strdate) { // ? String addmonth = DateUtil.addMonth(strdate); // ? String monthfirst = DateUtil.giveMonthFist(addmonth); // ?? String subday = DateUtil.subDay(monthfirst); return subday; } /** * ? ?2009-08-01 */ public static String giveBeforeMonthFirst(String strdate) { // String beforemonth = DateUtil.subMonth(strdate); // ? return DateUtil.giveMonthFist(beforemonth); } /** * ?2009-08-01 */ public static String giveBeforeMonthLast(String strdate) { // ? String monthfirst = DateUtil.giveMonthFist(strdate); // ?? return DateUtil.subDay(monthfirst); } /** * ?2009-08-01 */ public static String giveyrmo(String yrmoday) { // ?? String strArray[] = yrmoday.split("-"); String tempyear = strArray[0]; // String tempmonth = strArray[1]; // // ? return tempyear + "-" + tempmonth; // ???yyyy-mm? } /** * ?? * * @throws java.text.ParseException * @throws java.text.ParseException */ public static long datesub(Date date1, Date date2) throws ParseException { @SuppressWarnings("unused") long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime() - date2.getTime() : date2.getTime() - date1.getTime(); // ? long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) > 0 ? (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) : (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000); return day + 1; } /** * ? */ public static String giveMonthFist(Integer yr, Integer mo) { // ? if (mo >= 10) { return yr + "-" + mo + "-01"; } else { return yr + "-" + "0" + mo + "-01"; } } /** * ? */ public static String giveYrMo(Integer yr, Integer mo) { // ? if (mo >= 10) { return yr + "-" + mo; } else { return yr + "-" + "0" + mo; } } /** * ?2009-08-01 */ public static Integer retrunmo(String yrmoday) { // ?? String strArray[] = yrmoday.split("-"); String tempmonth = strArray[1]; // return new Integer(tempmonth); } /** * ?2009-08-01 */ public static Integer retrunyr(String yrmoday) { // ?? String strArray[] = yrmoday.split("-"); String tempmonth = strArray[0]; // return new Integer(tempmonth); } /** * bool ?2009-08-01 * * @throws java.text.ParseException */ public static boolean boolcompara(String startdate, String enddate) throws ParseException { if (DateFormat.getDateInstance().parse(startdate) .compareTo(DateFormat.getDateInstance().parse(startdate)) >= 0) { return true; } else { return false; } } /** * * @date1?date2? ? 2008-08-08 16:16:34 * @param date1 * @param date2 * @return */ public static boolean isDateBefore(String date1, String date2) { try { DateFormat df = DateFormat.getDateTimeInstance(); return df.parse(date1).before(df.parse(date2)); } catch (ParseException e) { System.out.print("[SYS] " + e.getMessage()); return false; } } // ??date2? // ? 2005-4-21 16:16:34 public static boolean isDateBefore(String date2) { try { Date date1 = new Date(); DateFormat df = DateFormat.getDateTimeInstance(); return date1.before(df.parse(date2)); } catch (ParseException e) { System.out.print("[SYS] " + e.getMessage()); return false; } } /** * * ??? ? 2008-08-08 16:16:34 */ public static String addHours(String startDate, int intHour) { try { DateFormat df = DateFormat.getDateTimeInstance(); Date date = df.parse(startDate); Calendar cal = Calendar.getInstance(); cal.setTime(date); long longMills = cal.getTimeInMillis() + intHour * 60 * 60 * 1000; cal.setTimeInMillis(longMills); // return df.format(cal.getTime()); } catch (Exception Exp) { return null; } } /** * * ???? ? 2008-08-08 16:16:34 */ public static String delHours(String startDate, int intHour) { try { DateFormat df = DateFormat.getDateTimeInstance(); Date date = df.parse(startDate); Calendar cal = Calendar.getInstance(); cal.setTime(date); long longMills = cal.getTimeInMillis() - intHour * 60 * 60 * 1000; cal.setTimeInMillis(longMills); // return df.format(cal.getTime()); } catch (Exception Exp) { return null; } } /** * ? ? 2008-08-08 */ public static String getCurrentDate() { try { @SuppressWarnings("unused") long longCalendar = 0; // ? Calendar cldCurrent = Calendar.getInstance(); // String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR)); String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1); String strDate = String.valueOf(cldCurrent.get(Calendar.DATE)); // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } // ? longCalendar = Long.parseLong(strYear + strMonth + strDate); // longCalendar += 100L; // ? return String.valueOf(strYear + "-" + strMonth + "-" + strDate); } catch (Exception Exp) { return "2008-08-08"; } } /** * ?2009?03? */ public static String getThisMonthLast(String strdate) { String thisStrDate = strdate + "-01"; // ? String addmonth = DateUtil.addMonth(thisStrDate); // ? String monthfirst = DateUtil.giveMonthFist(addmonth); // ?? String subday = DateUtil.subDay(monthfirst); return subday; } /** * ?long:8? 20090723 */ public static long getLongCalendar() { try { long longCalendar = 0; // ? Calendar cldCurrent = Calendar.getInstance(); // String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR)); String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH)); String strDate = String.valueOf(cldCurrent.get(Calendar.DATE)); // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } // ? longCalendar = Long.parseLong(strYear + strMonth + strDate); // longCalendar += 100L; // ? return longCalendar; } catch (Exception Exp) { return 0; } } /** * * * @param canlendar * 2009080120090802080808 * @return 2009/08/012009/08/01 08:08:08 */ public static String toString(long canlendar) { try { StringBuffer sbCalendar = new StringBuffer(); sbCalendar.insert(0, canlendar); // ?? if (sbCalendar.length() == 8) { sbCalendar.insert(6, "/"); sbCalendar.insert(4, "/"); } else if (sbCalendar.length() == 14) { sbCalendar.insert(12, ":"); sbCalendar.insert(10, ":"); sbCalendar.insert(8, " "); sbCalendar.insert(6, "/"); sbCalendar.insert(4, "/"); } else { // ? return null; } // ?? return sbCalendar.toString(); } catch (Exception Exp) { // ? return null; } } /** * ?Calender?Calender? 20090802080808 ? * */ public static long getLongTime() { try { long longCalendar = 0; // ? Calendar cldCurrent = Calendar.getInstance(); // String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR)); String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1); String strDate = String.valueOf(cldCurrent.get(Calendar.DATE)); String strHour = String.valueOf(cldCurrent.get(Calendar.HOUR)); String strAM_PM = String.valueOf(cldCurrent.get(Calendar.AM_PM)); String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE)); String strSecond = String.valueOf(cldCurrent.get(Calendar.SECOND)); // ?24? // strAM_PM=="1",??strHour?12 if (strAM_PM.equals("1")) { strHour = String.valueOf(Long.parseLong(strHour) + 12); } // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } if (strHour.length() < 2) { strHour = "0" + strHour; } if (strMinute.length() < 2) { strMinute = "0" + strMinute; } if (strSecond.length() < 2) { strSecond = "0" + strSecond; } // ? longCalendar = Long.parseLong(strYear + strMonth + strDate + strHour + strMinute + strSecond); // ? return longCalendar; } catch (Exception Exp) { return 0; } } /** * ? ??,?8?16? "20090808""20090808080808" * "20090808""20090808 08:08:08" */ public static String getDateStringByLongDatetime(long longCalendar) { try { String StrCalendar = String.valueOf(longCalendar); String StrCalendarResult = ""; // if (StrCalendar.length() == 8) { StrCalendarResult = StrCalendar.substring(0, 4) + "" + StrCalendar.substring(4, 6) + "" + StrCalendar.substring(6, 8) + ""; return StrCalendarResult; } // ? if (StrCalendar.length() == 14) { StrCalendarResult = StrCalendar.substring(0, 4) + "" + StrCalendar.substring(4, 6) + "" + StrCalendar.substring(6, 8) + ""; StrCalendarResult = StrCalendarResult + " " + StrCalendar.substring(8, 10) + ":" + StrCalendar.substring(10, 12) + ":" + StrCalendar.substring(12, 14); return StrCalendarResult; } // ? return ""; } catch (Exception e) { // ? e.printStackTrace(); return ""; } } /** * ? ??,?8?16? "20090808""20090808080808" * "2009/08/08""2009/08/08 08:08:08" */ public static String getDateStringByLongDatetimeForPage(long longCalendar) { try { String StrCalendar = String.valueOf(longCalendar); String StrCalendarResult = ""; // if (StrCalendar.length() == 8) { StrCalendarResult = StrCalendar.substring(0, 4) + "/" + StrCalendar.substring(4, 6) + "/" + StrCalendar.substring(6, 8); return StrCalendarResult; } // ? if (StrCalendar.length() == 14) { StrCalendarResult = StrCalendar.substring(0, 4) + "/" + StrCalendar.substring(4, 6) + "/" + StrCalendar.substring(6, 8); StrCalendarResult = StrCalendarResult + " " + StrCalendar.substring(8, 10) + ":" + StrCalendar.substring(10, 12); return StrCalendarResult; } // ? return ""; } catch (Exception e) { // ? e.printStackTrace(); return ""; } } /** * ? ?String:? ?yyyy/mm/dd */ public static String getCurrentDateTime() { return getDateStringByLongDatetimeForPage(getLongCalendar()); } /** * ? ?yyyy/mm/dd */ public static String getCurrentDateView() { // ? Calendar cldCurrent = Calendar.getInstance(); // String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR)); String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1); String strDate = String.valueOf(cldCurrent.get(Calendar.DATE)); // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } // String StrCurrentCalendar = strYear + "/" + strMonth + "/" + strDate; return StrCurrentCalendar; } /** * ?yyyy/mm/dd ??"20090808""20090808080808" */ public static String getCurrentDateView(long longCalendar) { if (longCalendar == 0) { return ""; } String strDateView = String.valueOf(longCalendar); // String strYear = strDateView.substring(0, 4); String strMonth = strDateView.substring(4, 6); String strDate = strDateView.substring(6, 8); // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } // String StrCurrentCalendar = strYear + "/" + strMonth + "/" + strDate; return StrCurrentCalendar; } /** * ? ??,?6? "123143" ?123143 */ public static String getTimeStringByLongTime(long longCalendar) { try { String StrCalendar = String.valueOf(longCalendar); String StrCalendarResult = ""; // if (StrCalendar.length() == 6) { StrCalendarResult = StrCalendar.substring(0, 2) + "" + StrCalendar.substring(2, 4) + "" + StrCalendar.substring(4, 6) + ""; return StrCalendarResult; } // ? return ""; } catch (Exception e) { // ? e.printStackTrace(); return ""; } } /** * ? ??,?6? "123143" ?12:31:43 */ public static String getTimeStringByLongTimeForPage(long longCalendar) { try { String StrCalendar = String.valueOf(longCalendar); String StrCalendarResult = ""; // if (StrCalendar.length() == 6) { StrCalendarResult = StrCalendar.substring(0, 2) + ":" + StrCalendar.substring(2, 4) + ":" + StrCalendar.substring(4, 6); return StrCalendarResult; } // ? return ""; } catch (Exception e) { // ? e.printStackTrace(); return ""; } } /** * Calendar8? */ public static long getLongCalendar(Calendar yourCalendar) { try { long longCalendar = 0; // String strYear = String.valueOf(yourCalendar.get(Calendar.YEAR)); String strMonth = String.valueOf(yourCalendar.get(Calendar.MONTH)); String strDate = String.valueOf(yourCalendar.get(Calendar.DATE)); // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } // ? longCalendar = Long.parseLong(strYear + strMonth + strDate); // longCalendar += 100L; // ? return longCalendar; } catch (Exception Exp) { return 0; } } /** * Calendar? */ public static long getLongTime(Calendar yourCalendar) { try { long longCalendar = 0; // String strYear = String.valueOf(yourCalendar.get(Calendar.YEAR)); String strMonth = String.valueOf(yourCalendar.get(Calendar.MONTH) + 1); String strDate = String.valueOf(yourCalendar.get(Calendar.DATE)); String strHour = String.valueOf(yourCalendar.get(Calendar.HOUR)); String strAM_PM = String.valueOf(yourCalendar.get(Calendar.AM_PM)); String strMinute = String.valueOf(yourCalendar.get(Calendar.MINUTE)); String strSecond = String.valueOf(yourCalendar.get(Calendar.SECOND)); // ?24? // strAM_PM=="1",??strHour?12 if (strAM_PM.equals("1")) { strHour = String.valueOf(Long.parseLong(strHour) + 12); } // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } if (strHour.length() < 2) { strHour = "0" + strHour; } if (strMinute.length() < 2) { strMinute = "0" + strMinute; } if (strSecond.length() < 2) { strSecond = "0" + strSecond; } // ? longCalendar = Long.parseLong(strYear + strMonth + strDate + strHour + strMinute + strSecond); // ? return longCalendar; } catch (Exception Exp) { return 0; } } /** * ?,?? */ public static Calendar getCalendar(long longCalendar) { long longNF = 0; long longYF = 0; long longRZ = 0; long longXS = 0; long longFZ = 0; long longM = 0; GregorianCalendar gc = null; // ?? if (String.valueOf(longCalendar).length() < 14) { longNF = Long.parseLong(String.valueOf(longCalendar).substring(0, 4)); longYF = Long.parseLong(String.valueOf(longCalendar).substring(4, 6)) - 1; longRZ = Long.parseLong(String.valueOf(longCalendar).substring(6)); gc = new GregorianCalendar((int) longNF, (int) longYF, (int) longRZ); } else { longNF = Long.parseLong(String.valueOf(longCalendar).substring(0, 4)); longYF = Long.parseLong(String.valueOf(longCalendar).substring(4, 6)) - 1; longRZ = Long.parseLong(String.valueOf(longCalendar).substring(6, 8)); longXS = Long.parseLong(String.valueOf(longCalendar).substring(8, 10)); longFZ = Long.parseLong(String.valueOf(longCalendar).substring(10, 12)); longM = Long.parseLong(String.valueOf(longCalendar).substring(12)); gc = new GregorianCalendar((int) longNF, (int) longYF, (int) longRZ, (int) longXS, (int) longFZ, (int) longM); } return gc; } /** * ?? */ public static long addMinutes(long longCalendar, int intMin) { try { // long longDate = 0; Calendar cal = getCalendar(longCalendar); long longMills = cal.getTimeInMillis() + intMin * 60 * 1000; cal.setTimeInMillis(longMills); if (String.valueOf(longCalendar).length() < 14) longDate = getLongCalendar(cal); else longDate = getLongTime(cal); // return longDate; } catch (Exception Exp) { return -1; } } /** * ??? */ public static long addHours(long longCalendar, int intHour) { try { // long longDate = 0; Calendar cal = getCalendar(longCalendar); long longMills = cal.getTimeInMillis() + intHour * 60 * 60 * 1000; cal.setTimeInMillis(longMills); if (String.valueOf(longCalendar).length() < 14) longDate = getLongCalendar(cal); else longDate = getLongTime(cal); // return longDate; } catch (Exception Exp) { return -1; } } /** * ?? */ public static long addDays(long longCalendar, int intDay) { try { // long longDate = 0; Calendar cal = getCalendar(longCalendar); long longMills = cal.getTimeInMillis() + intDay * 24 * 60 * 60 * 1000; cal.setTimeInMillis(longMills); if (String.valueOf(longCalendar).length() < 14) longDate = getLongCalendar(cal); else longDate = getLongTime(cal); // return longDate; } catch (Exception Exp) { return -1; } } /** * ?? */ public static long addWeeks(long longCalendar, int intWeek) { try { // long longDate = 0; Calendar cal = getCalendar(longCalendar); long longMills = cal.getTimeInMillis() + intWeek * 7 * 24 * 60 * 60 * 1000; cal.setTimeInMillis(longMills); if (String.valueOf(longCalendar).length() < 14) longDate = getLongCalendar(cal); else longDate = getLongTime(cal); // return longDate; } catch (Exception Exp) { return -1; } } /** * ?? */ public static long addMonths(long longCalendar, int intMonth) { try { long longNF = 0; long longYF = 0; long longRZ = 0; long longDate = 0; long longNIAN = 0; String strYF = ""; String strRZ = ""; longNF = Long.parseLong(String.valueOf(longCalendar).substring(0, 4)); longYF = Long.parseLong(String.valueOf(longCalendar).substring(4, 6)); longRZ = Long.parseLong(String.valueOf(longCalendar).substring(6, 8)); longYF = longYF + intMonth; if (longYF > 12) { longNIAN = (long) Math.floor(longYF / 12); longYF = longYF % 12; if (longYF == 0) { longYF = 12; } longNF = longNF + longNIAN; } // ? if (longRZ >= 28) longRZ = getNormalDay(longNF, longYF, longRZ); if (longYF < 10) strYF = "0" + String.valueOf(longYF); else strYF = String.valueOf(longYF); if (longRZ < 10) strRZ = "0" + String.valueOf(longRZ); else strRZ = String.valueOf(longRZ); // ?? if (String.valueOf(longCalendar).length() < 14) { longDate = Long.parseLong(String.valueOf(longNF) + strYF + strRZ); } else { longDate = Long.parseLong( String.valueOf(longNF) + strYF + strRZ + String.valueOf(longCalendar).substring(8, 14)); } // return longDate; } catch (Exception Exp) { return -1; } } /** * ????? ?long calendar ?, int intWeek * long:?? */ public static long getNormalDay(long longNF, long longYF, long longRZ) { try { // ??????? // ? if (longYF == 2) { if ((longNF % 4) == 0) { if (longRZ > 28) longRZ = 29; } else longRZ = 28; } if (longRZ == 31) { if (longYF == 4 || longYF == 6 || longYF == 9 || longYF == 11) longRZ = 30; } return longRZ; } catch (Exception Exp) { return -1; } } /** * */ public static String getStringCalendarAndWeek() { try { String strCalendar = ""; // ? Calendar cldCurrent = Calendar.getInstance(); // String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR)); String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1); String strDate = String.valueOf(cldCurrent.get(Calendar.DATE)); // ?? if (strMonth.length() < 2) { strMonth = "0" + strMonth; } if (strDate.length() < 2) { strDate = "0" + strDate; } // ? strCalendar = strYear + "" + strMonth + "" + strDate + ""; int intWeek = cldCurrent.get(Calendar.DAY_OF_WEEK); String strWeek = ""; switch (intWeek) { case 1: strWeek = ""; break; case 2: strWeek = ""; break; case 3: strWeek = ""; break; case 4: strWeek = ""; break; case 5: strWeek = ""; break; case 6: strWeek = ""; break; case 7: strWeek = ""; break; } strCalendar = strCalendar + " " + strWeek + " "; // ? return strCalendar; } catch (Exception Exp) { return ""; } } public static String Text2HtmlToPageContent(String text) { // if (text == null) { return ""; } // ????HTML? String strSource = text; // ? StringBuffer sbTarget = new StringBuffer(); // ?? char[] charArraySource = { '<', '>', '&', '"', '\n' }; // ??HTML? String[] strArrayTarget = { "<", ">", "&", """, "<br>" }; // ?? int intStart = 0; // ??? for (int i = 0; i < strSource.length(); i++) { // ???? for (int j = 0; j < charArraySource.length; j++) { // ???? if (strSource.charAt(i) == charArraySource[j]) { sbTarget.append(strSource.substring(intStart, i)); sbTarget.append(strArrayTarget[j]); intStart = i + 1; continue; } } } // ??? sbTarget.append(strSource.substring(intStart)); // ??? return sbTarget.toString(); } public static String getDateStringByLongDate(long longCalendar) { try { String StrCalendar = String.valueOf(longCalendar); String StrCalendarResult = ""; StrCalendarResult = StrCalendar.substring(0, 4) + "" + StrCalendar.substring(4, 6) + "" + StrCalendar.substring(6, 8) + ""; return StrCalendarResult; } catch (Exception e) { // ? e.printStackTrace(); return ""; } } public static String getJi(String strYue) { int intYue = Integer.decode(strYue).intValue(); if (intYue >= 1 && intYue <= 3) { return "1"; } else if (intYue >= 4 && intYue <= 6) { return "2"; } else if (intYue >= 7 && intYue <= 9) { return "3"; } else { return "4"; } } public static Date parse(String dateString, String dateFormat) { return parse(dateString, dateFormat, Date.class); } @SuppressWarnings("unchecked") public static <T extends Date> T parse(String dateString, String dateFormat, Class<T> targetResultType) { if (StringUtils.isEmpty(dateString)) return null; DateFormat df = new SimpleDateFormat(dateFormat); try { long time = df.parse(dateString).getTime(); Date t = targetResultType.getConstructor(long.class).newInstance(time); return (T) t; } catch (ParseException e) { String errorInfo = "cannot use dateformat:" + dateFormat + " parse datestring:" + dateString; throw new IllegalArgumentException(errorInfo, e); } catch (Exception e) { throw new IllegalArgumentException("error targetResultType:" + targetResultType.getName(), e); } } public static String format(Date date, String dateFormat) { if (date == null) return null; return new SimpleDateFormat(dateFormat).format(date); } }