时间getJulianDay()已弃用哪些替代方法将日期转换为julian,反之亦然? [英] Time getJulianDay() deprecated which alternatives to convert date to julian and vice versa?

查看:392
本文介绍了时间getJulianDay()已弃用哪些替代方法将日期转换为julian,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用GregorianCalendar而不是Time类,android文档如何建议,但是如何管理儒略日在sqlLite数据库中将日期存储为int类型呢?

I want to use GregorianCalendar instead of Time class, how android documentation suggest, but how manage julian day for store a date as a int in my sqlLite db?

如何转换此代码?

   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);
}

当我连续查询时,如何通过儒略日获得正常日期?

And when i query a row how can get the normal date by julian day?

推荐答案

如果只需要将日期转换为整数以保存在数据库中,则可以使用SimpleDateFormatter将Date转换为字符串,然后将字符串转换为int.像这样:

If you simply need to transform a date into an integer to save in a database, you can transform a Date into a String with a SimpleDateFormatter and then cast the string to an int. Something like:

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

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

That would create an integer you can easily save and retrieve in a database.

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

You should use a code similar to:

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
}

这篇关于时间getJulianDay()已弃用哪些替代方法将日期转换为julian,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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