我想我已经想出了一个解决方案。
获取数据后,将完成以下操作...
editPrefs.putLong("fetchTime", System.currentTimeMillis());
当应用程序启动并正常请求数据时,它现在会......
(注意: 页面每天在“9pm AEDT”更新 - (澳大利亚东部夏令时间),我假设它会更改为 AEST(澳大利亚东部标准时间) 夏令时结束时。“Australia/ACT”是经过这些区域的时区)
//create calendar date for update threshold based on last fetch time preference
Calendar updateThresh = new GregorianCalendar(TimeZone.getTimeZone("Australia/ACT"));
updateThresh.setTimeInMillis(prefs.getLong("fetchTime", 0));
//increment day if last fetch was >= 9pm of that day
if (updateThresh.get(Calendar.HOUR_OF_DAY) >= 21)
updateThresh.add(Calendar.DAY_OF_MONTH, 1);
//set time to 9:00:00pm
updateThresh.set(Calendar.HOUR_OF_DAY , 21);
updateThresh.set(Calendar.MINUTE , 0);
updateThresh.set(Calendar.SECOND , 0);
//check if current date is before or after
if (updateThresh.before(new GregorianCalendar(TimeZone.getTimeZone("Australia/ACT"))))
{
//update needed (fetch data)
}
else
{
//update not needed (show message)
}
这似乎工作正常。我已经使用设置为澳大利亚/ACT 的 DateFormat 对其进行了测试,它似乎可以工作......例如,制作另一个设置为获取时间的日历对象并使用:
DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("Australia/ACT"));
testTextField.setText("Last fetch in East AU time: " + df.format(fetchTime.getTimeInMillis()) +
"\nUpdate threshold in East AU time: " + df.format(updateThresh.getTimeInMillis()) );
即使我在手机上使用时区设置,这也向我显示始终正确的输出......我相当确定这个解决方案有效,所以我会将它标记为正确/接受 - 但如果有人注意到错误或有更好的建议,随意贡献!:)