0

I want to convert a date to a long value (that is the milliseconds)

I have a date like

2/11/2014

I want to calculate the date in long (manual)

What I've tried

(2014 - 1970 ) * 31449600000 + 11 * 2592000000 + 2 * 604800000

This equals 1413504000000.

But http://www.fileformat.info/tip/java/date2millis.htm tells me that 1413504000000 is

Date (America/New_York) Thursday, October 16, 2014 8:00:00 PM EDT
Date (GMT)  Friday, October 17, 2014 12:00:00 AM GMT
Date (short/short format)   10/16/14 8:00 PM

Where I'm wrong?

Again, I want to do this manually, not using java code.

msrd0
  • 7,816
  • 9
  • 47
  • 82
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • 2
    You are not considering leap years. – Keppil Nov 05 '14 at 14:54
  • @Keppil Good point. Do you have any ideea how can I change my 'formula' ? – Boldijar Paul Nov 05 '14 at 14:56
  • 1
    And while we're on the subject of leap years, you also can't assume every 4th year is a leap year. 2000 is a leap year, but 2100, 2200, and 2300 are not, even though they are %4. – Compass Nov 05 '14 at 14:57
  • 2
    Leap year, That is what you didnt consider! Also some month are 30 days some arent – Lrrr Nov 05 '14 at 14:57
  • You can loop through the years and check. All years divisible by 4 are leap years, except the ones divisible by 100, but including the ones divisible by 400. So, every 4 years in your range should be a leap year unless you are using dates from later than 2099. – Keppil Nov 05 '14 at 14:57
  • Even if you don't want to use java code, you can look into it: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Date.java#Date.%3Cinit%3E%28int%2Cint%2Cint%2Cint%2Cint%2Cint%29 – msrd0 Nov 05 '14 at 14:58
  • Some cases are even weirder than leap years: http://stackoverflow.com/a/11221765/2136410 and http://stackoverflow.com/a/6841479/2136410 – Fabinout Nov 05 '14 at 15:02
  • The date can be wrong for maximum 1-5 days, but not more.. – Boldijar Paul Nov 05 '14 at 15:08
  • Why do you want to do it manually? – Sizik Nov 05 '14 at 15:18
  • @Sizik Well. I'm using Odata, and I have the birthdate of a field in DateTime, and I need to calculate the age of the field in the URL .. there I can only use + * - / % ... , no 'if', no 'for' etc..Maybe I'll change the way I'm trying to do this right now.. – Boldijar Paul Nov 05 '14 at 15:20

3 Answers3

2

Do not re-invent the wheel. Time/date calculations are notoriously difficult, even standard java library does not get it right. Use JodaTime:

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class JodaTimeSample {

  public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
    DateTime date = DateTime.parse("2/11/2014", formatter);
    System.out.println("Date: " + date.toString());
    System.out.println("Millis: " + date.getMillis());
  }
}
LeffeBrune
  • 3,441
  • 1
  • 23
  • 36
0

If you are sure you could do it manually (huh, why? - looks like homework), open JodaTime source code and copy it. You won't invent it better. Or even better open, read and then try to write it in your editor.

1ac0
  • 2,875
  • 3
  • 33
  • 47
0

why you do manually to convert date into long or long into date, write a simple java code which give you correct results after convention, I am using the simple programme and convert it as per my user

public class test {

    public static void main(String[] args) {

            Date dt=new Date(Long.valueOf(1390973400983L));
            System.out.println(dt.toString());

            Calendar cal=Calendar.getInstance();
            cal.set(2014, Calendar.JANUARY, 29, 11, 00, 0);

            System.out.println(cal.getTimeInMillis());
            System.out.println();

    }

}

for more information visit here

Ankit jain
  • 4,198
  • 4
  • 19
  • 24