如何将Java Date转换为OADate或反之亦然? [英] How to convert Java Date to OADate or vice versa?

查看:467
本文介绍了如何将Java Date转换为OADate或反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Java Date转换为Microsoft OLE Automation-OADate类型,或者想将OADa​​te转换为Java Date. OADate for Java的公式是什么? 实际上,我已经在stackoverflow中进行了搜索,但找不到答案,我得到了答案,并想在这个社区中分享它.

I want to convert Java Date to Microsoft OLE Automation - OADate type or want to convert OADate to Java Date. What is the formula of the OADate for Java? Actually I have searched in the stackoverflow and couldnt find the answer, I got the answer and want to share it in this community.

例如: 43013.7659837963 等于 EET 2017年10月5日星期四18:23:01

推荐答案

Microsoft的Java OLE Automation Date Converter

Microsoft's OLE Automation Date Converter for Java

    /**
    * Convert Date to Microsoft OLE Automation - OADate type
    * @param date
    * @return
    * @throws ParseException
    */
    public static String convertToOADate(Date date) throws ParseException {
    double oaDate;
    SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
    Date baseDate = myFormat.parse("30 12 1899");
    Long days = TimeUnit.DAYS.convert(date.getTime() - baseDate.getTime(), TimeUnit.MILLISECONDS);

    oaDate = (double) days + ((double) date.getHours() / 24) + ((double) date.getMinutes() / (60 * 24)) + ((double)                     date.getSeconds() / (60 * 24 * 60));
    return String.valueOf(oaDate);
    }

如何将OADa​​te转换为Java日期:

/**
 * Convert Microsoft un OLE Automation - OADate to Java Date.
 * @param date
 * @return
 * @throws ParseException
 */
public static Date convertFromOADate(double d) throws ParseException {
    double  mantissa = d - (long) d;
    double hour = mantissa*24;
    double min =(hour - (long)hour) * 60;
    double sec=(min- (long)min) * 60;


    SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
    Date baseDate = myFormat.parse("30 12 1899");
    Calendar c = Calendar.getInstance();
    c.setTime(baseDate);
    c.add(Calendar.DATE,(int)d);
    c.add(Calendar.HOUR,(int)hour);
    c.add(Calendar.MINUTE,(int)min);
    c.add(Calendar.SECOND,(int)sec);

    return c.getTime();
}

这篇关于如何将Java Date转换为OADate或反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆