[英]Time getJulianDay() deprecated which alternatives to convert date to julian and vice versa?
我想使用GregorianCalendar而不是Time类,android文档如何提出建议,但是如何管理julian day在我的sqlLite db中将日期存储为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);
}
当我连续查询时,如何通过儒略日获得正常日期?
如果只需要将日期转换为整数以保存在数据库中,则可以使用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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.