- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.lang.StringUtils;
- public class DateUtils {
- private static final Map<String, String> dtfmt = new HashMap<String, String>();
- private static final Map<String, DateFormat> fmt = new HashMap<String, DateFormat>();
- static {
- dtfmt.put("\\d{8}", "yyyyMMdd");
- dtfmt.put("\\d{12}", "yyyyMMddHHmm");
- dtfmt.put("\\d{14}", "yyyyMMddHHmmss");
- dtfmt.put("\\d{4}-\\d{2}-\\d{2}", "yyyy-MM-dd");
- dtfmt.put("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}", "yyyy-MM-dd HH:mm");
- dtfmt.put("\\d{4}-\\d{2}-\\d{2} \\d{2}\\d{2}", "yyyy-MM-dd HHmm");
- dtfmt.put("\\d{2}:\\d{2}:\\d{2}", "HH:mm:ss");
- dtfmt.put("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", "yyyy-MM-dd HH:mm:ss");
- dtfmt.put("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\..*", "yyyy-MM-dd HH:mm:ss.SSS");
- dtfmt.put("\\d{4}/\\d{2}/\\d{2}", "yyyy/MM/dd");
- dtfmt.put("\\d{4}年\\d{2}月\\d{2}日", "yyyy年MM月dd日");
- dtfmt.put("\\d{4}年\\d{2}月\\d{2}日 \\d{2}时\\d{2}分\\d{2}秒", "yyyy年MM月dd日 HH时mm分ss秒");
- fmt.put("yyyyMMdd", new SimpleDateFormat("yyyyMMdd"));
- fmt.put("yyyyMMddHHmm", new SimpleDateFormat("yyyyMMddHHmm"));
- fmt.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
- fmt.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
- fmt.put("yyyy-MM-dd HH:mm", new SimpleDateFormat("yyyy-MM-dd HH:mm"));
- fmt.put("yyyy-MM-dd HHmm", new SimpleDateFormat("yyyy-MM-dd HHmm"));
- fmt.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
- fmt.put("HH:mm:ss", new SimpleDateFormat("HH:mm:ss"));
- fmt.put("yyyy-MM-dd HH:mm:ss.SSS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
- fmt.put("yyyy年MM月dd日", new SimpleDateFormat("yyyy年MM月dd日"));
- fmt.put("yyyy年MM月dd日 HH时mm分ss秒", new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"));
- fmt.put("yyyy/MM/dd", new SimpleDateFormat("yyyy/MM/dd"));
- }
- public static String getCurrentDate(String yyyyMMdd) {
- SimpleDateFormat formatDate = new SimpleDateFormat(yyyyMMdd);
- Calendar cal = Calendar.getInstance();
- return formatDate.format(cal.getTime());
- }
- public static String getCurrentYear() {
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- String str = formatDate.format(cal.getTime());
- String[] strArr = str.split("-");
- return strArr[0];
- }
- public static String getParseDate(Date date) {
- String str = "";
- if (date != null) {
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
- str = formatDate.format(date);
- }
- return str;
- }
- public static Date parseDate(String date) {
- Date dat = null;
- if (date != null && date.trim().length() > 0) {
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
- try {
- dat = formatDate.parse(date);
- } catch (ParseException e) {
- dat = null;
- }
- } else
- dat = new Date();
- return dat;
- }
- /**
- *
- * @param year
- * int 年份
- * @param month
- * int 月份
- *
- * @return int 某年某月的第一天
- */
-
- @SuppressWarnings("unused")
- public static int getFirstDayOfMonth(int year, int month) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, month);
- return cal.getActualMinimum(Calendar.DATE);
- }
-
- /**
- *
- * @param year
- * int 年份
- * @param month
- * int 月份
- *
- * @return int 某年某月的最后一天
- */
- @SuppressWarnings("unused")
- public static int getLastDayOfMonth(int year, int month) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, month);
- return cal.getActualMaximum(Calendar.DATE);
- }
- /**
- * 转换秒为 x天x小时x分x秒
- *
- * @param seconds
- * @return
- */
- public static String getDayHourMinSec(long seconds) {
- long day = seconds / (24 * 60 * 60);
- long hour = (seconds - day * 24 * 60 * 60) / (60 * 60);
- long min = (seconds - day * 24 * 60 * 60 - hour * 60 * 60) / 60;
- long sec = seconds - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
- StringBuffer result = new StringBuffer();
- if (day > 0) {
- result.append(day + "天");
- }
- if (hour > 0) {
- result.append(hour + "小时");
- }
- if (min > 0) {
- result.append(min + "分");
- }
- if (sec > 0) {
- result.append(sec + "秒");
- }
- if (result.length() == 0) {
- result.append("0秒");
- }
- return result.toString();
- }
-
- /**
- * 格式化日期为指定格式字符串
- *
- * @param date
- * @param pattern
- * @return
- */
- public static String format(final java.util.Date date, final String pattern) {
- if (StringUtils.isBlank(pattern) || !fmt.containsKey(pattern)) {
- throw new IllegalArgumentException("不支持格式化Pattern");
- }
- return fmt.get(pattern).format(date);
- }
-
-
- /**
- *
- * @param date
- * @param time
- * @return
- */
- public static String getNormalDateTime(String date, String time) {
- return StringUtils.leftPad(date.replaceAll("-|/|年|月|日", ""), 8, "20") + StringUtils.rightPad(StringUtils.leftPad(time.replaceAll(":|时|分|秒", ""), 4, "0"), 6, "0");
- }
-
-
- /**
- * 根据日期字符串生成java.util.Date
- * @param dateStr
- * @return
- */
- public static java.util.Date toDate(String dateStr) {
- if (StringUtils.isBlank(dateStr)) {
- return null;
- }
- String pattern = getDatePattern(dateStr);
- if (StringUtils.isBlank(pattern)) {
- throw new IllegalArgumentException("无法识别日期字符串格式[" + dateStr + "]");
- }
- return toDate(dateStr, pattern);
- }
-
- /**
- * 根据日期字符串及格式生成java.util.Date
- *
- * @param dateStr
- * @param pattern
- * @return
- */
- public static java.util.Date toDate(String dateStr, String pattern) {
- if (StringUtils.isBlank(dateStr)) {
- return null;
- }
- try {
- return org.apache.commons.lang.time.DateUtils.parseDate(dateStr, new String[] { pattern });
- } catch (ParseException e) {
- throw new IllegalArgumentException(String.format("转换日期字符串不正确,字符串:%s,格式:%s", dateStr, pattern));
- }
- }
-
- /**
- * 根据日期字符串取得日期格式
- *
- * @param dateStr
- * @return
- */
- public static String getDatePattern(String dateStr) {
- if (StringUtils.isBlank(dateStr)) {
- return null;
- }
- String pattern = null;
- for (String key : dtfmt.keySet()) {
- if (dateStr.matches(key)) {
- return dtfmt.get(key);
- }
- }
- return pattern;
- }
- }
复制代码 |