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");