4 hours in currentTimeMillis()

android, java

Solution

miliseconds are 1/1000 of a second. So 4 hours would be 4 * 60 * 60 * 1000 = 14,400,000

For cache invalidation this is probably fine. That said, date math is often dangerous. When dealing with larger units of time than milliseconds one can easily get tripped up during daylight savings transitions, leap seconds and all the other stuff that Calendar is meant to take care of. In some cases that rare imprecision is acceptable, and in others it's not. Be careful when doing date math.

For determining human consumable times in larger units of time such as +1 days, use Calendar.roll().

Problem

I have simple question, I have the following function and there is argument on it that called `cacheTime`, How can I set it to 4 hours, should I set it to `4 * 3600000`? ``` public static File getCache(String name, Context c, int cacheTime) { if (cacheTime <= 0) return null; File cache = new File(c.getCacheDir(), name); long now = System.currentTimeMillis(); if (cache.exists() && (now - cache.lastModified() < cacheTime)) return cache; return null; } ```

Original source