Java 在分析后端上查找当前时间和上次更新时间之间的差异

Java 在分析后端上查找当前时间和上次更新时间之间的差异,java,date,time,parse-platform,simpledateformat,Java,Date,Time,Parse Platform,Simpledateformat,我正试图编写一个Java脚本,计算当前日期和存储在解析后端的上次更新时间之间的时差。有人能帮我找出代码中的错误吗?您可能认为这并不是很糟糕,但我已经观察了堆栈溢出数小时,但都没有结果 //Create a date formatter. SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'"); //Create a date object for today's date.

我正试图编写一个Java脚本,计算当前日期和存储在解析后端的上次更新时间之间的时差。有人能帮我找出代码中的错误吗?您可能认为这并不是很糟糕,但我已经观察了堆栈溢出数小时,但都没有结果

//Create a date formatter.
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
        Date currentDate=new Date();
//Create a string for the date from parse.
        String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
        String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
        Date parseDate = null;
//Initialize the date object for the current time.
        Date FormattedCurrentDate = null;
        try {
//Here, we convert the parseTime string into a date object and format it.
            parseDate = formatter.parse(parseTime);
//Here, we convert the currentTime string into a date object and format it.
            FormattedCurrentDate = formatter.parse(currentTime);
} 

        catch (Exception e) {
            e.printStackTrace();
        }           

   //Get the time difference from the current date versus the date on Parse.
        long difference = FormattedCurrentDate.getTime()-parseDate.getTime();

不应调用toString()将日期转换为字符串。为什么要将currentTime转换为字符串,然后再将其解析为日期?这毫无意义

//Create a date formatter.
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'LLL'Z'");
//Create a date object for today's date.
        Date currentDate=new Date();
//Create a string for the date from parse.
        String parseTime = singleClaim.getString("updatedAt");
//Create a string for the current date.
        String currentTime=currentDate.toString();
//Initialize the date object for the updatedAt time on Parse.
        Date parseDate = null;
        try {
//Here, we convert the parseTime string into a date object and format it.
            parseDate = formatter.parse(parseTime);
} 

        catch (Exception e) {
            e.printStackTrace();
        }           

   //Get the time difference from the current date versus the date on Parse.
        long difference = currentDate.getTime()-parseDate.getTime();

这两条线的结合可能会导致您的问题:

String currentTime=currentDate.toString();
// ...
FormattedCurrentDate = formatter.parse(currentTime);
currentTime
变量不包含格式设置程序可以使用的正确格式字符串

我也认为没有必要创建这样的字符串,您可以按如下方式执行:

long difference = currentDate.getTime() - parseDate.getTime();
currentTime = formatter.format(currentDate);

如果您坚持从日期到字符串来回往返,则必须按如下方式创建
currentTime
字符串:

long difference = currentDate.getTime() - parseDate.getTime();
currentTime = formatter.format(currentDate);

您能给出输出的例子吗?为什么要将currentDate转换为字符串并再次解析?您是否尝试过Joda Time或Java 8的Time API?我想确定解析时当前时间和上次更新时间之间的时间差是否小于24小时。这就是我们的目标。毫秒算法充满了问题,我们甚至不必从时区开始。使用适当的API来解决以下问题:you@Downvoter请解释!我不知道,但我想这应该是一个简单的评论。@Filburt不,这就是解决方案。不是comment@Jens我要恢复宇宙的平衡。你的回答是正确的,所以我不知道你为什么被否决。@RobbyCornelissen这就是stackoverflow的问题所在。你可以向下投票,没有人知道为什么。但是currentDate不需要根据格式化程序进行格式化,才能从中减去parseDate吗?@Aly_G在Java中a
Date
只是一个容器,用于表示自Unix纪元以来的毫秒数,它没有自己的内部格式。。