Created
May 12, 2013 15:28
-
-
Save YoungjaeKim/5563920 to your computer and use it in GitHub Desktop.
기록물 시간 표시에 사용되는 친숙한 시간 표현 변환.
최적화 덜 됨.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 친숙한 시간 표기법. | |
* @param date 과거시간데이터. | |
* @return 한국어로 친숙한 시간 설명 출력. | |
* @exception 미래값을 넣으면 {@link IllegalArgumentException} 발생. | |
*/ | |
public static String toFriendlyDateTimeString(DateTime date){ | |
DateTime now = new DateTime(); | |
if (date.isAfterNow()) | |
throw new IllegalArgumentException("Future DateTime cannot be not handled."); | |
Period period = new Period(date, now); | |
if(date.isBefore(new DateTime().minusYears(1))) return period.getYears() + "년 전"; | |
if(date.isBefore(new DateTime().minusMonths(1))) return period.getMonths() + "달 전"; | |
if(date.isBefore(new DateTime().minusWeeks(1))) return period.getWeeks() + "주 전"; | |
if(date.isBefore(new DateTime().minusDays(1))){ | |
if(period.getDays() == 1) | |
return "하루 전"; | |
if(period.getDays() == 2) | |
return "이틀 전"; | |
return period.getDays() + "일 전"; | |
} | |
if(date.isBefore(new DateTime().minusHours(1))) return period.getHours() + "시간 전"; | |
if(date.isBefore(new DateTime().minusMinutes(1))) return period.getMinutes() + "분 전"; | |
if(date.isBefore(new DateTime().minusSeconds(1))) return period.getSeconds() + "초 전"; | |
return "몇 초전"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment