ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문자열 날짜 포멧 변경
    Language/Java 2023. 4. 18. 13:05

     

    문자열 날짜를 포멧형식을 변경해서 사용할 일이 있어서 간단하게 만들어 보았다, 어려운건 아니라서 나름의 형식에 따라 필요한 부분만 추가해 보았다.

     

    날짜 관련 유틸

    ...
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    ...
    
    
    public class DateUtils {
      ...
      
      /**
       * DateUtils.formatLocalDateString("20230405", "yyyyMMdd", "yy.MM.dd");
       *
       * @param date
       * @param fromPattern
       * @param toPattern
       * @return
       */
      public static String formatLocalDateString(String date, String fromPattern, String toPattern) {
        LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern(fromPattern));
        return localDate.format(DateTimeFormatter.ofPattern(toPattern));
      }
    
      /**
       * DateUtils.formatLocalDateTimeString("2023-04-05 13:01:40.0", "yyyy-MM-dd HH:mm:SS", "yy.MM.dd HH:mm");
       *
       * @param date
       * @param fromPattern
       * @param toPattern
       * @return
       */
      public static String formatLocalDateTimeString(String date, String fromPattern, String toPattern) {
        LocalDateTime localDate = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(fromPattern));
        return localDate.format(DateTimeFormatter.ofPattern(toPattern));
      }
      
      /**
       * DateUtils.formatDate(DateUtils.now(), "HH:mm:ss")
       *
       * @param date
       * @param format
       * @return
       */
      public static String formatDate(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
      }
    
      /**
       * DateUtils.now()
       *
       * @return
       */
      public static Date now() {
        return new Date();
      }
      
      
      
      
      /**
       * DateUtil.getNow("yyyyMMddHHmmss")
       *
       * @return
       */
      public static String getNow(String format) {
        return getDate(new Date(), format);
      }
    
      public static String getDate(String format) {
        return getDate(new Date(), format);
      }
      
      /**
       * DateUtils.getDate("yyyyMMdd")
       *
       * @return
       */
      public static String getDate(Date date, String format) {
        Locale currentLocale = new Locale("KOREAN", "KOREA");
        SimpleDateFormat formatter = new SimpleDateFormat(format, currentLocale);
        return formatter.format(date);
      }
      
      
      /**
       * <p>데이트 포맷과 데이트 문자열을 입력받아 Date 객체를 반환함.
       *
       * @param format
       * @param date
       * @return
       * @throws ParseException <p><pre>
       *                         - 사용 예
       *                         Date date = DateUtil.getDateFromStringFormat("yyyy-MM-dd", "2015-06-15")
       *                        </pre>
       */
      public static Date getDateFromStringFormat(String format, String date) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat(format);
        return f.parse(date);
      }
      
      /**
       * DateUtils.addMonths(DateUtils.getDateFromStringFormat("yyyyMMdd", "20230130"), 1)
       *
       * @param date
       * @param monthsToAdd
       * @return
       */
      public static Date addMonths(Date date, int monthsToAdd) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, monthsToAdd);
    
        return cal.getTime();
      }
      
      
      ...
    }

     

    테스트 케이스

    import junit.framework.TestCase;
    import org.junit.Test;
    
    public class DateUtilsTest extends TestCase {
        ...
        
        @Test
        public void testFormatLocalDateString() {
            String result = DateUtils.formatLocalDateString("20230405", "yyyyMMdd", "yyyy-MM-dd");
            assertEquals(result, "2023-04-05");
        }
    
        @Test
        public void testFormatLocalDateTimeString() {
            String result = DateUtils.formatLocalDateTimeString("2023-04-05 13:01:40.0", "yyyy-MM-dd HH:mm:ss.S", "yy.MM.dd HH:mm");
            assertEquals(result, "23.04.05 13:01");
        }
        
        @Test
        public void testFormatDate() {
            String result = DateUtils.formatDate(DateUtils.now(), "HH:mm:ss");
    //        System.out.println("result : " + result);
        }
    
        @Test
        public void testAddMonth() throws ParseException {
            Date now = DateUtils.getDateFromStringFormat("yyyyMMdd", "20230130");
            Date date = DateUtils.addMonths(now, 1);
    
            String result = DateUtils.getDate(date, "yyyyMMdd");
    
            assertEquals(result, "20230228");
        }
    }

     

    후다닥.

    댓글

Designed by Tistory.