javadatetimecalendar

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!!!!


Solution

  • 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