有没有什么工具包能方便把时间显示为“一周前”,“3小时前”这样的格式?

解决方案 »

  1.   


    //三天前
    public String getDayBefore(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar  ca = Calendar.getInstance();
    ca.add(Calendar.DATE,-3);
    java.util.Date date = ca.getTime();
    String strDate = sdf.format(date);
    return strDate;
    }
      

  2.   

    不要意思 我看成3天前了   不过一样的  把 ca.add(Calendar.DATE,-3);
    改成 ca.add(Calendar.HOUR,-3);
      

  3.   

    给思路:一个时间, date  与你系统当前时间 new Date 比较, 得到相差天数..(Java有提供,我忘了,百度一下)
    判断, 小于7: 相差的天数+"天前" else 相差天数/7+"周前"
      

  4.   

    自己找到了,http://ocpsoft.com/prettytime/可以实现
      

  5.   

    import com.ocpsoft.pretty.time.PrettyTime;Calendar calendar = Calendar.getInstance();
    PrettyTime pretty = new PrettyTime();pretty.format(calendar.getTime()); // 片刻之前calendar.set(2011,8,22);
    pretty.format(calendar.getTime()); // 6 天 后calendar.set(2011,7,22);
    pretty.format(calendar.getTime()); // 4 周 前calendar.set(2011,8,12);
    pretty.format(calendar.getTime()); // 4 天 前calendar.set(2011,8,2);
    pretty.format(calendar.getTime()); // 2 周 前
      

  6.   

    不需要引入jar包
    DateFormat df = new SimpleDateFormat("yyyy年m月d日");

    Date  now =df.parse("2011年10月16日");

    Date time=df.parse("2011年10月18日");


    long r=(now.getTime()-time.getTime())/(3600*24*1000);

    if(r==0) System.out.println("当天");
    else System.out.println(r>0? r+"天前":-r+"天后");
      

  7.   


    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;public class TestTimeFormat {
        public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance(); for (int i = 12; i > 7; i--) {// 使用最近5小时进行测试
        calendar.set(2011, 8, 16,i,0);
        Date date = calendar.getTime();
        System.out.println(date + "   ");
        System.out.println(getName(date));
    }  
    System.out.println("-----------------------------------------");
    for (int i = 16; i > 0; i--) {// 使用最近16天进行测试
        calendar.set(2011, 8, i);
        Date date = calendar.getTime();
        System.out.println(date + "   ");
        System.out.println(getName(date));
    }
        }    public static String getName(Date date) {// 获得名字
    long millis = getMillisOfTimeDifference(date, new Date());
    if (millis > 1000 * 3600) {
        if (millis > 1000 * 3600 * 3) {
    if (millis > 1000 * 3600 * 24) {
        if (millis > 1000 * 3600 * 24 * 3) {
    if (millis > 1000 * 3600 * 24 * 7) {
        if (millis > 1000 * 3600 * 24 * 7 * 3) {
    return "三周前";
        }
        return "一周前";
    }
    return "三天前";
        }
        return "一天前";
    }
    return "三小时前";
        }
        return "一小时前";
    }
    SimpleDateFormat f = new SimpleDateFormat("今天 kk时mm分ss秒");
    String newTypeDate = f.format(date);
    return newTypeDate;
        }    public static long getMillisOfTimeDifference(Date date1, Date date2) {// 获得时间差
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date1);
    long timelnMillis1 = calendar.getTimeInMillis();
    calendar.setTime(date2);
    long timelnMillis2 = calendar.getTimeInMillis();
    return timelnMillis2 - timelnMillis1;
        }
    }
    /*output: 我现在的时间11:57分
    Fri Sep 16 12:00:53 CST 2011   
    今天 12时00分53秒
    Fri Sep 16 11:00:53 CST 2011   
    今天 11时00分53秒
    Fri Sep 16 10:00:53 CST 2011   
    一小时前
    Fri Sep 16 09:00:53 CST 2011   
    一小时前
    Fri Sep 16 08:00:53 CST 2011   
    三小时前
    -----------------------------------------
    Fri Sep 16 08:00:53 CST 2011   
    三小时前
    Thu Sep 15 08:00:53 CST 2011   
    一天前
    Wed Sep 14 08:00:53 CST 2011   
    一天前
    Tue Sep 13 08:00:53 CST 2011   
    三天前
    Mon Sep 12 08:00:53 CST 2011   
    三天前
    Sun Sep 11 08:00:53 CST 2011   
    三天前
    Sat Sep 10 08:00:53 CST 2011   
    三天前
    Fri Sep 09 08:00:53 CST 2011   
    一周前
    Thu Sep 08 08:00:53 CST 2011   
    一周前
    Wed Sep 07 08:00:53 CST 2011   
    一周前
    Tue Sep 06 08:00:53 CST 2011   
    一周前
    Mon Sep 05 08:00:53 CST 2011   
    一周前
    Sun Sep 04 08:00:53 CST 2011   
    一周前
    Sat Sep 03 08:00:53 CST 2011   
    一周前
    Fri Sep 02 08:00:53 CST 2011   
    一周前
    Thu Sep 01 08:00:53 CST 2011   
    一周前
     */