java 各种日期格式化工具
2017-08-16 09:49
447 查看
package org.hsweb.commons.time; import org.joda.time.DateTime; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Pattern; /** * 日期格式化工具 * * @author zhouhao */ public interface DateFormatter { List<DateFormatter> supportFormatter = new ArrayList<>(Arrays.asList( /* *常见格式 * */ // yyyyMMdd new DefaultDateFormatter(Pattern.compile("[0-9]{4}[0-9]{2}[0-9]{2}"), "yyyyMMdd") // yyyy-MM-dd , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}"), "yyyy-MM-dd") // yyyy/MM/dd , new DefaultDateFormatter(Pattern.compile("[0-9]{4}/[0-9]{2}/[0-9]{2}"), "yyyy/MM/dd") //yyyy年MM月dd日 , new DefaultDateFormatter(Pattern.compile("[0-9]{4}年[0-9]{2}月[0-9]{2}日"), "yyyy年MM月dd日") //yyyy年M月d日 , new DefaultDateFormatter(Pattern.compile("[0-9]{4}年[0-9]月[0-9]日"), "yyyy年M月d日") //yyyy年MM月d日 , new DefaultDateFormatter(Pattern.compile("[0-9]{4}年[0-9]{2}月[0-9]日"), "yyyy年MM月d日") //yyyy年M月dd日 , new DefaultDateFormatter(Pattern.compile("[0-9]{4}年[0-9]月[0-9]{2}日"), "yyyy年M月dd日") // yyyy-M-d , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]-[0-9]"), "yyyy-M-d") // yyyy-MM-d , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]"), "yyyy-MM-d") // yyyy-M-dd , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]-[0-9]{2}"), "yyyy-M-dd") // yyyy/M/d , new DefaultDateFormatter(Pattern.compile("[0-9]{4}/[0-9]/[0-9]"), "yyyy/M/d") // yyyy/MM/d , new DefaultDateFormatter(Pattern.compile("[0-9]{4}/[0-9]{2}/[0-9]"), "yyyy/MM/d") // yyyy/M/dd , new DefaultDateFormatter(Pattern.compile("[0-9]{4}/[0-9]/[0-9]{2}"), "yyyy/M/dd") // yyyy-MM-dd HH:mm:ss , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}"), "yyyy-MM-dd HH:mm:ss") // yyyy/MM/dd HH:mm:ss , new DefaultDateFormatter(Pattern.compile("[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}"), "yyyy/MM/dd HH:mm:ss") // yyyy-MM-dd HH:mm:ssZ , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\+[0-9]{4}"), "yyyy-MM-dd HH:mm:ssZ") //yyyy-MM-dd'T'HH:mm:ss , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"), "yyyy-MM-dd'T'HH:mm:ss") //yyyy-MM-dd'T'HH:mm:ssZ , new DefaultDateFormatter(Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\+[0-9]{4}"), "yyyy-MM-dd'T'HH:mm:ssZ") //yyyy年MM月dd日HH时mm分ss秒 , new DefaultDateFormatter(Pattern.compile("[0-9]{4}年[0-9]{2}月[0-9]{2}日[0-9]{2}时[0-9]{2}分[0-9]{2}秒"), "yyyy年MM月dd日HH时mm分ss秒") //yyyy年MM月dd日 HH时mm分ss秒 , new DefaultDateFormatter(Pattern.compile("[0-9]{4}年[0-9]{2}月[0-9]{2}日 [0-9]{2}时[0-9]{2}分[0-9]{2}秒"), "yyyy年MM月dd日 HH时mm分ss秒") // HH:mm:ss , new DefaultDateFormatter(Pattern.compile("[0-9]{2}:[0-9]{2}:[0-9]{2}"), "HH:mm:ss") /* * 奇奇怪怪的格式 * */ // yyyyMMdd HH:mm:dd , new DefaultDateFormatter(Pattern.compile("[0-9]{4}[0-9]{2}[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}"), "yyyyMMdd HH:mm:ss") // yyyyMMddHHmmdd , new DefaultDateFormatter(Pattern.compile("[0-9]{4}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}"), "yyyyMMddHHmmdd") // yyyyMMdd HHmmdd , new DefaultDateFormatter(Pattern.compile("[0-9]{4}[0-9]{2}[0-9]{2} [0-9]{2}[0-9]{2}[0-9]{2}"), "yyyyMMdd HHmmdd") //yyyy年 MM月 dd日 EEE HH:mm:ss 'CST' , new SampleJDKDateFormatter(str -> str.contains("年") && str.contains("CST") && str.split("[ ]").length == 6, () -> new SimpleDateFormat("yyyy年 MM月 dd日 EEE HH:mm:ss 'CST'", Locale.CHINESE)) //yyyy年 MM月 dd日 EEE HH:mm:ss 'GMT' , new SampleJDKDateFormatter(str -> str.contains("年") && str.contains("GMT") && str.split("[ ]").length == 6, () -> new SimpleDateFormat("yyyy年 MM月 dd日 EEE HH:mm:ss 'GMT'", Locale.CHINESE)) //EEE MMM ddHH:mm:ss 'CST' yyyy , new SampleJDKDateFormatter(str -> str.contains("CST") && str.split("[ ]").length == 6, () -> new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CST' yyyy", Locale.US)) //EEE MMM ddHH:mm:ss 'GMT' yyyy , new SampleJDKDateFormatter(str -> str.contains("GMT") && str.split("[ ]").length == 6, () -> new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US)) //MMM d, yyyy K:m:s a , new SampleJDKDateFormatter(str -> str.endsWith("AM") || str.endsWith("PM") && str.split("[ ]").length == 5, () -> new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH)) )); boolean support(String str); Date format(String str); String toString(Date date); String getPattern(); static Date fromString(String dateString) { DateFormatter formatter = getFormatter(dateString); if (formatter != null) return formatter.format(dateString); return null; } static Date fromString(String dateString, String pattern) { try { return new SimpleDateFormat(pattern).parse(dateString); } catch (ParseException e) { e.printStackTrace(); return null; } } static String toString(Date date, String format) { if (null == date) return null; for (DateFormatter formatter : supportFormatter) { if (formatter.getPattern().equals(format)) return formatter.toString(date); } return new DateTime(date).toString(format); } static boolean isSupport(String dateString) { return !(dateString == null || dateString.length() < 4) && supportFormatter.parallelStream().anyMatch(formatter -> formatter.support(dateString)); } static DateFormatter getFormatter(String dateString) { if (dateString == null || dateString.length() < 4) return null; for (DateFormatter formatter : supportFormatter) { if (formatter.support(dateString)) return formatter; } return null; } }
相关文章推荐
- android 时间,日期,各种不同显示的格式化工具
- java正则实现各种日期格式化
- java日期格式化工具。
- JAVA日期格式化工具【L】
- java格式化日期大全
- JAVA将十位时间戳格式化为日期Date类型
- JAVA将十位时间戳格式化为日期Date类型
- Java写的获取日期工具类(今天、昨天、本周、上周、本月、上月、本年及特定时间的开始时间和结束时间)
- Java字符串,日期的格式化
- Java的日期格式化常用方法
- Java日期格式化
- java日期格式化、解析
- JAVA中String.format的用法 格式化字符串,格式化数字,日期时间格式化,
- java日期格式化总结
- JAVA中使用format()方法格式化数据(日期转换,货币)
- java中各种类型转换成Byte[]的工具方法
- 详解Java中格式化日期的DateFormat与SimpleDateFormat类
- 日期格式化工具DateUtils
- java中获取当前系统时间,日期并格式化输出
- java 各种日期格式统一为一种YYYYMMDD