Programming/java

[java] 몇 분전, 몇 시간전, 몇 일전 표현 Util

성일만 2014. 12. 11. 11:15

몇 분전, 몇 시간전, 몇 일전 표현 Util



private static class TIME_MAXIMUM {

    public static final int SEC = 60;
    public static final int MIN = 60;
    public static final int HOUR = 24;
    public static final int DAY = 30;
    public static final int MONTH = 12;
}

public String calculateTime(Date date) {
    long curTime = System.currentTimeMillis();
    long regTime = date.getTime();
    long diffTime = (curTime - regTime) / 1000;

    String msg = null;

    if(diffTime < TIME_MAXIMUM.SEC) {
        // sec
        msg = diffTime + "초전";
    } else if ((diffTime /= TIME_MAXIMUM.SEC) < TIME_MAXIMUM.MIN) {
        // min
        System.out.println(diffTime);
        msg = diffTime + "분전";
    } else if ((diffTime /= TIME_MAXIMUM.MIN) < TIME_MAXIMUM.HOUR) {
        // hour
        msg = (diffTime ) + "시간전";
    } else if ((diffTime /= TIME_MAXIMUM.HOUR) < TIME_MAXIMUM.DAY) {
        // day
        msg = (diffTime ) + "일전";
    } else if ((diffTime /= TIME_MAXIMUM.DAY) < TIME_MAXIMUM.MONTH) {
        // day
        msg = (diffTime ) + "달전";
    } else {
        msg = (diffTime) + "년전";
    }
    

return msg;

}


처 : http://gubok.tistory.com/227