0

如何将日期转换为其文本格式..例如:如果今天更新..那么它必须显示“今天”,而不是日期,必须显示“昨天”,然后两天后..它必须显示更新的一般形式的日期(/ /_)..我尝试使用 SimpleDateFormat..但不工作..

 SimpleDateFormat sdf =  new SimpleDateFormat("dd-MM-yyyy");

        Date d= new Date(); 

        //Convert Date object to string
        String strDate = sdf.format(d);
        System.out.println("Formated String is " + strDate);


        d  = sdf.parse("31-12-2009");

请帮助..提前谢谢..

4

6 回答 6

1

看看PrettyTime库。

于 2013-11-13T12:00:44.803 回答
1

尝试这个:

public class TimeUtils {

      public final static long ONE_SECOND = 1000;
      public final static long SECONDS = 60;

      public final static long ONE_MINUTE = ONE_SECOND * 60;
      public final static long MINUTES = 60;

      public final static long ONE_HOUR = ONE_MINUTE * 60;
      public final static long HOURS = 24;

      public final static long ONE_DAY = ONE_HOUR * 24;

      private TimeUtils() {
      }

      /**
       * converts time (in milliseconds) to human-readable format
       *  "<w> days, <x> hours, <y> minutes and (z) seconds"
       */
      public static String millisToLongDHMS(long duration) {
        StringBuffer res = new StringBuffer();
        long temp = 0;
        if (duration >= ONE_SECOND) {
          temp = duration / ONE_DAY;
          if (temp > 0) {
            duration -= temp * ONE_DAY;
            res.append(temp).append(" day").append(temp > 1 ? "s" : "")
               .append(duration >= ONE_MINUTE ? ", " : "");
          }

          temp = duration / ONE_HOUR;
          if (temp > 0) {
            duration -= temp * ONE_HOUR;
            res.append(temp).append(" hour").append(temp > 1 ? "s" : "")
               .append(duration >= ONE_MINUTE ? ", " : "");
          }

          temp = duration / ONE_MINUTE;
          if (temp > 0) {
            duration -= temp * ONE_MINUTE;
            res.append(temp).append(" minute").append(temp > 1 ? "s" : "");
          }

          if (!res.toString().equals("") && duration >= ONE_SECOND) {
            res.append(" and ");
          }

          temp = duration / ONE_SECOND;
          if (temp > 0) {
            res.append(temp).append(" second").append(temp > 1 ? "s" : "");
          }
          return res.toString();
        } else {
          return "0 second";
        }
      }


      public static void main(String args[]) {
        System.out.println(millisToLongDHMS(123));
        System.out.println(millisToLongDHMS((5 * ONE_SECOND) + 123));
        System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR));
        System.out.println(millisToLongDHMS(ONE_DAY + 2 * ONE_SECOND));
        System.out.println(millisToLongDHMS(ONE_DAY + ONE_HOUR + (2 * ONE_MINUTE)));
        System.out.println(millisToLongDHMS((4 * ONE_DAY) + (3 * ONE_HOUR)
            + (2 * ONE_MINUTE) + ONE_SECOND));
        System.out.println(millisToLongDHMS((5 * ONE_DAY) + (4 * ONE_HOUR)
            + ONE_MINUTE + (23 * ONE_SECOND) + 123));
        System.out.println(millisToLongDHMS(42 * ONE_DAY));
        /*
          output :
                0 second
                5 seconds
                1 day, 1 hour
                1 day and 2 seconds
                1 day, 1 hour, 2 minutes
                4 days, 3 hours, 2 minutes and 1 second
                5 days, 4 hours, 1 minute and 23 seconds
                42 days
         */
    }
}
于 2013-11-13T11:56:38.487 回答
0

您可以检查此日期比较

于 2013-11-13T11:47:00.430 回答
0

您必须根据时差实现自己的逻辑并使用相应的日期格式。

假设您从服务器获取日期。获取设备的时间并与您的日期进行比较。根据您的要求,将有两种情况。

  1. 两个日期之差小于一天,则返回“Today”字符串。
  2. 两个日期之间的差异大于一天,然后使用简单日期格式根据需要设置日期格式。
于 2013-11-13T11:51:44.763 回答
0

如需比较日期,请参阅此条目:datecompare

于 2013-11-13T11:48:49.273 回答
0
import java.text.SimpleDateFormat;
import java.util.Date;

public class App {

    SimpleDateFormat sdf =  new SimpleDateFormat("dd-MM-yyyy"); 
    public static long ms, s, m, h, d, w;

    static {
        ms = 1;
        s = ms * 1000;
        m = s * 60;
        h = m * 60;
        d = h * 24;
        w = d * 7;
    }

    public App() {
           Date now = new Date();
           Date old = new Date();

           try {
               old = sdf.parse("12-11-2013");
           } catch (Exception e) {
               e.printStackTrace();
           }

           long diff = now.getTime() - old.getTime();

           if (diff < this.d) {
               System.out.println("Today");
           }
           else if (diff > this.d && diff < this.d*2) {
               System.out.println("Yesterday");
           }

           System.out.println("Difference: " + msToHms(diff));
    }

    public String msToHms(long ms) {
        int seconds = (int) (ms / this.s) % 60 ;
        int minutes = (int) ((ms / this.m) % 60);
        int hours   = (int) ((ms / this.h) % 24);

        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }

    public static void main(String[] args) {
        new App();
    }
}

输出

Yesterday
Difference: 07:11:22
于 2013-11-13T12:06:28.287 回答