开发者

identifying if a time given in milliseconds is within the current week

开发者 https://www.devze.com 2023-03-21 10:25 出处:网络
So I have a time in milliseconds (obtained previously from System.currentTimeMillis()) saved in a long va开发者_运维问答riable. What I need now is a way to determine wether that time is within the act

So I have a time in milliseconds (obtained previously from System.currentTimeMillis()) saved in a long va开发者_运维问答riable. 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?


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.


public static boolean withinThisRange(long milliseconds, Date startDate, Date endDate)
{
    Date date = new Date(milliseconds);
    return date.after(startDate) && date.before(endDate);
}


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);
}


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());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消