ohGosh
ohGosh

Reputation: 559

Date Time in Java - How to find how many milliseconds from a certain date to 1970?

I have a quick question, I have values of day (int), month (String), year (int), hour (int), minute (int), and second (int). What I want to do it see how many milliseconds there are between the date I have and Jan 1 1970.

So just as an example, how could I tell how many milliseconds there were from Jan 1970 to June 1 2011, 3:12:59 pm?

I am pretty sure this will be simple but I am really exhausted. I think I could use

dateTime.getTimeInMillis());

but I am not exactly sure. I am not the best with Java dates so any help would be awesome!

Thanks!!!!

Upvotes: 2

Views: 1170

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 340230

tl;dr

Duration
.between( 
    Instant.EPOCH , 
    ZonedDateTime.of( y , m , d , h , m , s , nanos , ZoneId.of( "America/Edmonton" ) )
)
.toMillis()

Time Zone

I have values of day (int), month (String), year (int), hour (int), minute (int), and second (int).

You also need the context of a time zone or offset-from-UTC. Without that context, we have no way to know if your date and time were meant for Tokyo, Toulouse, or Toledo.

java.time

In Java 8+, use the modern java.time classes that supplanted the terribly-flawed legacy date-time classes. Never use Date, Calendar, SimpleDateFormat, etc. Use only java.time classes.

ZonedDateTime

Given your inputs, plus a time zone, determine a moment, a point on the timeline.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdt = ZonedDateTime.of( y , m , d , h , m , s , nanos , z ) ;

UTC

You said:

how many milliseconds there are between the date I have and Jan 1 1970.

You neglected to mention a time zone or offset for that beginning of 1970. I presume you meant UTC, that is, an offset from UTC of zero hours-minutes-seconds.

Instant.EPOCH

We have a constant defined for that moment: Instant.EPOCH which represents the first moment of 1970 as seen in UTC, 1970-01-01T00:00:00Z (where Z means offset of zero). Internally, this constant object holds a count of zero whole seconds, and zero nanoseconds, since that epoch reference.

Duration

To represent a span of time unattached to the timeline on a scale of hours-minutes-seconds, use Duration.

The arguments to Duration must be of the same type. So to compare to Instant.EPOCH, we need another Instant object. We can extract one from our ZonedDateTime, effectively adjusting from that time zone to UTC. Same moment, different wall-clock/calendar.

Duration d = Duration.between( Instant.EPOCH , zdt.toInstant() ) ;

You want a count of milliseconds. Be aware that your ask may involve data loss, as the java.time classes such as Instant & ZonedDateTime resolve to much finer nanoseconds. Asking for milliseconds may mean dropping any microseconds and nanoseconds.

long millis = d.toMillis() ;

Upvotes: 1

havexz
havexz

Reputation: 9590

If you have two Date objects already then just do this:

date1.getTimeInMillis() - date2.getTimeInMillis();

If you only have individual values, then create the date like:

Calendar cal = new GregorianCalendar(year,month,dayOfMonth,hourOfDay,minute,second);
cal.getTimeInMillis();

Now you can use the above formula.

Ref:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/GregorianCalendar.html http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html

Upvotes: 5

ziesemer
ziesemer

Reputation: 28707

Yes - System.currentTimeMillis() - dateTime.getTimeInMillis(), where dateTime is a a created date instance (possibly obtained from a Calendar) with your year/month/date/etc.

Upvotes: 4

Related Questions