从当前日期减去两天,在历史毫秒java [英] subtracting two days from current date in epoch milliseconds java

查看:322
本文介绍了从当前日期减去两天,在历史毫秒java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力做一些非常简单的事情。我试图从当天减去2天。我从UI中获取了几个小时。所以在这个例子中,我从用户界面48小时。我在做以下事情,我不知道我在这里做错了什么。我认为这样做的结果是从时间上减去几分钟。

  long timeInEpoch =(currentMillis()/ 1000  - (48 * 60 * 60)); // 48来自UI 

public long currentMillis(){
return new Date()。getTime();
}

d = new Date(timeInEpoch * 1000);

我也尝试过

  d1 = new Date(timeInEpoch); 

没有什么可以工作。我在这里做错什么?

解决方案

尝试

  long millis = System.currentTimeMillis() -  2 * 24 * 60 * 60 * 1000; 
Date date = new Date(millis);

它绝对有效


I am trying to do something really simple. I am trying to subtract 2 days from the current day. I get the number of hours from the UI. So in this example, I get 48 hours from the UI. I am doing the following and I don't know what i'm doing wrong here. I think the result of this is it only subtracts a few minutes from the time.

long timeInEpoch = (currentMillis()/1000 - (48 * 60 * 60)); //48 comes from UI

public long currentMillis(){
    return new Date().getTime();
}

d = new Date(timeInEpoch * 1000);

I also tried

d1 = new Date(timeInEpoch);

Nothing seems to work. What am I doing wrong here?

解决方案

try

    long millis = System.currentTimeMillis() - 2 * 24 * 60 * 60 * 1000; 
    Date date = new Date(millis);

it definitely works

这篇关于从当前日期减去两天,在历史毫秒java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆