该工具类实现字符串日期的相互转换,包含当前日期常用相关处理(前一年、上一月、前半年、前一个小时、上周最后一天或第一天),具体代码如下
import java.sql.Timestamp;@b@import java.text.DateFormat;@b@import java.text.ParseException;@b@import java.text.SimpleDateFormat;@b@import java.util.ArrayList;@b@import java.util.Calendar;@b@import java.util.Date;@b@import java.util.List;@b@@b@/**@b@ * 日期工具类@b@ */@b@public class DateUtils {@b@@b@ private final static String pattern = "yyyy-MM-dd HH:mm:ss";@b@ private final static String forPattern = "yyyy-M-d";@b@ private final static String forPatternB = "yyyy-MM-dd";@b@ private final static String forPatternC = "yyyyMMdd";@b@ private final static String forPatternD = "yyyyMMddHHmm";@b@@b@ public static String dateToString() {@b@ SimpleDateFormat sdf = new SimpleDateFormat(pattern);@b@ return sdf.format(new Date());@b@ }@b@@b@ /**@b@ * 根据时间来获得月初@b@ * @b@ * @param object @b@ * @return getFirstDateOfCurMonth@b@ * @throws ParseException@b@ */@b@ public static String getFirstDateByCurDate(String date)@b@ throws ParseException {@b@ Calendar first = stringToCalendar(date);@b@ int minDay = first.getMinimum(Calendar.DAY_OF_MONTH);@b@ minDay = first.getMinimum(Calendar.DAY_OF_MONTH);@b@ first.set(Calendar.DAY_OF_MONTH, minDay);@b@ SimpleDateFormat sdf = new SimpleDateFormat(forPattern);@b@ return sdf.format(first.getTime());@b@ }@b@@b@ public static Calendar stringToCalendar(String date) throws ParseException {@b@ Calendar calendar = Calendar.getInstance();@b@ Date firstDate = null;@b@ try {@b@ firstDate = DateUtils.stringToDate(date, forPattern);@b@ } catch (ParseException e) {@b@ e.printStackTrace();@b@ }@b@ calendar.setTime(firstDate);@b@ return calendar;@b@ }@b@@b@ public static String dateToString(Date object) {@b@ if (null == object) {@b@ return null;@b@ }@b@ SimpleDateFormat sdf = new SimpleDateFormat(pattern);@b@ return sdf.format(object);@b@@b@ }@b@@b@ public static Date stringToDate(String date) throws ParseException {@b@ SimpleDateFormat sdf = new SimpleDateFormat(pattern);@b@ return sdf.parse(date);@b@ }@b@@b@ public static Date stringToDate(String date, String pattern)@b@ throws ParseException {@b@ if (date == null || "".equals(date.trim())@b@ || "null".equals(date.trim())) {@b@ return null;@b@ }@b@ SimpleDateFormat sdf = new SimpleDateFormat(pattern);@b@ return sdf.parse(date);@b@ }@b@@b@ public static String getStrNowDateHmsMs() {@b@ Date tdate = new Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ int len = nowtime.length();@b@ int between = 23 - len;@b@ for (int i = 0; i < between; i++) {@b@ nowtime = nowtime + "0";@b@ }@b@ String nowYear = nowtime.substring(0, 4);@b@ String nowMonth = nowtime.substring(5, 7);@b@ String nowDay = nowtime.substring(8, 10);@b@ String nowHour = nowtime.substring(11, 13);@b@ String nowMinute = nowtime.substring(14, 16);@b@ String nowSecond = nowtime.substring(17, 19);@b@ String nowMilliSecond = nowtime.substring(20, 23);@b@ String nowdate = nowYear + nowMonth + nowDay + nowHour + nowMinute@b@ + nowSecond + nowMilliSecond;@b@ return nowdate;@b@ }@b@@b@ /**@b@ * 20030801@b@ */@b@ public static String getStrNowDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 10);@b@ String nowYear = nowtime.substring(0, 4);@b@ String nowMonth = nowtime.substring(5, 7);@b@ String nowDay = nowtime.substring(8, 10);@b@ String nowdate = nowYear + nowMonth + nowDay;@b@ return nowdate;@b@@b@ }@b@@b@ /**@b@ * 20030801@b@ */@b@ public static String getStrMonthFirstDay() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 10);@b@ String nowYear = nowtime.substring(0, 4);@b@ String nowMonth = nowtime.substring(5, 7);@b@ String nowDay = "01";@b@ String nowdate = nowYear + nowMonth + nowDay;@b@ return nowdate;@b@@b@ }@b@@b@ /**@b@ * 20030801@b@ */@b@ public static String getMonthFirstDay() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ return nowtime.substring(0, 8) + "01";@b@@b@ }@b@@b@ /**@b@ * 取得上个月字符串 20007@b@ */@b@ public static String getStrPreviousMonth() {@b@ Calendar cal = Calendar.getInstance();@b@ cal.add(Calendar.MONTH, -1);@b@ int month = cal.get(Calendar.MONTH) + 1;@b@ int year = cal.get(Calendar.YEAR);@b@ String mon = String.valueOf(month);@b@ if (mon.length() < 2) {@b@ return year + "0" + mon;@b@ } else {@b@ return year + mon;@b@ }@b@ }@b@@b@ /**@b@ * 取得上个月 2008-07@b@ */@b@ public static String getPreviousMonth() {@b@ Calendar cal = Calendar.getInstance();@b@ cal.add(Calendar.MONTH, -1);@b@ int month = cal.get(Calendar.MONTH) + 1;@b@ int year = cal.get(Calendar.YEAR);@b@ String mon = String.valueOf(month);@b@ if (mon.length() < 2) {@b@ return year + "-0" + mon;@b@ } else {@b@ return year + "-" + mon;@b@ }@b@ }@b@@b@ /**@b@ * 取得上个月最后一天 20050430@b@ */@b@ public static String getStrPreviousMonthDate() {@b@ Calendar cal = Calendar.getInstance();// 当前日期@b@ cal.set(Calendar.DATE, 1);// 设为当前月的1号@b@ cal.add(Calendar.DATE, -1);// 减一天,变为上月最后一天@b@ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");@b@ return simpleDateFormat.format(cal.getTime());// 输出@b@ }@b@@b@ /**@b@ * 取得上个月最后一天 2005-04-30@b@ */@b@ public static String getPreviousMonthDate() {@b@ Calendar cal = Calendar.getInstance();// 当前日期@b@ cal.set(Calendar.DATE, 1);// 设为当前月的1号@b@ cal.add(Calendar.DATE, -1);// 减一天,变为上月最后一天@b@ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@ return simpleDateFormat.format(cal.getTime());// 输出20050430@b@ }@b@@b@ /**@b@ * 取得明天 2005-05-01@b@ */@b@ public static String getTomorrowDate() {@b@ Calendar cal = Calendar.getInstance();// 当前日期@b@ cal.add(Calendar.DATE, +1);// 加一天@b@ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");@b@ return simpleDateFormat.format(cal.getTime());// 输出2005-05-01@b@ }@b@@b@ /**@b@ * 20030801151515@b@ */@b@ public static String getStrNowDateHms() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ String nowYear = nowtime.substring(0, 4);@b@ String nowMonth = nowtime.substring(5, 7);@b@ String nowDay = nowtime.substring(8, 10);@b@ String nowHour = nowtime.substring(11, 13);@b@ String nowMinute = nowtime.substring(14, 16);@b@ String nowSecond = nowtime.substring(17, 19);@b@ String nowdate = nowYear + nowMonth + nowDay + nowHour + nowMinute@b@ + nowSecond;@b@ return nowdate;@b@ }@b@@b@ /**@b@ * 2009-03-31@b@ */@b@ public static String getNowDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 10);@b@ return nowtime;@b@ }@b@@b@ /**@b@ * 2009-03-31 15:48:28@b@ */@b@ public static String getNowDateHms() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ return nowtime;@b@ }@b@@b@ public static String dateToString(Date date, String pattern) {@b@ try {@b@ DateFormat format = new SimpleDateFormat(pattern);@b@ String str = format.format(date);@b@ return str;@b@ } catch (Exception e) {@b@ return null;@b@ }@b@ }@b@@b@ /**@b@ * 根据传入的日期得到上个月yyyy-mm的形式,例如传入2009-11-11,得到2009-10 @b@ */@b@ public static String getPreviousMonth(String strDate) {@b@ java.sql.Date date = java.sql.Date.valueOf(strDate);@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(date);@b@ calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);@b@ DateFormat format = new SimpleDateFormat("yyyy-MM");@b@ return format.format(calendar.getTime());@b@ }@b@@b@ public static String getPreviousMonthEndDay(String strDate) {@b@ java.sql.Date date = java.sql.Date.valueOf(strDate);@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(date);@b@ calendar.set(Calendar.DAY_OF_MONTH, 1);@b@ calendar.add(Calendar.DAY_OF_YEAR, -1);@b@ DateFormat format = new SimpleDateFormat("yyyyMMdd");@b@ return format.format(calendar.getTime());@b@ }@b@@b@ /**@b@ * 检查日期合法性@b@ * @b@ * @param s@b@ * String@b@ * @param dataFormat@b@ * String 日期样式,比如"yyyy-mm-dd","yyyymmddHHmmSS"@b@ * @return boolean@b@ */@b@ public static boolean checkDate(String s, String dataFormat) {@b@ boolean ret = true;@b@ try {@b@ DateFormat df = new SimpleDateFormat(dataFormat);@b@ ret = df.format(df.parse(s)).equals(s);@b@ } catch (ParseException e) {@b@ ret = false;@b@ }@b@ return ret;@b@ }@b@@b@ public static Date string2Date(String s) {@b@ List<DateFormat> formats = new ArrayList<DateFormat>();@b@ formats.add(new SimpleDateFormat("yyyy-MM-dd"));@b@ formats.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));@b@ formats.add(new SimpleDateFormat("yy-MM-dd"));@b@ formats.add(new SimpleDateFormat("yy-MM-dd HH:mm:ss"));@b@ formats.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));@b@@b@ formats.add(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));@b@ formats.add(new SimpleDateFormat("yyyy/MM/dd"));@b@ formats.add(new SimpleDateFormat("yy/MM/dd"));@b@ formats.add(new SimpleDateFormat("yy/MM/dd HH:mm:ss"));@b@@b@ formats.add(new SimpleDateFormat("yyyyMMdd HH:mm:ss"));@b@ formats.add(new SimpleDateFormat("yyyyMMddHHmmss"));@b@ formats.add(new SimpleDateFormat("yyyyMMddHHmm"));@b@ formats.add(new SimpleDateFormat("yyyyMMddHH"));@b@ formats.add(new SimpleDateFormat("yyyyMMdd"));@b@@b@ formats.add(new SimpleDateFormat("yyyy.MM.dd"));@b@ formats.add(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"));@b@@b@ formats.add(new SimpleDateFormat("EEE MMM d hh:mm:ss a z yyyy"));@b@ formats.add(new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"));@b@@b@ for (DateFormat format : formats) {@b@ try {@b@ Date d = format.parse(s);@b@ return d;@b@ } catch (ParseException e) {@b@ }@b@ }@b@ return null;@b@ }@b@@b@ /**@b@ * 获得当年1月1日的日期@b@ */@b@ public static String getFirstDayOfYear(String str) {@b@ SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@ Date date = null;@b@ String year = null;@b@ try {@b@ date = sd.parse(str);@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(date);@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy");@b@ year = sdf.format(calendar.getTime());@b@ return year + "-01-01 00:00:00";@b@ } catch (ParseException e) {@b@ e.printStackTrace();@b@ }@b@ return year;@b@ }@b@@b@ /**@b@ * 判断时间是否在凌晨19:00:00 ~~23:59:59@b@ */@b@ public static boolean is7To12() {@b@ Calendar cal = Calendar.getInstance();@b@ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");@b@ String c = sdf.format(cal.getTime());@b@ Integer time = Integer.parseInt(c.replace(":", ""));@b@ if (time >= 190000 && time <= 235959) {@b@ return true;@b@ }@b@ return false;@b@ }@b@@b@ /**@b@ * 判断时间是否在凌晨00:00:00 ~~05:00:00@b@ */@b@ public static boolean is0To5() {@b@ Calendar cal = Calendar.getInstance();@b@ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");@b@ String c = sdf.format(cal.getTime());@b@ Integer time = Integer.parseInt(c.replace(":", ""));@b@ if (time >= 1 && time <= 50000) {@b@ return true;@b@ }@b@ return false;@b@ }@b@@b@ /**@b@ * 根据传入日期param参数获取前一天日期@b@ * @b@ * @param param@b@ * : yyyy-MM-dd HH:mm:ss@b@ */@b@ public static String getYesterDay(String param) {@b@ SimpleDateFormat frt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@ try {@b@ Date date = frt.parse(param);@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTime(date);@b@ cal.add(Calendar.DATE, -1);@b@ return frt.format(cal.getTime());@b@ } catch (ParseException e) {@b@ e.printStackTrace();@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ return null;@b@ }@b@@b@ /**@b@ * 根据传入日期param参数获取35天前的日期@b@ * @b@ * @param param@b@ * : yyyy-MM-dd HH:mm:ss@b@ **/@b@ public static String getBefore35(String param) {@b@ SimpleDateFormat frt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@ try {@b@ Date date = frt.parse(param);@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTime(date);@b@ cal.add(Calendar.DATE, -34);@b@ return frt.format(cal.getTime());@b@ } catch (ParseException e) {@b@ e.printStackTrace();@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ return null;@b@ }@b@@b@ /**@b@ * 判断当前日期是否是一周的第一天@b@ */@b@ public static boolean isFirstDayOfWeek() {@b@ Calendar cal = Calendar.getInstance();@b@ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 这里设置从周一开始,若需要根据系统时区自动获取,则采用下边的方式@b@ Calendar cal2 = Calendar.getInstance();@b@ return cal.equals(cal2);@b@ }@b@@b@ /**@b@ * 判断当前日期是否是一月的第一天@b@ */@b@ public static boolean isFirstDayOfMonth() {@b@ Calendar cal = Calendar.getInstance();@b@ cal.set(Calendar.DAY_OF_MONTH, 1);// 这里设置从周一开始,若需要根据系统时区自动获取,则采用下边的方式@b@ Calendar cal2 = Calendar.getInstance();@b@ return cal.equals(cal2);@b@ }@b@@b@ /**@b@ * 当前日期的获得本月1号日期 calendar 当前日期@b@ */@b@ public static String getFirstDateOfCurMonth(Calendar calendar) {@b@ Calendar first = Calendar.getInstance();@b@ first.setTime(calendar.getTime());@b@@b@ int minDay = first.getMinimum(Calendar.DAY_OF_MONTH);@b@@b@ minDay = first.getMinimum(Calendar.DAY_OF_MONTH);@b@ first.set(Calendar.DAY_OF_MONTH, minDay);@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@ return sdf.format(first.getTime());@b@ }@b@@b@ /**@b@ * 获得上个月1号@b@ */@b@ public static Date getFirstDateOfLastMonth(Calendar calendar) {@b@ Calendar first = Calendar.getInstance();@b@ first.setTime(calendar.getTime());@b@@b@ int minDay = first.getMinimum(Calendar.DAY_OF_MONTH);@b@ first.add(Calendar.MONTH, -1);@b@@b@ minDay = first.getMinimum(Calendar.DAY_OF_MONTH);@b@ first.set(Calendar.DAY_OF_MONTH, minDay);@b@ return first.getTime();@b@ }@b@@b@ /**@b@ * 获取上个月最后一天的日期@b@ */@b@ public static Date getLastDateOfLastMonth(Calendar calendar) {@b@ Calendar last = Calendar.getInstance();@b@ last.setTime(calendar.getTime());@b@@b@ int maxDay = last.getMaximum(Calendar.DAY_OF_MONTH);@b@ last.add(Calendar.MONTH, -1);@b@@b@ maxDay = last.getActualMaximum(Calendar.DAY_OF_MONTH);@b@ last.set(Calendar.DAY_OF_MONTH, maxDay);@b@ return last.getTime();@b@ }@b@@b@ /**@b@ * 获取日期所在月的最后一天的日期@b@ * @b@ * @param calendar@b@ * @return@b@ */@b@ public static Date getLastDateOfMonth(Date date) {@b@ Calendar last = Calendar.getInstance();@b@ last.setTime(date);@b@@b@ int maxDay = last.getActualMaximum(Calendar.DAY_OF_MONTH);@b@ last.set(Calendar.DAY_OF_MONTH, maxDay);@b@ return last.getTime();@b@@b@ }@b@@b@ public static String getDateYMD(Date date) {@b@ Calendar c = Calendar.getInstance();@b@ c.setTime(date);@b@ String ymd = c.get(Calendar.YEAR) + "" + c.get(Calendar.MONTH) + ""@b@ + c.get(Calendar.DATE);@b@ return ymd;@b@ }@b@@b@ /**@b@ * 获得上周第一天的日期@b@ */@b@ public static Date getFirstDateOfLastWeek(Calendar calendar) {@b@ Calendar first = Calendar.getInstance();@b@ first.setTime(calendar.getTime());@b@ first.setFirstDayOfWeek(Calendar.MONDAY);@b@ int index = first.getFirstDayOfWeek();@b@ first.set(Calendar.DAY_OF_WEEK, index);@b@ first.add(Calendar.DAY_OF_WEEK, -7);@b@ return first.getTime();@b@ }@b@@b@ /**@b@ * 获得上周最后一天的日期@b@ */@b@ public static Date getLastDateOfLastWeek(Calendar calendar) {@b@ Calendar last = Calendar.getInstance();@b@ last.setTime(calendar.getTime());@b@ last.setFirstDayOfWeek(Calendar.MONDAY);@b@ int index = last.getFirstDayOfWeek();@b@ last.set(Calendar.DAY_OF_WEEK, index);@b@ last.add(Calendar.DAY_OF_WEEK, -1);@b@ return last.getTime();@b@ }@b@@b@ /**@b@ * 获得日期date所在的当前周数@b@ */@b@ public static String getConvertoWeekDate(String date, String inputFormat)@b@ throws ParseException {@b@ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@ Date myd = format.parse(date);@b@ SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);@b@ Calendar ccc = Calendar.getInstance();@b@ ccc.setTime(myd);@b@ ccc.setFirstDayOfWeek(Calendar.MONDAY);@b@@b@ /**/@b@ String str = date.substring(0, 4);@b@ SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");@b@ Date compara = tempFormat.parse(str + "-01-01");@b@ Calendar CalP = Calendar.getInstance();@b@ CalP.setTime(compara);@b@ int x = CalP.get(Calendar.DAY_OF_WEEK);@b@ /**/@b@ if (x != 2) {@b@ sdf.setCalendar(ccc);@b@ String s = sdf.format(ccc.getTime());@b@ String s1 = s.substring(0, 4);@b@ String s2 = s.substring(4, 6);@b@ Integer i = Integer.parseInt(s2);@b@ if (i > 1) {@b@ i = i - 1;@b@ }@b@ s2 = i + "";@b@ if ((i + "").length() < 2) {@b@ s2 = "0" + i;@b@ }@b@ return s1 + s2;@b@ } else {@b@ sdf.setCalendar(ccc);@b@ return sdf.format(ccc.getTime());@b@ }@b@ }@b@@b@ /**@b@ * 获得前一天开始的日期@b@ */@b@ public static String getYesterdayStart(Calendar calendar) {@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTime(calendar.getTime());@b@ cal.add(Calendar.DATE, -1);@b@ return sdf.format(cal.getTime()) + " 00:00:00";@b@ }@b@@b@ /**@b@ * 获得前一天结束的日期@b@ */@b@ public static String getYesterdayEnd(Calendar calendar) {@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTime(calendar.getTime());@b@ cal.add(Calendar.DATE, -1);@b@ return sdf.format(cal.getTime()) + " 23:59:59";@b@ }@b@@b@ /**@b@ * 获得前一天的日期@b@ */@b@ public static String getYesterday(Calendar calendar) {@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTime(calendar.getTime());@b@ cal.add(Calendar.DATE, -1);@b@ return sdf.format(cal.getTime());@b@ }@b@@b@ public static Date StringToDate(String date) throws ParseException {@b@ SimpleDateFormat sdf = new SimpleDateFormat();@b@ sdf.applyPattern("yyyy-MM-dd HH:mm:ss");@b@ return sdf.parse(date);@b@ }@b@@b@ public static Date StringToDate(String date, String pattern)@b@ throws ParseException {@b@ if (date == null || "".equals(date.trim())@b@ || "null".equals(date.trim())) {@b@ return null;@b@ }@b@ SimpleDateFormat sdf = new SimpleDateFormat();@b@ sdf.applyPattern(pattern);@b@ return sdf.parse(date);@b@ }@b@@b@ public static String getOracleDate() {@b@ String str = "to_date('" + DateUtils.dateToString()@b@ + "','YYYY-MM-DD HH24:MI:SS')";@b@ return str;@b@ }@b@@b@ /**@b@ * 业务日期判定月份 月份范围-上月的26号到本月的25号 例:201208 2012-07-26~2012-08-25@b@ * @b@ * @param operDate@b@ * yyyy-MM-dd@b@ * @param month_flag@b@ * yyyyMMdd@b@ * @return@b@ * @throws ParseException@b@ */@b@ public static boolean tskfContainDate(String operDate, String month_flag) {@b@ try {@b@ String endDateStr = month_flag + "25";@b@ Date tarDate = stringToDate(operDate, "yyyy-MM-dd");@b@ Date endDate = stringToDate(endDateStr, "yyyyMMdd");@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(endDate);@b@ calendar.add(Calendar.MONTH, -1);@b@ calendar.add(Calendar.DAY_OF_MONTH, 1);@b@ Date firstDate = calendar.getTime();@b@ if (tarDate.compareTo(firstDate) < 0@b@ || tarDate.compareTo(endDate) > 0) {@b@ return false;@b@ }@b@ } catch (ParseException e) {@b@ e.printStackTrace();@b@ }@b@@b@ return true;@b@ }@b@@b@ /**@b@ * 业务月数据处理周期判定 月份范围-本月的26号到下月的25号 例:201208 2012-08-26~2012-09-25@b@ * @b@ * @param operDate@b@ * yyyy-MM-dd@b@ * @param month_flag@b@ * yyyyMMdd@b@ * @return@b@ * @throws ParseException@b@ */@b@ public static boolean tskfMonthDate(String month_flag) {@b@ try {@b@ String firstDateStr = month_flag + "26";@b@ Date firstDate = stringToDate(firstDateStr, "yyyyMMdd");@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(firstDate);@b@ calendar.add(Calendar.MONTH, 1);@b@ Date endDate = calendar.getTime();@b@ Date tarDate = new Date();@b@ if (tarDate.compareTo(firstDate) < 0@b@ || tarDate.compareTo(endDate) >= 0) {@b@ return false;@b@ }@b@ } catch (ParseException e) {@b@ e.printStackTrace();@b@ }@b@@b@ return true;@b@ }@b@@b@ public static String getPreHourDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ String nowYear = nowtime.substring(0, 4);@b@ String nowMonth = nowtime.substring(5, 7);@b@ String nowDay = nowtime.substring(8, 10);@b@ String nowHour = String.format("%02d",@b@ Long.valueOf(nowtime.substring(11, 13)) - 1L);@b@ String nowMinute = nowtime.substring(14, 16);@b@ String nowSecond = nowtime.substring(17, 19);@b@@b@ String nowdate = nowYear + nowMonth + nowDay + nowHour + nowMinute@b@ + nowSecond;@b@ return nowdate;@b@ }@b@@b@ public static String getPreHalfHourDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ String nowYear = nowtime.substring(0, 4);@b@ String nowMonth = nowtime.substring(5, 7);@b@ String nowDay = nowtime.substring(8, 10);@b@ long nowHour = Long.valueOf(nowtime.substring(11, 13));@b@ long nowMinute = Long.parseLong(nowtime.substring(14, 16));@b@@b@ if (nowMinute > 30) {@b@ nowMinute = nowMinute - 30;@b@ } else {@b@ nowMinute = nowMinute + 30;@b@ nowHour = nowHour - 1;@b@ }@b@ String nowSecond = nowtime.substring(17, 19);@b@ String nowdate = nowYear + nowMonth + nowDay@b@ + String.format("%02d", nowHour)@b@ + String.format("%02d", nowMinute) + nowSecond;@b@ return nowdate;@b@ }@b@@b@ public static String getOneYearDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ Long nowYear = Long.valueOf(nowtime.substring(0, 4));@b@ Long nowMonth = Long.valueOf(nowtime.substring(5, 7));@b@ if (nowMonth >= 3)@b@ nowMonth = nowMonth - 2;@b@ else {@b@ nowYear = nowYear - 1;@b@ nowMonth = nowMonth + 10;@b@ }@b@ // 20141029101653@b@ return String.valueOf(nowYear) + String.format("%02d", nowMonth)@b@ + "00000000";@b@ }@b@@b@ public static String getPreYearDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ Long nowYear = Long.valueOf(nowtime.substring(0, 4)) - 1;@b@ String nowMonth = nowtime.substring(5, 7);@b@ String nowDay = nowtime.substring(8, 10);@b@ String nowHour = nowtime.substring(11, 13);@b@ String nowMinute = nowtime.substring(14, 16);@b@ String nowSecond = nowtime.substring(17, 19);@b@ return String.format("%04d", nowYear) + nowMonth + nowDay + nowHour@b@ + nowMinute + nowSecond;@b@ }@b@@b@ public static String getPreMonthDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ // Long nowYear = Long.valueOf(nowtime.substring(0, 4));@b@ String nowYear = nowtime.substring(0, 4);@b@ Long nowMonth = Long.valueOf(nowtime.substring(5, 7)) - 1;@b@ String nowDay = nowtime.substring(8, 10);@b@ String nowHour = nowtime.substring(11, 13);@b@ String nowMinute = nowtime.substring(14, 16);@b@ String nowSecond = nowtime.substring(17, 19);@b@ return nowYear + String.format("%02d", nowMonth) + nowDay + nowHour@b@ + nowMinute + nowSecond;@b@ }@b@@b@ public static String getHalfYearDate() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ Long nowYear = Long.valueOf(nowtime.substring(0, 4));@b@ Long nowMonth = Long.valueOf(nowtime.substring(5, 7));@b@ if (nowMonth >= 3)@b@ nowMonth = nowMonth - 2;@b@ else {@b@ nowYear = nowYear - 1;@b@ nowMonth = nowMonth + 10;@b@ }@b@ return String.format("%04d", nowYear) + String.format("%02d", nowMonth)@b@ + "00000000";@b@ }@b@@b@ public static String getPreHourDateForTest() {@b@ return "00000000000000";@b@ }@b@@b@ public static long transHMS2Sec(Date date) {@b@ String nowtime = new Timestamp(date.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ long nowHour = Long.valueOf(nowtime.substring(11, 13)) * 60 * 60;@b@ long nowMinute = Long.valueOf(nowtime.substring(14, 16)) * 60;@b@ long nowSecond = Long.valueOf(nowtime.substring(17, 19));@b@ long totalSec = nowHour + nowMinute + nowSecond;@b@ return totalSec;@b@ }@b@@b@ public static String transforYMDPattern(Date object) {@b@ if (null == object) {@b@ return null;@b@ }@b@ SimpleDateFormat sdf = new SimpleDateFormat(forPatternB);@b@ return sdf.format(object);@b@ }@b@@b@ public static String transforYMDPatternC(Date object) {@b@ if (null == object) {@b@ return null;@b@ }@b@ SimpleDateFormat sdf = new SimpleDateFormat(forPatternC);@b@ return sdf.format(object);@b@ }@b@@b@ public static String transforYMDHmPatternC(Date object) {@b@ if (null == object) {@b@ return null;@b@ }@b@ SimpleDateFormat sdf = new SimpleDateFormat(forPatternD);@b@ return sdf.format(object);@b@ }@b@@b@ public static Date transforDate2Date(Date object) throws ParseException {@b@ if (null == object) {@b@ return null;@b@ }@b@ SimpleDateFormat sdf = new SimpleDateFormat(forPatternB);@b@ return sdf.parse(sdf.format(object));@b@ }@b@@b@ public static Date transforYMDToDate(String date) throws ParseException {@b@ SimpleDateFormat sdf = new SimpleDateFormat(forPatternB);@b@ return sdf.parse(date);@b@ }@b@@b@ public static String getPreHourPattern() {@b@ SimpleDateFormat frt = new SimpleDateFormat(pattern);@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTime(new Date());@b@ cal.add(Calendar.HOUR, -1);@b@ return frt.format(cal.getTime());@b@ }@b@@b@ public static String String2String(String date) throws ParseException {@b@ String nowYear = date.substring(0, 4);@b@ String nowMonth = date.substring(5, 7);@b@ String nowDay = date.substring(8, 10);@b@ return nowYear + nowMonth + nowDay;@b@ }@b@@b@ public static Long Date2Day(String date) throws ParseException {@b@ Long nowYear = Long.valueOf(date.substring(0, 4)) * 365;@b@ Long nowMonth = Long.valueOf(date.substring(5, 7)) * 30;@b@ Long nowDay = Long.valueOf(date.substring(8, 10));@b@ return nowYear + nowMonth + nowDay;@b@ }@b@@b@ public static String getHour() {@b@ java.util.Date tdate = new java.util.Date();@b@ String nowtime = new Timestamp(tdate.getTime()).toString();@b@ nowtime = nowtime.substring(0, 19);@b@ String nowDay = nowtime.substring(8, 10);@b@ Long nowHour = Long.valueOf(nowtime.substring(11, 13));@b@ return String.format("%02d", nowHour);@b@ }@b@@b@ public static String getQbDealerDate(String date) throws ParseException {@b@ String nowYear = date.substring(0, 4);@b@ String nowMonth = date.substring(5, 7);@b@ String nowDay = date.substring(8, 10);@b@ return nowYear + nowMonth + nowDay + "000000";@b@ }@b@@b@ /**@b@ * 取得上个月 200807@b@ */@b@ public static String getDateVersion() {@b@ Calendar cal = Calendar.getInstance();@b@ int date = cal.get(Calendar.DATE);@b@ if (date < 26) {@b@ cal.add(Calendar.MONTH, -1);@b@ }@b@ int month = cal.get(Calendar.MONTH) + 1;@b@ int year = cal.get(Calendar.YEAR);@b@@b@ String mon = String.valueOf(month);@b@ if (mon.length() < 2) {@b@ return year + "0" + mon;@b@ } else {@b@ return year + "" + mon;@b@ }@b@ }@b@@b@ @b@}