반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

class Subject {
   private static final int ONE_MIN = 60;
   private static final int ONE_HOUR = 3600;
   private static final int ONE_DAY = 86400;
   private static final int ONE_MONTH = 2592000;
   private static final int ONE_YEAR = 31104000;

   public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int testCase = Integer.parseInt(br.readLine());

        String today = br.readLine();
        DateFormat todayDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date todayDate = null;
        try {
            todayDate = todayDateFormat.parse(today);
        } catch (ParseException e) {
            System.out.println("Today format error.");
            return;
        }

        for(int i = 0 ; i < testCase; i ++) {
            String otherDay = br.readLine();
            DateFormat otherDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date otherDate = null;
            try {
                otherDate = otherDateFormat.parse(otherDay);
            } catch (ParseException e) {
                System.out.println("Other day format error.");
                continue;
            }

           // 해당 diff를 초단위로 변환
            long diffInMillies = todayDate.getTime() - otherDate.getTime();
            long second = TimeUnit.SECONDS.convert(diffInMillies, TimeUnit.MILLISECONDS);

           // 현재 시간 대비 작성된 내용이 얼마나 지났는지 표현
           if (0 <= second && second < ONE_MIN) {
                System.out.println("방금 전");
            } else if(ONE_MIN <= second && second < ONE_HOUR) {
                System.out.println(second / ONE_MIN + "분 전");
            } else if(ONE_HOUR <= second && second < ONE_DAY) {
                System.out.println(second / ONE_HOUR + "시간 전");
            } else if(ONE_DAY <= second && second < ONE_MONTH) {
                System.out.println(second / ONE_DAY + "일 전");
            } else if(ONE_MONTH <= second && second < ONE_YEAR) {
                System.out.println(second / ONE_MONTH + "개월 전");
            } else if(second >= ONE_YEAR) {
                DateFormat output = new SimpleDateFormat("yy년 M월 d일", Locale.KOREAN);
                ParsePosition pos = new ParsePosition(0);
                Date outTime = otherDateFormat.parse(otherDay, pos);
                String ans = output.format(outTime);
                System.out.println(ans);
            } else {
               System.out.println("error");
           }
        }
   }
}
반응형