From 73af094530830b3db2f75b78ce0b19a461eeab06 Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Mon, 7 Aug 2023 16:48:08 +0900 Subject: [PATCH] =?UTF-8?q?DateFormats=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cokr/xit/foundation/util/DateFormats.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/cokr/xit/foundation/util/DateFormats.java 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