Google SpreadSheet的Java时代日期 [英] Java Epoch Date for Google SpreadSheet

查看:77
本文介绍了Google SpreadSheet的Java时代日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Google电子表格中添加日期。要添加日期,我们已将日期转换为时代。

I tried to add date on Google spreadsheet. To add date we have convert date in epoch.

对于电子表格时代

Google表格使用一种时代形式电子表格中常用的日期。值的整数部分(小数点左边)计算自1899年12月30日以来的天数。小数部分(小数点右边)将时间计算为一日的分数。例如,1900年1月1日中午为2.5,因为1899年12月30日之后的两天是2,而0.5是中午为半天。 1900年2月1日下午3点将是33.625。

Google Sheets uses a form of epoch date that is commonly used in spreadsheets. The whole number portion of the value (left of the decimal) counts the days since December 30th 1899. The fractional portion (right of the decimal) counts the time as a fraction of one day. For example, January 1st 1900 at noon would be 2.5, 2 because it's two days after December 30th, 1899, and .5 because noon is half a day. February 1st 1900 at 3pm would be 33.625.

我们使用Joda时间API进行计算(JDK版本1.7)。

We uses Joda time API for calculation.(Our JDK version 1.7).

对于整数,我们使用下面的代码。

For Whole number we uses below code.

public static double getEpochDate(Date inputDate)
{

    MutableDateTime epoch = new MutableDateTime();
    epoch.setTime(0, 0, 0, 0);
    epoch.setDate(1899,12,30); //Set to Epoch time
    System.out.println(epoch);
    DateTime now = new DateTime(inputDate);
    Days days = Days.daysBetween(epoch, now);


    return Double.valueOf(days.getDays());
}

我们面临的问题是找到时代编号的派系部分

We are facing issue to find faction part of epoch number

推荐答案

一种选择是采用自Unix纪元以来的毫秒数值(例如,使用 Date.getTime()),将其偏移为自Sheets时代以来的毫秒数,然后将浮点数除以24小时。像这样:

One option would be to take the "milliseconds since the Unix epoch" value (e.g. with Date.getTime()), offset that to get "milliseconds since the Sheets epoch" and then perform a floating point division by 24 hours. Something like this:

// Precomputed difference between the Unix epoch and the Sheets epoch.
private static final long SHEETS_EPOCH_DIFFERENCE = -2209161600000L;

public static double getEpochDate(Date date)
{
    long millisSinceUnixEpoch = date.getTime();
    long millisSinceSheetsEpoch = millisSinceUnixEpoch - SHEETS_EPOCH_DIFFERENCE;
    return millisSinceSheetsEpoch / (double) TimeUnit.DAYS.toMillis(1);
}

无需Joda Time参与此案。 (当您 do 想要使用Joda Time时,强烈建议您避免使用 Mutable * 类型。仅使用 DateTime 当确实涉及到时区时...)

No need for Joda Time to get involved in this case. (When you do want to use Joda Time, I'd strongly advise avoiding the Mutable* types. And only use DateTime when there's genuinely a time zone involved...)

这篇关于Google SpreadSheet的Java时代日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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