package com.cku.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.LinkedHashMap; import java.util.Map; import java.util.Random; import com.cku.core.ZAErrorCode; import com.cku.core.ZAException; public class DateUtils { private static String previousDateTime; public synchronized static String getUniqueDateTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String dateTime = sdf.format(new Date()); while (dateTime.equals(previousDateTime)) { dateTime = sdf.format(new Date()); } previousDateTime = dateTime; return dateTime; } /** * ����ǰϵͳʱ��ת����ֱ��Millisecond����ʽ */ public static String getDateTimeZone() { return new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date()); } /** * ����ǰϵͳʱ��ת����ֱ��HHmmssS����ʽ * * @date 2008-4-3 ����09:48:56 * @return */ public static String getTimeZone() { return new SimpleDateFormat("HHmmssS").format(new Date()); } /** * ���ʱ���ʽȡ��ǰʱ�� * * @param format * @return */ public static String getCurrentDate(String format) { return new SimpleDateFormat(format).format(new Date()); } /** * �õ���ǰϵͳʱ������� * * @return */ public static String getDateTime() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } public static String getDateTime(Date date) { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } public static Timestamp getCurrentTimestamp() { return new Timestamp(System.currentTimeMillis()); } /** * ��õ�ǰϵͳ���� * * @return */ public static String getDate() { return new SimpleDateFormat("yyyy-MM-dd").format(new Date()); } /** * 获取系统前一天的时间 * @return yestedayDate */ public static String yestedayDate() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); String yestedayDate= new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); return yestedayDate; } /** * ��õ�ǰϵͳʱ�� * * @return */ public static String getTime() { return new SimpleDateFormat("HH:mm:ss").format(new Date()); } public static boolean isDateAfter(String timeString, long rating) { try { Date date = addDay(timeString, rating); Date now = new SimpleDateFormat("yyyy-MM-dd HH:mm") .parse(new SimpleDateFormat("yyyy-MM-dd HH:mm") .format(new Date())); long min = date.getTime(); long nowmin = now.getTime(); if (nowmin - min > 10 * 60 * 1000) return true; else return false; } catch (ParseException e) { return false; } } /** * �жϴ���������Ƿ���ڵ�ǰϵͳʱ����ʱ�� * * @param timeString * @param rating * @return */ public static boolean isAfter(String timeString, int rating) { try { Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm") .parse(timeString); Date now = new SimpleDateFormat("yyyy-MM-dd HH:mm") .parse(new SimpleDateFormat("yyyy-MM-dd HH:mm") .format(new Date())); long min = date.getTime(); long nowmin = now.getTime(); long count = rating * 60 * 1000; if (nowmin - min > count) return true; else return false; } catch (Exception e) { return false; } } /** * �жϴ���������Ƿ���ڵ�ǰϵͳʱ����ʱ�� * * @param timeString * @param rating * @return */ public static boolean isBefore(String timeString, int rating) { try { Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm") .parse(timeString); Date now = new SimpleDateFormat("yyyy-MM-dd HH:mm") .parse(new SimpleDateFormat("yyyy-MM-dd HH:mm") .format(new Date())); long min = date.getTime(); long nowmin = now.getTime(); if (nowmin - min < rating * 60 * 1000) return true; else return false; } catch (Exception e) { return false; } } /** * ���ָ�������ڸ�ʽ��ʾ���� * * @param dDate * @param sFormat * @return */ public static String formatDate(Date dDate, String sFormat) { SimpleDateFormat formatter = new SimpleDateFormat(sFormat); String dateString = formatter.format(dDate); return dateString; } public static String dateToString(Date dDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(dDate); return dateString; } public static String dateTimeToString(Date dDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(dDate); return dateString; } public static Date toDate(Date dDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(dDate); return strToDate(dateString, "yyyy-MM-dd"); } public static Date toDatetime(Date dDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(dDate); return strToDate(dateString, "yyyy-MM-dd HH:mm:ss"); } /** * ���ַ�ת��Ϊָ�������ڸ�ʽ��� * * @param s * @param pattern * @return */ public static Date strToDate(String s, String pattern) { SimpleDateFormat formatter = new SimpleDateFormat(pattern); Date date1; try { Date theDate = formatter.parse(s); Date date = theDate; return date; } catch (Exception ex) { date1 = null; } return date1; } /** * ���ַ�ת��Ϊ���� *@param s *@return */ public static Date strToDateFormat(String s) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1; try { Date theDate = formatter.parse(s); Date date = theDate; return date; } catch (Exception ex) { date1 = null; } return date1; } public static Date strToDate(String s) { return strToDate(s, "yyyy-MM-dd HH:mm:ss"); } /** * ���Сʱ�ͷ� * * @param sDate * @param iNbDay * @return */ public static Date addMinute(String sDate, long iNbTime) { Calendar cal = Calendar.getInstance(); cal.setTime(strToDate(sDate, "yyyy-MM-dd HH:mm")); cal.add(Calendar.MINUTE, (int) iNbTime); Date date = cal.getTime(); return date; } public static Date addHour(String sDate, long iNbTime) { Calendar cal = Calendar.getInstance(); cal.setTime(strToDate(sDate, "yyyy-MM-dd HH:mm")); cal.add(Calendar.HOUR_OF_DAY, (int) iNbTime); Date date = cal.getTime(); return date; } /** * ��ָ��������� * * @param dDate * @param iNbDay * @return */ public static Date addDay(String sDate, long iNbDay) { Calendar cal = Calendar.getInstance(); cal.setTime(strToDate(sDate, "yyyy-MM-dd HH:mm")); cal.add(Calendar.DAY_OF_MONTH, (int) iNbDay); Date result = cal.getTime(); return result; } /** * ��ǰʱ��������� *@param sDate *@param iNbDay *@return */ public static Date addDay(long iNbDay) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.DAY_OF_MONTH, (int) iNbDay); Date result = cal.getTime(); return result; } /** * ��ǰʱ����� �� * * @param dDate * @param iNbWeek * @return */ public static Date addWeek(String sDate, long iNbWeek) { Calendar cal = Calendar.getInstance(); cal.setTime(strToDate(sDate, "yyyy-MM-dd HH:mm")); cal.add(Calendar.WEEK_OF_YEAR, (int) iNbWeek); Date result = cal.getTime(); return result; } /** * ��ǰʱ����� �� * * @param dDate * @param iNbMonth * @return */ public static Date addMonth(String sDate, int iNbMonth) { Calendar cal = Calendar.getInstance(); cal.setTime(strToDate(sDate, "yyyy-MM-dd HH:mm")); int month = cal.get(Calendar.MONTH); month += iNbMonth; int year = month / 12; month %= 12; cal.set(Calendar.MONTH, month); if (year != 0) { int oldYear = cal.get(Calendar.YEAR); cal.set(Calendar.YEAR, year + oldYear); } return cal.getTime(); } /** * ��ǰʱ����� �� * * @param dDate * @param iNbYear * @return */ public static Date addYear(Date dDate, int iNbYear) { Calendar cal = Calendar.getInstance(); cal.setTime(dDate); int oldYear = cal.get(1); cal.set(1, iNbYear + oldYear); return cal.getTime(); } /** * �õ���ǰ���������ڼ� * * @param dDate * @return */ public static int getWeek(Date dDate) { Calendar cal = Calendar.getInstance(); cal.setTime(dDate); return cal.get(Calendar.DAY_OF_WEEK) - 1; } /** * �õ���ǰ�������ڼ� * * @return */ public static String getWeeks() { final String dayNames[] = { "������", "����һ", "���ڶ�", "������", "������", "������","������" }; SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // SimpleDateFormat ��һ���������Ի�����صķ�ʽ����ʽ���ͷ������ڵľ����ࡣ��������и�ʽ�������� -> �ı������������ı� // -> ���ڣ��͹淶���� Calendar calendar = Calendar.getInstance(); Date date = new Date(); try { date = sdfInput.parse(sdfInput.format(date)); } catch (ParseException ex) { } calendar.setTime(date); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);// get �� set // ���ֶ����֣�ָʾһ�������е�ij�졣 System.out.println("dayOfWeek:"+dayOfWeek); return dayNames[dayOfWeek - 1]; } /** * ���һ�����ڣ����������ڼ����ַ� * * @param sdate * @return */ public static String getWeek(String sdate) { // ��ת��Ϊʱ�� Date date = VeDate.strToDate(sdate); Calendar c = Calendar.getInstance(); c.setTime(date); // int hour=c.get(Calendar.DAY_OF_WEEK); // hour�д�ľ������ڼ��ˣ��䷶Χ 1~7 // 1=������ 7=�������������� return new SimpleDateFormat("EEEE").format(c.getTime()); } /** * �õ���ǰ������ �Ǹ��µļ��� * * @param dDate * @return */ public static int getMonthOfDay(Date dDate) { Calendar cal = Calendar.getInstance(); cal.setTime(dDate); return cal.get(Calendar.DAY_OF_MONTH); } public static void main(String[] gta) { } /** * ����漴�� * * @param pwd_len * @return */ public static String genRandomNum(int pwd_len) { // 35����Ϊ�����Ǵ�0��ʼ�ģ�26����ĸ+10������ final int maxNum = 36; int i; // ��ɵ������ int count = 0; // ��ɵ�����ij��� char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; StringBuffer pwd = new StringBuffer(""); Random r = new Random(); while (count < pwd_len) { // ��������ȡ���ֵ����ֹ��ɸ��� i = Math.abs(r.nextInt(maxNum)); // ��ɵ������Ϊ36-1 if (i >= 0 && i < str.length) { pwd.append(str[i]); count++; } } return pwd.toString(); } public static String genRandNum(int pwd_len) { // 35����Ϊ�����Ǵ�0��ʼ�ģ�26����ĸ+10������ final int maxNum = 36; int i; // ��ɵ������ int count = 0; // ��ɵ�����ij��� char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; StringBuffer pwd = new StringBuffer(""); Random r = new Random(); while (count < pwd_len) { // ��������ȡ���ֵ����ֹ��ɸ��� i = Math.abs(r.nextInt(maxNum)); // ��ɵ������Ϊ36-1 if (i >= 0 && i < str.length) { pwd.append(str[i]); count++; } } return pwd.toString(); } public static boolean isCurrentWeek(Timestamp ts){ long c=getCurrentTimestamp().getTime(); long t=ts.getTime(); long l=1000; long d=l*60*60*24*7; if((c-t)/d<1) return true; else return false; } public static Map getYearMap() { Map yearMap = new LinkedHashMap(); for(int i = 2010; i <= 2020 ; i ++) { yearMap.put(i, i); } return yearMap; } @SuppressWarnings("deprecation") public static Date getDate(Integer year, Integer month, Integer day) { Date date = new Date(year,month,day); return date; } public static Timestamp getBefore(Integer days){ Calendar calendar = Calendar.getInstance(); //java.util包 calendar.add(Calendar.DATE, days); //日期减 如果不够减会将月变动 String date= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(calendar.getTime()); Timestamp ts = Timestamp.valueOf(date); return ts; } public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /** * 获取日期差值,获取两个日期的相差月份 * * @param date1 * @param date2 * @param x * @return * @throws Exception */ public static boolean isInXMounth(Date date1, Date date2, int x) throws ZAException { if (date1 == null || date2 == null) { throw new ZAException(ZAErrorCode.ZA_ERC_UNKNOWN, "日期为空"); } if (x < 1) { throw new ZAException(ZAErrorCode.ZA_ERC_UNKNOWN, "月份数必须为大于1的正整数"); } try { date1 = sdf.parse(sdf.format(date1)); date2 = sdf.parse(sdf.format(date2)); } catch (ParseException e) { throw new ZAException(ZAErrorCode.ZA_ERC_UNKNOWN, "日期有误"); } Calendar startDateCalendar = Calendar.getInstance(); Calendar endDateCalendar = Calendar.getInstance(); if (date1.before(date2)) { startDateCalendar.setTime(date1); endDateCalendar.setTime(date2); } else { startDateCalendar.setTime(date2); endDateCalendar.setTime(date1); } int year = endDateCalendar.get(Calendar.YEAR) - startDateCalendar.get(Calendar.YEAR); int month = endDateCalendar.get(Calendar.MONTH) - startDateCalendar.get(Calendar.MONTH); if (year * 12 + month < x) { return true; } else if (year * 12 + month == x) { return endDateCalendar.get(Calendar.DAY_OF_MONTH) < startDateCalendar.get(Calendar.DAY_OF_MONTH); } else { return false; } } }