Browse Source

[이벤트][New] 날짜 표시 형식 통일을 위한 메서드 생성

hyodong.min 6 years ago
parent
commit
06abae5dd7

+ 30 - 5
app/src/main/java/kr/co/zumo/app/lifeplus/bean/api/EventBean.java

@@ -39,7 +39,7 @@ public class EventBean extends LifeplusContentsBean {
   public static final String TYPE_SELECT_TEXT = "02";
   public static final String TYPE_SELECT_TEXT_IMAGE = "03";
 
-  public static final String DATE_FORMAT_SHORT = Formatter.DATE_FORMAT_DASH;
+  public static final String DATE_FORMAT = Formatter.DATE_FORMAT_DASH;
 
   @SerializedName("evntType")
   private String eventType; // 01:응모형, 02:항목선택형_글자형, 03:항목선택형_복합형
@@ -109,7 +109,7 @@ public class EventBean extends LifeplusContentsBean {
    */
   public boolean isEventEnd() {
     // 종료일 24시까지 비교(+24)해야 하므로 오늘 시간에서 24시간을 빼줌(-24)
-    return isEnd(DATE_FORMAT_SHORT, getTimeTo(), System.currentTimeMillis() - Formatter.A_DAY_MILLIS);
+    return isEnd(DATE_FORMAT, getTimeTo(), System.currentTimeMillis() - Formatter.A_DAY_MILLIS);
   }
 
   /**
@@ -119,14 +119,39 @@ public class EventBean extends LifeplusContentsBean {
    */
   public boolean isBetween() {
     long now = System.currentTimeMillis();
-    return isBeginning(DATE_FORMAT_SHORT, getTimeFrom(), now) && false == isEnd(DATE_FORMAT_SHORT, getTimeTo(), now);
+    return isBeginning(DATE_FORMAT, getTimeFrom(), now) && false == isEnd(DATE_FORMAT, getTimeTo(), now);
   }
 
-  public static boolean isBeginning(String format, String from, long nowMillis) {
+  static boolean isBeginning(String format, String from, long nowMillis) {
     return Formatter.dateStringToMillis(format, from) <= nowMillis;
   }
 
-  public static boolean isEnd(String format, String to, long nowMillis) {
+  static boolean isEnd(String format, String to, long nowMillis) {
     return Formatter.dateStringToMillis(format, to) < nowMillis;
   }
+
+  /**
+   * 이벤트 기간 문자열
+   *
+   * @param bean EventBean
+   * @return "2019.03.29 ~ 2019.04.05"
+   */
+  public static String getTermString(EventBean bean) {
+    return new StringBuilder().append(getTimeString(bean.getTimeFrom()))
+      .append(" ~ ")
+      .append(getTimeString(bean.getTimeTo())).toString();
+  }
+
+  /**
+   * 날짜 표시 형식 변환
+   *
+   * @param timeString
+   * @return
+   */
+  public static String getTimeString(String timeString) {
+    if (Formatter.validateDateFormat(EventBean.DATE_FORMAT, timeString) == false) {
+      return timeString;
+    }
+    return Formatter.format(timeString, EventBean.DATE_FORMAT, Formatter.DATE_FORMAT_DOT);
+  }
 }

+ 26 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/util/Formatter.java

@@ -26,7 +26,9 @@ public class Formatter {
 
   public static final String DATE_FORMAT_DOT = "yyyy.MM.dd";
   public static final String DATE_FORMAT_DASH = "yyyy-MM-dd";
-  public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
+  public static final String DATE_TIME_API_FORMAT = "yyyy-MM-dd HH:mm:ss";
+  public static final String DATE_TIME_UI_FORMAT = "yyyy.MM.dd HH:mm:ss";
+  public static final String DATE_TIME_SHORT_UI_FORMAT = "yyyy.MM.dd HH:mm";
 
   /**
    * 날짜/시간 변환
@@ -73,4 +75,27 @@ public class Formatter {
       return 0;
     }
   }
+
+  /**
+   * 요구하는 날짜 형식이 맞는지 확인
+   *
+   * @param format     "yyyy-MM-dd"
+   * @param dateString "2018-10-21"
+   * @return true
+   */
+  public static boolean validateDateFormat(String format, String dateString) {
+    if (null == format || null == dateString) {
+      return false;
+    }
+    SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
+    //To make strict date format validation
+    formatter.setLenient(false);
+    try {
+      formatter.parse(dateString);
+      return true;
+    } catch (ParseException e) {
+      //Handle exception
+      return false;
+    }
+  }
 }