-4

I have an integer that when I put on Google Sheets, it represents the date 14/10/1911 (integer value is 4305).

I need to do this same conversion in my code using Java. So I'll have an array of integer that goes down by a factor of 1 (like 4305, 4304, 4303...) and I need to convert these integers into date (14/10/1911, 13/10/1911, 12/10/1911...) using the same method that excel/sheets uses.

Any ideas?

Thanks

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Lucas Heise
  • 139
  • 3
  • 12
  • 2
    How you can achieve `4305` from `14/10/1911`? – Boken Mar 09 '19 at 13:13
  • Possible duplicate of [Converting Number representation of Date in excel to Date in java](https://stackoverflow.com/questions/19028192/converting-number-representation-of-date-in-excel-to-date-in-java) – Ole V.V. Mar 09 '19 at 13:52

1 Answers1

1
    LocalDate msBaseDate = LocalDate.of(1899, Month.DECEMBER, 30);
    int[] integers = { 4305, 4304, 4303 };
    for (int integerValue : integers) {
        LocalDate date = msBaseDate.plusDays(integerValue);
        System.out.println(date);
    }

Output from this snippet is:

1911-10-14
1911-10-13
1911-10-12

Excel and other Microsoft products and possibly other software too use December 30, 1899, as a base date and represent dates as a count of days since that date. So just add the number to that date. I am using LocalDate from java.time, the modern Java date and time API.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161