3

I am trying to calculate the time of 100 days from now using the following:

import java.util.Date;
public class Main
{
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println(new Date(System.currentTimeMillis() + 8640000 * 1000));
    }
}

It gives me back Jan 04 08:57:25 UTC 2021 which is not correct. How should I remedy this?

Adam
  • 257
  • 3
  • 12
  • 5
    Integer overflow. 8640000×1000 > 2³¹. Change `8640000` to `8640000L` so Java knows you’re doing math with 64-bit `long` values, which will not overflow. – VGR Jan 03 '21 at 19:13
  • 3
    Also consider `LocalDateTime.now().plus(Duration.ofDays(100))`. – trashgod Jan 03 '21 at 19:23
  • 1
    @trashgod Why `plus(Duration.ofDays(100))` when `plusDays(100)` is available? – Andreas Jan 03 '21 at 20:31
  • 2
    While in UTC 100 days equals the number of milliseconds you try to calculalte, in very many other time zones it often is not the case since a day may be for example 23 or 25 hours. – Ole V.V. Jan 03 '21 at 22:16
  • @Andreas: Good question; I started to go that way, but I wanted to emphasize the notion of `Duration`. – trashgod Jan 04 '21 at 13:31
  • Does this answer your question? [Android Days between two dates](https://stackoverflow.com/questions/23323792/android-days-between-two-dates). [This](https://stackoverflow.com/questions/49776292/why-is-this-not-casting-to-long)? – Ole V.V. Jan 05 '21 at 02:43

3 Answers3

5

Use the following:

System.out.println(new Date(System.currentTimeMillis() + (long)8640000 * 1000));

Or

System.out.println(new Date(System.currentTimeMillis() + 8640000L * 1000));

8640000 * 1000 is 8,640,000,000, which exceeds Integer.MAX_VALUE of 2,147,483,647. You can solve this issue by casting it to a long.

This gives the result:

Tue Apr 13 15:26:10 EDT 2021
Spectric
  • 30,714
  • 6
  • 20
  • 43
4

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

Using the modern API:

import java.time.LocalDate;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        // Change the ZoneId as per your requirement e.g. ZoneId.of("Europe/London")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());
        LocalDate after100Days = today.plusDays(100);
        System.out.println(after100Days);
    }
}

Output:

2021-04-13

Learn about the modern date-time API from Trail: Date Time.

Using the legacy API:

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        // Change the TimeZone as per your requirement e.g. TimeZone.getTimeZone("Europe/London")
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        calendar.add(Calendar.DAY_OF_YEAR, 100);
        Date after100Days = calendar.getTime();
        System.out.println(after100Days);
    }
}

Output:

Tue Apr 13 19:40:11 BST 2021
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
3

Try sometime like

LocalDateTime.now().plusDays(100).toString()

or

SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); // you could used any format
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 100); // Adding 100 days
String output = sdf.format(c.getTime());
System.out.println(output);
  • 1
    `c.setTime(new Date());` is redundant, since the `Calendar` object returned by [`getInstance()`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--) is already the current time: *"Gets a calendar using the default time zone and locale. The **Calendar returned is based on the current time** in the default time zone with the default FORMAT locale."* – Andreas Jan 03 '21 at 20:33
  • Yes you are right, Just added it incase you want to reset the date. I have edited it – Technical 101 Jan 04 '21 at 08:48