From 759a2d4898c3895083446ddc007a041d16646cdc Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Mon, 1 Apr 2024 16:16:56 +0900 Subject: [PATCH] =?UTF-8?q?=EB=82=A0=EC=A7=9C=ED=8F=AC=EB=A7=B7=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cokr/xit/foundation/data/DataFormat.java | 77 +++++++++++-------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/src/main/java/cokr/xit/foundation/data/DataFormat.java b/src/main/java/cokr/xit/foundation/data/DataFormat.java index 738c8b2..722f08c 100644 --- a/src/main/java/cokr/xit/foundation/data/DataFormat.java +++ b/src/main/java/cokr/xit/foundation/data/DataFormat.java @@ -1,6 +1,8 @@ package cokr.xit.foundation.data; import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import cokr.xit.foundation.AbstractComponent; @@ -28,15 +30,19 @@ public class DataFormat extends AbstractComponent { public static final String yyyy_mm_dd(Object obj) { if (isEmpty(obj)) return ""; - String str = obj.toString(); - int length = str.length(), - pos = length - 2; + if (obj instanceof String) { + String str = obj.toString(); + int length = str.length(), + pos = length - 2; - String - day = str.substring(pos), - month = str.substring(pos = pos - 2, pos + 2), - year = str.substring(0, pos); - return String.format("%s-%s-%s", year, month, day); + String + day = str.substring(pos), + month = str.substring(pos = pos - 2, pos + 2), + year = str.substring(0, pos); + return String.format("%s-%s-%s", year, month, day); + } else { + return yyyy_mm_dd_hh_mm_ss(obj).substring(0, 10); + } } /**HHmmss 포맷의 문자열을 HH:mm:ss 포맷으로 변환하여 반환한다. @@ -46,37 +52,48 @@ public class DataFormat extends AbstractComponent { public static final String hh_mm_ss(Object obj) { if (isEmpty(obj)) return ""; - String str = obj.toString(); - int length = str.length(), - pos = length - 2; + if (obj instanceof String) { + String str = obj.toString(); + int length = str.length(), + pos = length - 2; - String - ss = str.substring(pos), - mm = str.substring(pos = pos - 2, pos + 2), - hh = str.substring(0, pos); - return String.format("%s:%s:%s", hh, mm, ss); + String + ss = str.substring(pos), + mm = str.substring(pos = pos - 2, pos + 2), + hh = str.substring(0, pos); + return String.format("%s:%s:%s", hh, mm, ss); + } else { + return yyyy_mm_dd_hh_mm_ss(obj).substring(11); + } } + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + /**yyyyMMddHHmmss, 또는 yyMMddHHmmss 포맷의 문자열을 yyyy-MM-dd HH:mm:ss 포맷으로 변환하여 반환한다. * @param obj yyyyMMddHHmmss, 또는 yyMMddHHmmss 포맷의 문자열 * @return yyyy-MM-dd HH:mm:ss 포맷 문자열 */ - public static final String yyyy_MM_dd_HH_mm_ss(Object obj) { + public static final String yyyy_mm_dd_hh_mm_ss(Object obj) { if (isEmpty(obj)) return ""; - String str = obj.toString(); - int length = str.length(), - pos = length - 6; - - try { - String - date = str.substring(0, pos), - time = str.substring(pos); - - return yyyy_mm_dd(date) + " " + hh_mm_ss(time); - } catch (Exception e) { - return str; - } + if (obj instanceof String) { + String str = obj.toString(); + int length = str.length(), + pos = length - 6; + + try { + String + date = str.substring(0, pos), + time = str.substring(pos); + + return yyyy_mm_dd(date) + " " + hh_mm_ss(time); + } catch (Exception e) { + return str; + } + } else if (obj instanceof Date) + return dateFormat.format((Date)obj); + else + throw new IllegalArgumentException(obj + " is not a Date"); } private static final DecimalFormat numberFormat = new DecimalFormat("#,##0.00");