날짜포맷 메소드 수정

master
mjkhan21 8 months ago
parent 728d527752
commit 759a2d4898

@ -1,6 +1,8 @@
package cokr.xit.foundation.data; package cokr.xit.foundation.data;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import cokr.xit.foundation.AbstractComponent; import cokr.xit.foundation.AbstractComponent;
@ -28,6 +30,7 @@ public class DataFormat extends AbstractComponent {
public static final String yyyy_mm_dd(Object obj) { public static final String yyyy_mm_dd(Object obj) {
if (isEmpty(obj)) return ""; if (isEmpty(obj)) return "";
if (obj instanceof String) {
String str = obj.toString(); String str = obj.toString();
int length = str.length(), int length = str.length(),
pos = length - 2; pos = length - 2;
@ -37,6 +40,9 @@ public class DataFormat extends AbstractComponent {
month = str.substring(pos = pos - 2, pos + 2), month = str.substring(pos = pos - 2, pos + 2),
year = str.substring(0, pos); year = str.substring(0, pos);
return String.format("%s-%s-%s", year, month, day); 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 . /**HHmmss HH:mm:ss .
@ -46,6 +52,7 @@ public class DataFormat extends AbstractComponent {
public static final String hh_mm_ss(Object obj) { public static final String hh_mm_ss(Object obj) {
if (isEmpty(obj)) return ""; if (isEmpty(obj)) return "";
if (obj instanceof String) {
String str = obj.toString(); String str = obj.toString();
int length = str.length(), int length = str.length(),
pos = length - 2; pos = length - 2;
@ -55,15 +62,21 @@ public class DataFormat extends AbstractComponent {
mm = str.substring(pos = pos - 2, pos + 2), mm = str.substring(pos = pos - 2, pos + 2),
hh = str.substring(0, pos); hh = str.substring(0, pos);
return String.format("%s:%s:%s", hh, mm, ss); 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 . /**yyyyMMddHHmmss, yyMMddHHmmss yyyy-MM-dd HH:mm:ss .
* @param obj yyyyMMddHHmmss, yyMMddHHmmss * @param obj yyyyMMddHHmmss, yyMMddHHmmss
* @return yyyy-MM-dd HH:mm:ss * @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 ""; if (isEmpty(obj)) return "";
if (obj instanceof String) {
String str = obj.toString(); String str = obj.toString();
int length = str.length(), int length = str.length(),
pos = length - 6; pos = length - 6;
@ -77,6 +90,10 @@ public class DataFormat extends AbstractComponent {
} catch (Exception e) { } catch (Exception e) {
return str; 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"); private static final DecimalFormat numberFormat = new DecimalFormat("#,##0.00");

Loading…
Cancel
Save