13

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?

p0tta
  • 1,461
  • 6
  • 28
  • 49

5 Answers5

22

try

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

it definitely works

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 5
    According to JavaDocs of System.currentTimeMillis() method: "Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds. See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC)." This has to OS specific. Outside theory, this is not always accurate 2 * 24 * 60 * 60 * 1000 – nabster Apr 20 '15 at 21:42
  • 1
    Interesting answer. And so not correct :-) try to use it to subtract a year or let say 360 days. You will see why. How much one letter can change the result :-) – Ondrej Havlicek Sep 20 '19 at 18:31
3

Use Calendar API like this to subtract 2 days from a Date object:

Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DATE, -2);
d.setTime( c.getTime().getTime() );

long millisec = d.getTime();
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Your code is alright , your variable d should be at offset of 48 hours from the current time on your server.

Make sure the server and your clients are running on the same time otherwise request your server admins to fix the time on your deployment machine.

You would also notice this difference if your client is opening a browser in e.g. Japan and your server is running in USA because of the standard time difference.

Avinash Singh
  • 3,421
  • 2
  • 20
  • 21
1

try this

   long diff = Math.abs(d1.getTime() - d2.getTime());
   long diffDays = diff / (2*24 * 60 * 60 * 1000);
PSR
  • 39,804
  • 41
  • 111
  • 151
1

Avoid the old java.util.Date and .Calendar classes as they are notoriously troublesome.

Use either Joda-Time or the new Java.time package built into Java 8. Search StackOverflow for hundreds of Questions and Answers on this.

Joda-Time offers methods for adding and subtracting hour, days, and more. The math is done in a smart way, handling Daylight Saving Time nonsense and other issues.

Quick example in Joda-Time 2.7 ( as this is really a duplicate Question, see others for more info ).

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( zone );
DateTime fortyEightHoursAgo = now.minusHours( 48 );
DateTime twoDaysAgo = now.minusDays( 2 );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154