问题
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