OLEDate Java implementation

I need a good Java OLEDate implementation, but this one does not work. Are there any known good open source implementations (like in apache commons)? If not, where do I read about it to write my own implementation?

+3
source share
3 answers

This Old New Thing Blog Blog seems like a decent treatise on the topic:

The OLE Automation Date format is a floating point value, counting the days from midnight December 30, 1899. Hours and minutes are represented as fractional days.

If you have access to Visual Studio and the MFC source COleDateTime, you can override this in Java.

+2

:

public static Date getDateFromCOMDate(float comtime) {
    String floatstr = String.valueOf(comtime);
    String [] ss = floatstr.split("\\.");
    long nulltime = -2209183200000L;
    long dayms = 86400000;
    int days = Integer.valueOf(ss[0]);
    float prop = comtime - days;
    long cdayms = Math.round(dayms * prop);
    long time = nulltime + days*dayms + cdayms;
    Date d = new Date(time);
    return d;
}
+3

, . 1.0 -1.25. OLE, MSDN, . : https://msdn.microsoft.com/en-us/library/system.datetime.tooadate(v=vs.110).aspx

MSDN. BigDecimal Joda LocalDateTime. BigDecimal , float double, .

class COMDateToRegularDateConverter {
    private static final LocalDateTime ZERO_COM_TIME = new LocalDateTime(1899, 12, 30, 0, 0);
    private static final BigDecimal MILLIS_PER_DAY = new BigDecimal(86400000);

    LocalDateTime toLocalDateTime(BigDecimal comTime) {
        BigDecimal daysAfterZero = comTime.setScale(0, RoundingMode.DOWN);
        BigDecimal fraction = comTime.subtract(daysAfterZero).abs(); //fraction always represents the time of that day
        BigDecimal fractionMillisAfterZero = fraction.multiply(MILLIS_PER_DAY).setScale(0, RoundingMode.HALF_DOWN);

        return ZERO_COM_TIME.plusDays(daysAfterZero.intValue()).plusMillis(fractionMillisAfterZero.intValue());
    }
}
+2

Source: https://habr.com/ru/post/1740300/


All Articles