时间getJulianDay()不赞成将日期转换为朱利安的替代方案,反之亦然?

时间:2015-10-18 17:26:49

标签: java android deprecated julian-date

我想使用GregorianCalendar而不是Time类,android文档如何建议,但是如何在我的sqlLite数据库中管理julian day作为int的日期?

我如何转换此代码?

   public static long normalizeDate(long startDate) {
    // normalize the start date to the beginning of the (UTC) day
    Time time = new Time();
    time.set(startDate);
    int julianDay = Time.getJulianDay(startDate, time.gmtoff);
    return time.setJulianDay(julianDay);
}

当我查询一行时如何在朱利安日获得正常日期?

1 个答案:

答案 0 :(得分:1)

如果您只需要将日期转换为整数以保存在数据库中,则可以使用SimpleDateFormatter将Date转换为String,然后将该字符串转换为int。类似的东西:

Date date = new Date(startDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dateString = formatter.format(date);
int dateInt = Integer.parseInt(dateString);

这将创建一个可以在数据库中轻松保存和检索的整数。

您应该使用类似于以下的代码:

public static long normalizeDate(long startDate) {
    // normalize the start date to the beginning of the (UTC) day
    GregorianCalendar date = (GregorianCalendar)     GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
    date.setTime(new Date(startDate));
    date.set(Calendar.HOUR_OF_DAY, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0)

    //transform your calendar to a long in the way you prefer
}