subtracting two days from current date in epoch milliseconds java

25,665

Solution 1

try

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

it definitely works

Solution 2

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();

Solution 3

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.

Solution 4

try this

   long diff = Math.abs(d1.getTime() - d2.getTime());
   long diffDays = diff / (2*24 * 60 * 60 * 1000);

Solution 5

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 );
Share:
25,665
p0tta
Author by

p0tta

Mornings are for coffee and contemplation.

Updated on July 05, 2022

Comments

  • p0tta
    p0tta almost 2 years

    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?

  • nabster
    nabster about 9 years
    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
  • Ondrej Havlicek
    Ondrej Havlicek almost 5 years
    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 :-)
  • anubhava
    anubhava about 4 years
    @0tta: Did this answer work for you?