Time getJulianDay() deprecated which alternatives to convert date to julian and vice versa?

最后都变了- 提交于 2020-01-07 03:07:18

问题


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?

How can i convert this code?

   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?


回答1:


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
}


来源:https://stackoverflow.com/questions/33200970/time-getjulianday-deprecated-which-alternatives-to-convert-date-to-julian-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!