| | |
| | | package com.java110.common.util; |
| | | |
| | | import java.sql.Timestamp; |
| | | import java.text.DateFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | |
| | | public static final String DATE_FORMATE_STRING_J = "yyyyMMddHHmmss.SSS"; |
| | | public static final String DATE_FORMATE_STRING_K = "yyyyMMddHHmmssSSS"; |
| | | |
| | | static |
| | | { |
| | | static { |
| | | formats.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss")); |
| | | formats.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); |
| | | formats.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd")); |
| | |
| | | |
| | | /** |
| | | * 返回 yyyyMMddhhmmss 格式的日期串 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getyyyyMMddhhmmssDateString(){ |
| | | public static String getyyyyMMddhhmmssDateString() { |
| | | return dateFormat.format(new Date()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前时间 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Date getCurrentDate() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月 |
| | | * |
| | | * @return |
| | | */ |
| | | public static int getCurrentMonth() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | return calendar.get(Calendar.MONTH) + 1; |
| | | } |
| | | |
| | | public static Date getLastDate() throws ParseException { |
| | | return getDateFromString("3000-01-01", DATE_FORMATE_STRING_B); |
| | | } |
| | | |
| | | public static String getFormatTimeString(Date date, String pattern) |
| | | { |
| | | /** |
| | | * 转TimeStamp |
| | | * |
| | | * @param date |
| | | * @return |
| | | */ |
| | | public static Timestamp getTimestamp(Date date) { |
| | | Timestamp timestamp = new Timestamp(date.getTime()); |
| | | return timestamp; |
| | | } |
| | | |
| | | /** |
| | | * 获取未来时间 |
| | | * |
| | | * @param second 秒 |
| | | * @return |
| | | */ |
| | | public static Date getFutureDate(int second) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.SECOND, second); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | public static String getFormatTimeString(Date date, String pattern) { |
| | | SimpleDateFormat sDateFormat = getDateFormat(pattern); |
| | | |
| | | synchronized (sDateFormat) { |
| | |
| | | } |
| | | } |
| | | |
| | | public static String getDefaultFormateTimeString(Date date) |
| | | { |
| | | public static String getDefaultFormateTimeString(Date date) { |
| | | return getFormatTimeString(date, "yyyyMMddHHmmss"); |
| | | } |
| | | |
| | | public static SimpleDateFormat getDateFormat(String pattern) |
| | | { |
| | | SimpleDateFormat sDateFormat = (SimpleDateFormat)formats.get(pattern); |
| | | public static SimpleDateFormat getDateFormat(String pattern) { |
| | | SimpleDateFormat sDateFormat = (SimpleDateFormat) formats.get(pattern); |
| | | if (sDateFormat == null) { |
| | | sDateFormat = new SimpleDateFormat(pattern); |
| | | formats.put(pattern, sDateFormat); |
| | |
| | | } |
| | | |
| | | public static Date getDateFromString(String date, String pattern) |
| | | throws ParseException |
| | | { |
| | | throws ParseException { |
| | | SimpleDateFormat sDateFormat = getDateFormat(pattern); |
| | | |
| | | synchronized (sDateFormat) { |
| | |
| | | } |
| | | |
| | | public static Date getDefaultDateFromString(String date) |
| | | throws ParseException |
| | | { |
| | | throws ParseException { |
| | | return getDateFromString(date, "yyyyMMddHHmmss"); |
| | | } |
| | | |
| | | public static String getNowDefault() |
| | | { |
| | | public static String getNowDefault() { |
| | | return getNow("yyyyMMddHHmmss"); |
| | | } |
| | | |
| | | public static String getNow(String pattern) |
| | | { |
| | | public static String getNow(String pattern) { |
| | | return getFormatTimeString(new Date(), pattern); |
| | | } |
| | | |
| | | public static String getNowII() |
| | | { |
| | | public static String getNowII() { |
| | | return getFormatTimeString(new Date(), "yyyyMMdd"); |
| | | } |
| | | |
| | | public static long dateString2Long(String str, String pattern) |
| | | throws ParseException |
| | | { |
| | | throws ParseException { |
| | | return getDateFromString(str, pattern).getTime(); |
| | | } |
| | | |
| | | public static String longToDateStringDefault(long time) |
| | | { |
| | | /** |
| | | * 校验字符串是否可以格式化为时间 |
| | | * |
| | | * @param str |
| | | * @param pattern |
| | | * @return |
| | | */ |
| | | public static boolean judgeDate(String str, String pattern) { |
| | | try { |
| | | dateString2Long(str, pattern); |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public static String longToDateStringDefault(long time) { |
| | | return getFormatTimeString(new Date(time), "yyyyMMddHHmmss"); |
| | | } |
| | | |
| | | public static String longToDateString(long time, String pattern) |
| | | { |
| | | public static String longToDateString(long time, String pattern) { |
| | | return getFormatTimeString(new Date(time), pattern); |
| | | } |
| | | |
| | | public static long date2Long(Date date) |
| | | { |
| | | public static long date2Long(Date date) { |
| | | return date.getTime(); |
| | | } |
| | | |
| | | public static Date longToDate(long time) |
| | | { |
| | | public static Date longToDate(long time) { |
| | | return new Date(time); |
| | | } |
| | | |
| | | public static Date getDateFromStringAdaptTwoPattern(String date) |
| | | throws ParseException |
| | | { |
| | | try |
| | | { |
| | | return getDateFromString(date, "yyyy-MM-dd HH:mm:ss"); } catch (ParseException e) { |
| | | throws ParseException { |
| | | try { |
| | | return getDateFromString(date, "yyyy-MM-dd HH:mm:ss"); |
| | | } catch (ParseException e) { |
| | | } |
| | | return getDateFromString(date, "yyyy-MM-dd"); |
| | | } |
| | | |
| | | public static String changeNumDateToDate(String numdate, String inFormat, String outFormat) |
| | | throws ParseException |
| | | { |
| | | throws ParseException { |
| | | Date date = getDateFromString(numdate, inFormat); |
| | | return getFormatTimeString(date, outFormat); |
| | | } |
| | | |
| | | public static String getNextMonthFistDay(String nowdate, String inFormat, String outFormat) |
| | | throws ParseException |
| | | { |
| | | throws ParseException { |
| | | Date date = getDateFromString(nowdate, inFormat); |
| | | |
| | | Calendar cl = Calendar.getInstance(); |
| | |
| | | return getFormatTimeString(date, outFormat); |
| | | } |
| | | |
| | | public static boolean isLeapYear(int year) |
| | | { |
| | | public static boolean isLeapYear(int year) { |
| | | if (year % 400 == 0) |
| | | return true; |
| | | if (year % 4 == 0) |
| | | { |
| | | if (year % 4 == 0) { |
| | | return (year % 100 != 0); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | public static String getLastDay(String nowdate, String inFormat, String outFormat) |
| | | throws ParseException |
| | | { |
| | | throws ParseException { |
| | | String returndate = ""; |
| | | |
| | | Date date = getDateFromString(nowdate, inFormat); |
| | | |
| | | Calendar cl = Calendar.getInstance(); |
| | | cl.setTime(date); |
| | | switch (cl.get(2)) |
| | | { |
| | | switch (cl.get(2)) { |
| | | case 0: |
| | | cl.set(5, 31); |
| | | break; |
| | |
| | | return returndate; |
| | | } |
| | | |
| | | public static String getMonthLastDay(String fmt) |
| | | { |
| | | public static String getMonthLastDay(String fmt) { |
| | | String returndate = ""; |
| | | Date date = null; |
| | | Calendar cl = Calendar.getInstance(); |
| | | switch (cl.get(2)) |
| | | { |
| | | switch (cl.get(2)) { |
| | | case 0: |
| | | cl.set(5, 31); |
| | | break; |
| | |
| | | return returndate; |
| | | } |
| | | |
| | | public static String getNextMonthFirstDay(String fmt) |
| | | { |
| | | public static String getNextMonthFirstDay(String fmt) { |
| | | String returndate = ""; |
| | | Date date = null; |
| | | |
| | |
| | | } |
| | | |
| | | public static boolean compareDate(String fistDate, String secondDate, String format) |
| | | throws ParseException |
| | | { |
| | | throws ParseException { |
| | | boolean flag = false; |
| | | |
| | | Date fist = null; |
| | |
| | | return flag; |
| | | } |
| | | |
| | | public static boolean isRightDate(String value, String varValue) |
| | | { |
| | | try |
| | | { |
| | | public static boolean isRightDate(String value, String varValue) { |
| | | try { |
| | | SimpleDateFormat format = new SimpleDateFormat(varValue); |
| | | format.setLenient(false); |
| | | format.parse(value); |