-5

我正在使用以下函数来计算时差。它没有显示正确的输出。1个月的时差后显示2分钟的差异。

我的程序有什么问题?

public String TimestampDiff(Timestamp t) {
    long t1 = t.getTime();
    String st = null;
    long diff;
    java.util.Date date = new java.util.Date();
    long currT = date.getTime();
    System.out.println();
    System.out.println(" current timesstamp is  " + currT);

    diff = (currT - t1) / 60;
    int years = (int) Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
    double remainder = Math.floor(diff % (1000 * 60 * 60 * 24 * 365));
    int days = (int) Math.floor(remainder / (1000 * 60 * 60 * 24));
    remainder = Math.floor(remainder % (1000 * 60 * 60 * 24));
    int hours = (int) Math.floor(remainder / (1000 * 60 * 60));
    remainder = Math.floor(remainder % (1000 * 60 * 60));
    int minutes = (int) Math.floor(remainder / (1000 * 60));
    remainder = Math.floor(remainder % (1000 * 60));
    int seconds = (int) Math.floor(remainder / (1000));
    System.out.println("\nyr:Ds:hh:mm:ss " + years + ":" + days + ":"
            + hours + ":" + minutes + ":" + seconds);

    if (years == 0 && days == 0 && hours == 0 && minutes == 0) {
        st = "few seconds ago";
    } else if (years == 0 && days == 0 && hours == 0) {
        st = minutes + " minuts ago";
    } else if (years == 0 && days == 0) {
        st = hours + " hours ago";
    } else if (years == 0 && days == 1) {
        st = new SimpleDateFormat("'yesterday at' hh:mm a").format(t1);

    } else if (years == 0 && days > 1) {
        st = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(t1);

    } else if (years > 0) {
        st = new SimpleDateFormat("MMM d ''yy 'at' hh:mm a").format(t1);

    }
    st = st.replace("AM", "am").replace("PM", "pm");
    return st;
}
4

3 回答 3

9

我建议看一下Joda Time,并指出:

Joda-Time 是 Java SE 8 之前 Java 的事实上的标准日期和时间库。现在要求用户迁移到 java.time (JSR-310)。

安装

  • 对于基于 Debian 的系统:libjoda-time-java. 罐子将/usr/share/javajoda-time.jar
  • 对于其他人:下载最新的 jar,例如 joda-time-2.2-dist.zip,其中包括 joda-time-2.2.jar

使用 Eclipse 时,将其添加到 Java 构建路径(项目 > 属性 > Java 构建路径 > 添加外部 Jar)

相关JavaDoc

示例代码

import java.sql.Timestamp;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

public class MinimalWorkingExample {
    static Date date = new Date(1990, 4, 28, 12, 59);

    public static String getTimestampDiff(Timestamp t) {
        final DateTime start = new DateTime(date.getTime());
        final DateTime end = new DateTime(t);
        Period p = new Period(start, end);
        PeriodFormatter formatter = new PeriodFormatterBuilder()
                .printZeroAlways().minimumPrintedDigits(2).appendYears()
                .appendSuffix(" year", " years").appendSeparator(", ")
                .appendMonths().appendSuffix(" month", " months")
                .appendSeparator(", ").appendDays()
                .appendSuffix(" day", " days").appendSeparator(" and ")
                .appendHours().appendLiteral(":").appendMinutes()
                .appendLiteral(":").appendSeconds().toFormatter();
        return p.toString(formatter);
    }

    public static void main(String[] args) {
        String diff = getTimestampDiff(new Timestamp(2013, 3, 20, 7, 51, 0, 0));
        System.out.println(diff);
    }
}

输出:

22 years, 10 months, 01 day and 18:52:00

为什么我推荐一个新的解决方案

  • 它更短(726 个字符/14 行,而您的 1665 个字符/41 行)
  • 更容易理解
  • 更容易调整
  • 代码和表示分离更清晰
  • 我不想修复你的代码
于 2013-03-21T07:02:00.400 回答
2
import org.apache.commons.lang.time.DateUtils;
import java.text.SimpleDateFormat;


@Test
public void testDate() throws Exception {

    long t1 = new SimpleDateFormat("dd.MM.yyyy").parse("20.03.2013").getTime();
    long now = System.currentTimeMillis();
    String result = null;
    long diff = Math.abs(t1-now);


    if(diff < DateUtils.MILLIS_PER_MINUTE){
         result =  "few seconds ago";
    }else if(diff < DateUtils.MILLIS_PER_HOUR){
         result = (int)(diff/DateUtils.MILLIS_PER_MINUTE) + " minuts ago";
    }else if(diff < DateUtils.MILLIS_PER_DAY){
        result =  (int)(diff/DateUtils.MILLIS_PER_HOUR) + " hours ago";
    }else if(diff < DateUtils.MILLIS_PER_DAY * 2){
        result = new SimpleDateFormat("'yesterday at' hh:mm a").format(t1);
    }else if(diff < DateUtils.MILLIS_PER_DAY * 365){
        result = new SimpleDateFormat(" MMM d 'at' hh:mm a").format(t1);
    } else{
        result = new SimpleDateFormat("MMM d ''yy 'at' hh:mm a").format(t1);
    }
    result = result.replace("AM", "am").replace("PM", "pm");
    System.out.println(result);


}
于 2013-03-21T06:55:17.057 回答
-1

不要long转换为int,因为它会失去精度。全部改成intlong看看有什么不同。

希望这有帮助

于 2013-03-21T06:30:36.467 回答