如何在java中获得时间

如何在java中获得时间,java,Java,如何使用java在几分钟、几小时或几天内找到上次评论 例如: 8分钟前 8小时前 8天前 8个月前 8年前只需在两个日期时间之间执行一个减法运算,并使用SimpleDateFormat类为其提供一些格式 这里有一个链接可能对您有所帮助 只需在两个日期时间之间执行减法运算,并使用SimpleDateFormat类为其提供一些格式 这里有一个链接可能对您有所帮助 //使用这个类 公共类TimeUtils{ public final static long ONE_SECOND = 1000;

如何使用java在几分钟、几小时或几天内找到上次评论

例如:

8分钟前 8小时前 8天前 8个月前
8年前

只需在两个日期时间之间执行一个减法运算,并使用SimpleDateFormat类为其提供一些格式

这里有一个链接可能对您有所帮助

只需在两个日期时间之间执行减法运算,并使用SimpleDateFormat类为其提供一些格式

这里有一个链接可能对您有所帮助

//使用这个类

公共类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
     */
}
公共最终静态长1秒=1000;
公共最终静态长秒=60;
公开决赛静态长1分=1秒*60;
公共最终静态长分钟=60;
公共最终静态长1小时=1分钟*60;
公共最终静态长时间=24小时;
公共最终静态长一天=一小时*24;
私有TimeUtils(){
}
/**
*将时间(以毫秒为单位)转换为人类可读的格式
*天、小时、分钟和(z)秒
*/
公共静态字符串millisToLongDHMS(长持续时间){
StringBuffer res=新的StringBuffer();
长期温度=0;
如果(持续时间>=1秒){
温度=持续时间/一天;
如果(温度>0){
持续时间-=临时*一天;
res.append(临时)。append(“日”)。append(临时>1?“s”:“”)
.append(持续时间>=一分钟?,“:”);
}
温度=持续时间/一小时;
如果(温度>0){
持续时间-=温度*一小时;
res.append(temp.append(“hour”).append(temp>1?“s”):“”)
.append(持续时间>=一分钟?,“:”);
}
温度=持续时间/一分钟;
如果(温度>0){
持续时间-=温度*一分钟;
res.append(temp.append(“分钟”)。append(temp>1?“s”:“);
}
如果(!res.toString().equals(“”&&duration>=1秒){
附加(“和”);
}
温度=持续时间/1秒;
如果(温度>0){
res.append(temp.append(“第二”)。append(temp>1?“s”:“);
}
return res.toString();
}否则{
返回“0秒”;
}
}
公共静态void main(字符串参数[]){
系统输出打印LN(毫秒长DHMS(123));
System.out.println(毫秒((5*1秒)+123));
System.out.println(毫秒长(一天+一小时));
System.out.println(毫秒长(一天+2*1秒));
System.out.println(毫秒长(一天+一小时+(2*1分钟));
System.out.println(毫秒长((4*1天)+(3*1小时)
+(2*一分钟)+一秒);
System.out.println(毫秒长((5*1天)+(4*1小时)
+一分钟+(23*1秒)+123);
系统输出打印Ln(毫秒(42*一天);
/*
输出:
0秒
5秒
1天1小时
1天2秒
1天1小时2分钟
4天3小时2分1秒
5天4小时1分23秒
42天
*/
}
}

//使用这个类

公共类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
     */
}
公共最终静态长1秒=1000;
公共最终静态长秒=60;
公开决赛静态长1分=1秒*60;
公共最终静态长分钟=60;
公共最终静态长1小时=1分钟*60;
公共最终静态长时间=24小时;
公共最终静态长一天=一小时*24;
私有TimeUtils(){
}
/**
*将时间(以毫秒为单位)转换为人类可读的格式
*天、小时、分钟和(z)秒
*/
公共静态字符串millisToLongDHMS(长持续时间){
StringBuffer res=新的StringBuffer();
长期温度=0;
如果(持续时间>=1秒){
温度=持续时间/一天;
如果(温度>0){
持续时间-=临时*一天;
res.append(临时)。append(“日”)。append(临时>1?“s”:“”)
.append(持续时间>=一分钟?,“:”);
}
温度=持续时间/一小时;
如果(温度>0){
持续时间-=温度*一小时;
res.append(temp.append(“hour”).append(temp>1?“s”):“”)
.append(持续时间>=一分钟?,“:”);
}
温度=持续时间/一分钟;
如果(温度>0){
持续时间-=温度*一分钟;
res.append(temp.append(“分钟”)。append(temp>1?“s”:“);
}
如果(!res.toString().equals(“”&&duration>=1秒){
附加(“和”);
}
温度=持续时间/1秒;
如果(温度>0){
res.append(temp.append(“第二”)。append(temp>1?“s”:“);
}
return res.toString();
}否则{
返回“0秒”;
}
}
公共静态void main(字符串参数[]){
系统输出打印LN(毫秒长DHMS(123));
System.out.println(毫秒((5*1秒)+123));
System.out.println(毫秒长(一天+一小时));
System.out.println(毫秒长(一天+2*1秒));
System.out.println(毫秒长(一天+一小时+(2*1分钟));
System.out.println(毫秒长((4*1天)+(3*1小时)
+(2*一分钟)+一秒);
System.out.println(毫秒长((5*1天)+(4*1小时)
+一分钟+(23*1秒)+123);
系统输出打印Ln(毫秒(42*一天);
/*
输出:
0秒
5秒
1天1小时
1天2秒
1天1小时2分钟
4天3小时2分1秒
5天4小时1分23秒
42天
*/
}

}

在maven依赖项下面添加

<dependency>
    <groupId>org.ocpsoft.prettytime</groupId>
    <artifactId>prettytime</artifactId>
    <version>1.0.8.Final</version>
</dependency> 
回答:是:5小时前


在maven依赖项下面添加

<dependency>
    <groupId>org.ocpsoft.prettytime</groupId>
    <artifactId>prettytime</artifactId>
    <version>1.0.8.Final</version>
</dependency> 
回答:是:5小时前


你的数据是什么?你试过什么?你说的“最后评论时间”是什么意思?在这种情况下,什么是评论?所以