public class BusinessDate { public BusinessDate() { } /** * 取得当天日期,格式 2009-02-11 * @return */ public static String getToday() { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd"); Calendar cl = new GregorianCalendar(); return sdf.format(cl.getTime()); } /** * 取得当天日期时间,格式 2009-02-11 23:9:21 * @return */ public static String getTodaytime() { Calendar cl = new GregorianCalendar(); return getToday() + " " + cl.get(Calendar.HOUR_OF_DAY) cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " "; }
+
":"
+
/** * 取得当前时间,格式 23:12:20 * @return */ public static String getTime() { Calendar cl = new GregorianCalendar(); return cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " "; } /** * 取得当前小时 * @return */ public static int getHour() { Calendar cl = new GregorianCalendar(); return cl.get(Calendar.HOUR_OF_DAY); }
/** * 取得当前日期 格式为 20090211 * @return */ public static String getNoFormatToday() { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd"); Calendar cl = new GregorianCalendar(); return sdf.format(cl.getTime()); } /** * 取得当前时间 格式为 231611 * @return */ public static String getNoFormatTime() { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("HHmmss"); Calendar cl = new GregorianCalendar(); return sdf.format(cl.getTime()); } /** * 取得当前年份 * @return */ public static String getYear() { return BusinessDate.getNoFormatToday().substring(0, 4); } /** * 取得当前月份 * @return */ public static String getMonth() { return BusinessDate.getNoFormatToday().substring(4, 6); } /** * 取得当前日 * @return */ public static String getDay() { return BusinessDate.getNoFormatToday().substring(6,8 }
) ;
/** * 返回昨天的日期 格式 2009-02-10 * @return */ public static String getYesterday() { String strYesterday = ""; Calendar cale = null; cale = new GregorianCalendar(); cale.add(Calendar.DATE, -1); strYesterday = BusinessDate.getStrByCalendar(cale); return strYesterday; } public static String getStrByCalendar(Calendar cale) { return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(cale.getTime()); } /** * 日期字符串的格式转换,例如"2009-02-11"转换为 2009 年 2 月 11 日 * @param sDate * @return */ public static String getChnDateString(String sDate) { if (sDate == null) { return null; } sDate = sDate.trim(); if (sDate.length() == 7) { sDate += "-01"; } StringTokenizer st = new StringTokenizer(sDate, "-"); int year = 2100; int month = 0; int day = 1; try { year = Integer.parseInt(st.nextToken()); month = Integer.parseInt(st.nextToken()) - 1; day = Integer.parseInt(st.nextToken()); } catch (Exception e) { e.printStackTrace(); } Calendar cl = new GregorianCalendar(year, month, day); return cl.get(Calendar.YEAR) + " 年 " + (cl.get(Calendar.MONTH) + 1) + " 月 " + cl.get(Calendar.DATE) + "日";
} /** * 取得某年
某月的最后一天 * @param year * @param month * @return */ public static String getMaxDayOfMonth(int year, int month) { Calendar cal = new GregorianCalendar(year, month - 1, 1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } /** * 取得某年某月的第一天 * @param year * @param month * @return