Java LocalDateTime Format toDoubleExcelFormat(LocalDateTime date, boolean date1904)

Here you can find the source of toDoubleExcelFormat(LocalDateTime date, boolean date1904)

Description

to Double Excel Format

License

Open Source License

Declaration

public static String toDoubleExcelFormat(LocalDateTime date, boolean date1904) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.time.LocalDateTime;

import java.time.temporal.ChronoUnit;

public class Main {
    public static String toDoubleExcelFormat(LocalDateTime date, boolean date1904) {
        if (date == null)
            return "";

        LocalDateTime from;/*from   www  .j a v  a  2  s.c  o  m*/

        // Excel has 2 date systems:
        // - https://support.microsoft.com/pt-br/help/214330/differences-between-the-1900-and-the-1904-date-system-in-excel
        if (date1904) {
            from = LocalDateTime.of(1904, 1, 1, 0, 0, 0);
        } else {
            from = LocalDateTime.of(1900, 1, 1, 0, 0, 0);
            // http://polymathprogrammer.com/2009/10/26/the-leap-year-1900-bug-in-excel/
            if (date.isBefore(LocalDateTime.of(1900, 3, 1, 0, 0, 0))) {
                from = from.minusDays(1);
            } else {
                from = from.minusDays(2);
            }
        }

        long millis = from.until(date, ChronoUnit.MILLIS);

        // excel hours are represented as a fraction of a day
        double dayMillis = 24.0 * 60.0 * 60.0 * 1000.0;

        return Double.toString(millis / dayMillis);
    }
}

Related

  1. formatShortDate(LocalDateTime dateTime)
  2. formattedWeek(LocalDateTime localDateTime)
  3. formatTime(LocalDateTime dateTime)
  4. formatTime(LocalDateTime time)
  5. localDateTime2Str(LocalDateTime localDateTime, String dateTimeFormatter)
  6. toLocalDateTime(String date, DateTimeFormatter formatter)
  7. toString(LocalDateTime ldt, DateTimeFormatter formatter)