1

I know how to get the milliseconds for a date after 1.1.1970. But how can I get the milliseconds for a specific date that occurs before that date? I need that in an android app. Thanks!

JA3
  • 45
  • 6
darkwall
  • 53
  • 6
  • If you're using specific dates, using ms since an arbitrary epoch probably isn't the best idea. Why not just use a Date library? – Carcigenicate Feb 08 '17 at 18:44
  • 2
    I don't know how you're getting the milliseconds after the date, but most libraries should work fine with negative millis-since-epoch. -86400000 represents 24 hours before epoch, for instance. – yshavit Feb 08 '17 at 18:48
  • 2
    have you tried something? – lelloman Feb 08 '17 at 18:50
  • whatever you tried put it in the question please, there's an edit button – lelloman Feb 09 '17 at 12:50
  • @lelloman No problem anymore. I don't know that went wrong, but now it works, also with the negative number. Thanks again for all the good answers! – darkwall Feb 09 '17 at 13:01

4 Answers4

5

All numbers are signed in java, so all numbers can be negative. Therefore you can have a date with a negative amount of milliseconds. Therfore dates with a timestamp which is less than 0 is definitely possible.

You can know the earliest possible date by performing:

new Date(Long.MIN_VALUE)

You'll find that this returns a date which is more than 292 million years ago. This is even before the jurassic era. So there really isn't a need to worry about dates before 1970 :)

Cristan
  • 12,083
  • 7
  • 65
  • 69
  • The `Date` class is now legacy, supplanted by the java.time classes in general and the `java.time.Instant` class in particular. See [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) for an adaptation for Android of a back-port for Java 6 & 7. – Basil Bourque Feb 08 '17 at 20:21
  • Thanks for all comments. The calculation with the negative numbers work well now. Don't know, why it didn't before... – darkwall Feb 09 '17 at 13:02
1

Using java.time.Instant, get your date in the Instant and you can use the getEpochSecond() method to return the number of seconds before the Epoch and just convert from that.

https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html for more details about the Instant class.

JA3
  • 45
  • 6
1

tl;dr

LocalDate.of( 1953 , 1 , 17 )
         .atStartOfDay( ZoneId.of( "America/Montreal" ) )
         .toInstant()
         .toEpochMilli()

…yields a negative number:

-535057200000

Details

As the other Answers said:

(a) In representing moments as a count-from-epoch-reference-date, moments before the epoch use negative numbers, moments after the epoch use positive numbers.

(b) You should be using java.time classes rather than the troublesome old date-time classes that are now legacy. See below for info on back-port to Android.

Using java.time

Use LocalDate for a date-only value, with no time-of-day nor time zone.

LocalDate ld = LocalDate.of( 1953 , 1 , 17 );

To convert to a moment, let's get the first moment of the day on that date. That means specifying a time zone, as the date varies around the globe by time zone. Apply a ZoneId to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ld.atStartOfDay( z );

Extract an Instant, a moment on the timeline in UTC.

Instant instant = zdt.toInstant();

From the Instant, extract the number of milliseconds since the epoch of first moment of 1970 in UTC (1970-01-01T00:00:00Z). Beware of data-loss, as the java.time classes can hold a moment with a resolution of up to nanoseconds; asking for milliseconds means lopping off any nanoseconds if present.

long epochMillis = instant.toEpochMilli();

See this code run line in IdeOne.com.

ld.toString(): 1953-01-17

zdt.toString(): 1953-01-17T00:00-05:00[America/Montreal]

instant.toString(): 1953-01-17T05:00:00Z

epochMillis: -535057200000


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • The class `java.util.Date` might be legacy for you but not for Google who still considers this old API as standard on Android. There are good reasons to regret this decision of Google but we have at least to respect it. – Meno Hochschild Feb 09 '17 at 08:12
  • Thanks for your answer @Basil Bourque. I will have a look at it. – darkwall Feb 09 '17 at 12:47
0

The problem was something I cannot reproduce. Before 1.1.1970 the values are negative. Now, my program calculates right, but I think, I didn't change something in the code. Maybe it was a cached version, which caused the problems. Thanks for all answers!

darkwall
  • 53
  • 6