确定以毫秒为单位的时间是否在本周内

发布于 2024-11-24 19:16:17 字数 119 浏览 4 评论 0原文

所以我有一个以毫秒为单位的时间(之前从 System.currentTimeMillis() 获得)保存在一个 long 变量中。我现在需要的是一种方法来确定该时间是否在当前一周内。我需要获得布尔值的结果。最好的方法是什么?

So I have a time in milliseconds (obtained previously from System.currentTimeMillis()) saved in a long variable. What I need now is a way to determine wether that time is within the actual current week or not. I need to obtain the result as a boolean. What would be the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

記柔刀 2024-12-01 19:16:17

使用 Joda

new DateTime().weekOfWeekyear().toInterval().contains(millis);

之类的操作来检查它是否正确选择了整周

System.out.println(new DateMidnight(2011, 12, 31).weekOfWeekyear().toInterval());

您可以通过执行诸如Which prints 2011 -12-26T00:00:00.000/2012-01-02T00:00:00.000。向您表明它已正确找到跨越年份边界的星期。请注意,Joda 默认情况下将星期日视为一周的第一天。如果您需要的话,不确定是否可以将其更改为星期一。

由于 Joda 对象是不可变的(只有少数情况不是),它们的生命周期通常非常短,并且上述代码中几乎没有性能开销,因为默认 GC 在处理短生命周期对象时非常有效。但你确实获得了巨大的可读性奖励。

With Joda

new DateTime().weekOfWeekyear().toInterval().contains(millis);

You can check it picks out the whole week properly by doing something like

System.out.println(new DateMidnight(2011, 12, 31).weekOfWeekyear().toInterval());

Which prints 2011-12-26T00:00:00.000/2012-01-02T00:00:00.000. Showing you that it has correctly found the week that crosses the year boundary. Note that Joda, by default, considers Sunday to be the first day of a week. Not sure if you can change this to Monday if that's what you need.

Since Joda objects are immutable (there a only a few cases which aren't) they are generally very short lived and there is little to no performance overhead in the above code as the default GC is very efficient at dealing with short lived objects. But what you do gain is a massive readability bonus.

歌枕肩 2024-12-01 19:16:17
public static boolean withinThisRange(long milliseconds, Date startDate, Date endDate)
{
    Date date = new Date(milliseconds);
    return date.after(startDate) && date.before(endDate);
}
public static boolean withinThisRange(long milliseconds, Date startDate, Date endDate)
{
    Date date = new Date(milliseconds);
    return date.after(startDate) && date.before(endDate);
}
高冷爸爸 2024-12-01 19:16:17

如果您必须使用 JavaSE API,我强烈建议您这样做。对于未来的维护者来说,这更容易,他们可能不是真正的 java 人员,并且理解 WEEK_OF_YEAR 字段的噩梦。

public static boolean isCurrentWeek(long time) {
    Calendar now = Calendar.getInstance();
    Calendar work = Calendar.getInstance();
    work.clear();
    work.set(Calendar.YEAR, now.get(Calendar.YEAR));
    work.set(Calendar.MONTH, now.get(Calendar.MONTH));
    work.set(Calendar.DATE, now.get(Calendar.DATE));
    work.getTime(); //force computation of WEEK_OF_MONTH
    work.set(Calendar.DAY_OF_WEEK, work.getFirstDayOfWeek());
    final Date start = work.getTime();
    work.add(Calendar.DAY_OF_YEAR, 7);
    final Date end = work.getTime();
    Date timeStamp = new Date(time);
    return timeStamp.before(end) && !timeStamp.before(start);
}

If you must use the JavaSE APIs, I would strongly suggest doing it this way. It's easier for future maintainers who may not really be java people and understand the nightmares of the WEEK_OF_YEAR field.

public static boolean isCurrentWeek(long time) {
    Calendar now = Calendar.getInstance();
    Calendar work = Calendar.getInstance();
    work.clear();
    work.set(Calendar.YEAR, now.get(Calendar.YEAR));
    work.set(Calendar.MONTH, now.get(Calendar.MONTH));
    work.set(Calendar.DATE, now.get(Calendar.DATE));
    work.getTime(); //force computation of WEEK_OF_MONTH
    work.set(Calendar.DAY_OF_WEEK, work.getFirstDayOfWeek());
    final Date start = work.getTime();
    work.add(Calendar.DAY_OF_YEAR, 7);
    final Date end = work.getTime();
    Date timeStamp = new Date(time);
    return timeStamp.before(end) && !timeStamp.before(start);
}
樱娆 2024-12-01 19:16:17

获取本周开始的时间(以毫秒为单位)和下周开始的时间(以毫秒为单位)。然后查看输入时间是否在这两个值之间:

public boolean isInCurrentWeek(long time) {
    Calendar cal1 = GregorianCalendar.getInstance();
    Calendar cal2;
    cal1.set(Calendar.DAY_OF_WEEK, 0);
    cal1.set(Calendar.HOUR_OF_DAY, 0);
    cal1.set(Calendar.MINUTE, 0);
    cal1.set(Calendar.SECOND, 0);
    cal1.set(Calendar.MILLISECOND, 0);
    cal2 = (GregorianCalendar) cal1.clone();
    cal2.add(Calendar.DATE, 7);
    return (time >= cal1.getTimeInMillis() && time < cal2.getTimeInMillis());
}

Get the time in milliseconds of the start of the current week, and the time in milliseconds of the start of the following week. Then see if the input time is between these two values:

public boolean isInCurrentWeek(long time) {
    Calendar cal1 = GregorianCalendar.getInstance();
    Calendar cal2;
    cal1.set(Calendar.DAY_OF_WEEK, 0);
    cal1.set(Calendar.HOUR_OF_DAY, 0);
    cal1.set(Calendar.MINUTE, 0);
    cal1.set(Calendar.SECOND, 0);
    cal1.set(Calendar.MILLISECOND, 0);
    cal2 = (GregorianCalendar) cal1.clone();
    cal2.add(Calendar.DATE, 7);
    return (time >= cal1.getTimeInMillis() && time < cal2.getTimeInMillis());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文