diff --git a/src/main/java/cokr/xit/foundation/util/DateFormats.java b/src/main/java/cokr/xit/foundation/util/DateFormats.java new file mode 100644 index 0000000..a7696a5 --- /dev/null +++ b/src/main/java/cokr/xit/foundation/util/DateFormats.java @@ -0,0 +1,30 @@ +package cokr.xit.foundation.util; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; + +import cokr.xit.foundation.Assert; + +public class DateFormats { + private static final HashMap dateFormats = new HashMap<>(); + + public String format(String pattern, Date date) { + if (date == null) return ""; + return dateFormat(pattern).format(date); + } + + private SimpleDateFormat dateFormat(String pattern) { + return dateFormats.computeIfAbsent(pattern, key -> new SimpleDateFormat(key)); + } + + public Date parse(String pattern, String str) { + if (Assert.isEmpty(str)) return null; + + try { + return dateFormat(pattern).parse(str); + } catch (Exception e) { + throw Assert.runtimeException(e); + } + } +} \ No newline at end of file