需要 prettytime-3.2.3.Final.jar 包 代碼例子 輸出結果 ...
需要 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 小時 後