gpt4 book ai didi

java - Time getJulianDay() 不推荐使用哪些替代方法将日期转换为朱利安日期,反之亦然?

转载 作者:行者123 更新时间:2023-12-01 11:02:58 25 4
gpt4 key购买 nike

我想使用 GregorianCalendar 而不是 Time 类,Android 文档是如何建议的,但是如何管理儒略日以在我的 sqlLite 数据库中将日期存储为 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 将日期转换为字符串,然后将字符串转换为 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
}

关于java - Time getJulianDay() 不推荐使用哪些替代方法将日期转换为朱利安日期,反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33200970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com