java util - 时间工具包 PrettyTime

 

需要 prettytime-3.2.3.Final.jar 包

 

代码例子

package cn.java.prettytime;

import java.util.Date;
import java.util.Locale;

import org.ocpsoft.prettytime.PrettyTime;

/**
 * 时间间隔
 * @author zhouzhian
 *
 */
public class Test {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
    }

    public static void test1() {
//        PrettyTime p = new PrettyTime();
        PrettyTime p = new PrettyTime(Locale.CHINESE);
        System.out.println(p.format(new Date()));
    }

    public static void test2() {
        PrettyTime p = new PrettyTime(Locale.CHINESE);
        Date mDate = new Date();
        long milliseconds = mDate.getTime();
        milliseconds = milliseconds - 3600*1000;
        System.out.println(p.format(new Date(milliseconds)));
    }

    public static void test3() {
        PrettyTime p = new PrettyTime(Locale.CHINESE);
        Date mDate = new Date();
        long milliseconds = mDate.getTime();
        milliseconds = milliseconds - 3600 * 24 *1000;
        System.out.println(p.format(new Date(milliseconds)));
    }

    public static void test4() {
        PrettyTime p = new PrettyTime(Locale.CHINESE);
        Date mDate = new Date();
        long milliseconds = mDate.getTime();
        milliseconds = milliseconds + 3600*1000;
        System.out.println(p.format(new Date(milliseconds)));
    }
}

输出结果

刚刚
1 小时 前
1 天 前
1 小时 后

 

posted on 2017-01-19 10:59  周~~  阅读(1782)  评论(0编辑  收藏  举报

导航