英文:
How to get a DateTime/Calendar instance with the Time of next Monday 6:00am?
问题
如何获得下周特定时间(自Unix纪元以来的毫秒值)。当我的代码执行时,它需要获得下周一上午6:00的毫秒值。我不能传入任何静态值,它必须是动态的。
例如:
如果当前时间是 Tue 05/05/2020 21:30
,代码必须返回 Mon 11/05/2020 06:00
的毫秒值。
或者
如果当前时间是 Mon 04/05/2020 05:59
,代码必须返回 Mon 04/05/2020 06:00
的毫秒值。
我已经阅读了许多类似的问题,但它们没有给出明确的答案,或者只关注于星期几,并没有考虑特定的时间(在我这里是06:00)。我研究过使用 TemporalAdjusters
,但我不太愿意将它们包含在我的Android项目中,因为它们需要API 26(我的最低版本是21)。我也研究了JodaTime,但找不到一个适合将时间舍入到特定时间的函数。
在下面的代码中,我尝试实现了某种解决方案,但在DateTime是星期一但在06:00之后时遇到了问题。
Calendar date1 = Calendar.getInstance();
date1.setTimeInMillis(System.currentTimeMillis());
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
date1.add(Calendar.DATE, 1);
}
while (date1.get(Calendar.HOUR_OF_DAY) < 6){
date1.add(Calendar.HOUR,1);
}
英文:
How can I get the millisecond value (since Unix Epoch) of a specific time next week. When my code executes, it needs to get next Monday at 6:00 am in milliseconds. I cannot pass in any static values, it has to be dynamic.
For example:
If it's currently Tue 05/05/2020 21:30
the code must return the millisecond value of Mon 11/05/2020 06:00
Or
If it's currently Mon 04/05/2020 05:59
the code must return the millisecond value of Mon 04/05/2020 06:00
I've read alot of the similar questions but none of them give a definitive answer or only focus on the day of the week and do not factor the specific time (in my case 06:00). I've looked into using TemporalAdjusters
but I'm hesitant to include them in my android project as they require API 26 (my min is 21). I've looked at JodaTime but couldn't find a suitable function to round to a specific time.
In the code below, I attempted to implement some sort of solution but came across issues when the DateTime was on a Monday but after 06:00.
Calendar date1 = Calendar.getInstance();
date1.setTimeInMillis(System.currentTimeMillis());
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
date1.add(Calendar.DATE, 1);
}
while (date1.get(Calendar.HOUR_OF_DAY) < 6){
date1.add(Calendar.HOUR,1);
}
答案1
得分: 1
Joda Time非常适用于这种情况。例如,当前日期的毫秒数为DateTime.now().withTimeAtStartOfDay()
。本周的哪一天是DateTime.now().dayOfWeek()
(星期一=1,星期二=2,依此类推)。有一个plusDays()
方法可用于向未来移动。将这些与switch语句结合使用,以找出要添加的天数,然后您应该能够得出所需的解决方案。哦,在重新阅读您的问题时,将所需的毫秒数添加到一天的开始,以便从那时开始到达6:00,这应该很简单...抱歉我忽略了那个;-) 祝您好运!
英文:
Joda time is great for stuff like this. For example, the millisecond of the current day is DateTime.now().withTimeAtStartOfDay()
. Which day of the week is DateTime.now().dayOfWeek()
(Monday = 1, Tuesday = 2, etc.). There is a plusDays()
method to move to the future. Combine these with a switch statement to figure out the number of days to add and you should arrive at the desired solution. Oh, and re-reading your question, it should be straightforward to add the number of milliseconds necessary to get to 6:00 from the start of the day...sorry I missed that Good luck!
答案2
得分: 1
我认为这对你应该是可行的。(DateUtils:来自commons-lang3
库)
public Date getNextMonday() {
Calendar now = Calendar.getInstance();
while (now.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY || now.get(Calendar.HOUR_OF_DAY) > 6) {
now.add(Calendar.DATE, 1);
now.set(Calendar.HOUR, 0);
}
Date date = now.getTime();
date = DateUtils.truncate(date, Calendar.DATE);
date = DateUtils.addHours(date, 6);
return date;
}
英文:
I think this should work for you. (DateUtils: from commons-lang3
lib)
public Date getNextMonday() {
Calendar now = Calendar.getInstance();
while (now.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY || now.get(Calendar.HOUR_OF_DAY) > 6) {
now.add(Calendar.DATE, 1);
now.set(Calendar.HOUR, 0);
}
Date date = now.getTime();
date = DateUtils.truncate(date, Calendar.DATE);
date = DateUtils.addHours(date, 6);
return date;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论