java 日期转化工具类和智能转化总结
2018-01-05 09:55
411 查看
/** * Created by Administrator on 2017/5/11 0011. * 日期转化工具类 */ public class DateUtil { public static final String Y_M_D = "yyyy-MM-dd"; public static final String Y_M_D_HM = "yyyy_MM_dd HH:mm"; public static final String Y_M_D_HMS = "yyyy-MM-dd HH:mm:ss"; public static final String YMD = "yyyyMMdd"; public static final String YMDHM = "yyyyMMddHHmm"; public static final String YMDHMS = "yyyyMMddHHmmss"; public static final String ymd = "yyyy/MM/dd"; public static final String ymd_HM = "yyyy/MM/dd HH:mm"; public static final String ymd_HMS = "yyyy/MM/dd HH:mm:ss"; /** * 智能转换日期 * * @param date * @return */ public static String smartFormat(Date date) { String dateStr = null; if (date == null) { dateStr = ""; } else { try { dateStr = formatDate(date, Y_M_D_HMS); // 时分秒 if (dateStr.endsWith(" 00:00:00")) { dateStr = dateStr.substring(0, 10); } // 时分 else if (dateStr.endsWith("00:00")) { dateStr = dateStr.substring(0, 19); } // 秒 else if (dateStr.endsWith(":00")) { dateStr = dateStr.substring(0, 19); } } catch (Exception ex) { throw new IllegalArgumentException("转换日期失败: " + ex.getMessage(), ex); } } return dateStr; } /** * 智能转换日期 * * @param text * @return */ public static Date smartFormat(String text) { Date date = null; try { if (text == null || text.length() == 0) { date = null; } else if (text.length() == 10) { date = formatStringToDate(text, Y_M_D); } else if (text.length() == 13) { date = new Date(Long.parseLong(text)); } else if (text.length() == 16) { date = formatStringToDate(text, Y_M_D_HM); } else if (text.length() == 19) { date = formatStringToDate(text, Y_M_D_HMS); } else { throw new IllegalArgumentException("日期长度不符合要求!"); } } catch (Exception e) { throw new IllegalArgumentException("日期转换失败!"); } return date; } /** * 获取当前日期 * * @param format * @return * @throws Exception */ public static String getNow(String format) throws Exception { return formatDate(new Date(), format); } /** * 格式化日期格式 * * @param argDate * @param argFormat * @return 格式化后的日期字符串 */ public static String formatDate(Date argDate, String argFormat) throws Exception { if (argDate == null) { throw new Exception("参数[日期]不能为空!"); } if (StringUtils.isEmpty(argFormat)) { argFormat = Y_M_D; } SimpleDateFormat sdfFrom = new SimpleDateFormat(argFormat); return sdfFrom.format(argDate).toString(); } /** * 把字符串格式化日期 * @param argDateStr * @param argFormat * @return * @throws Exception */ public static Date formatStringToDate(String argDateStr,String argFormat) throws Exception{ if (argDateStr == null || argDateStr.trim().length()<1){ throw new Exception("日期不能为空!"); } String strFormat = null; if (StringUtils.isEmpty(argFormat)){ strFormat=Y_M_D; if (argDateStr.length()>16){ strFormat =Y_M_D_HMS; }else if (argDateStr.length()>10){ strFormat=Y_M_D_HM; } } SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat); //严格模式 simpleDateFormat.setLenient(false);//这个功能是不能把1996-13-1转换为1997-1-3 return simpleDateFormat.parse(argDateStr); } } /** * * 智能日期转换 * */ @SuppressWarnings("serial") public class SmartDateFormat extends SimpleDateFormat { //parse()返回的是一个Date类型数据,format返回的是一个StringBuffer类型的数据 //SimpleDateFormat中的parse方法可以 //把String型的字符串转换成特定格式的date类型 @Override public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) { return new StringBuffer(DateUtil.smartFormat(date)); } //SimpleDateFormat中的format方法可以 //把Date型的字符串转换成特定格式的String类型 @Override public Date parse(String text) throws ParseException { return DateUtil.smartFormat(text); } }
相关文章推荐
- java的office转化为pdf,pdf转化为swf的工具类 在线预览之总结
- Java中的日期转化格式DateUtil工具类
- java日期转化工具类
- JAVA 日期工具类的总结
- Java后台开发总结,以及前端javascript日期转化
- JAVA 日期工具类使用总结
- java 反射(reflect)总结,附对象打印工具类
- java 日期处理工具类
- 黑马程序员_Java第17天知识总结_集合框架的工具类_Collections_Arrays_将数组变成list集合_集合变数组_高级for循环_可变参数
- 一段简单的java 代码,用于日期格式转化
- java日期类封装及My97DatePicker日期插件使用总结
- BaseCode之日期工具类:DateUtil.java
- java日期工具类DateUtil-续一
- Java日期时间使用总结
- JAVA 多线程同步工具类总结
- Java—遍历集合的N种方式总结&Collections工具类
- JAVA日期工具类
- Java日期工具类一
- Java 日期简单工具类
- C#把datetime类型的日期转化成年月日或其他格式方法总结