比较两个日期是否在android中的同一周内

时间:2012-03-09 20:02:02

标签: java android date

我有两个约会。他们来自......

Calendar c=Calendar.getInstance();
year=c.get(c.YEAR);
month=c.get(c.MONTH);
month++;
date=c.get(c.DATE);

和其他日期分为date2,month2

现在我想知道它们是否都在同一周。

可以通过大量的计算和逻辑。当第一个日期假设为3月3日,第2个日期为28Feb时,会出现问题。它们都在同一周,但难以比较/检查。所以我想知道是否有任何内置功能或任何方式来轻松比较它们。请帮助..........

8 个答案:

答案 0 :(得分:13)

使用类似的东西:

Calendar c = Calendar.getInstance();
Integer year1 = c.get(c.YEAR);
Integer week1 = c.get(c.WEEK_OF_YEAR);

Calendar c = Calendar.getInstance();
c.setTimeInMillis(/*Second date in millis here*/);
Integer year2 = c.get(c.YEAR);
Integer week2 = c.get(c.WEEK_OF_YEAR);

if(year1 == year2) {
    if(week1 == week2) {
         //Do what you want here
    }
}

这应该这样做:D

答案 1 :(得分:5)

您可以使用c.get(Calendar.WEEK_OF_YEAR)获取日期的周数,并比较两个日期的结果。

不建议通过实例变量(c.YEAR)访问常量 - 使用类(Calendar.YEAR)访问它们。

答案 2 :(得分:3)

刚刚发布一个基于@FabianCook的略微修改过的解决方案,正如@harshal指出的那样,他的解决方案并不能满足不同年份的两个日期,而是在同一周内。

修改是将两个日历日期中的DAY_OF_WEEK实际设置为指向星期一.....在这种情况下,两个日期将设置为同一天,即使它们处于不同年份,然后我们可以比较它们。

Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int year1 = cal1.get(Calendar.YEAR);
int week1 = cal1.get(Calendar.WEEK_OF_YEAR);

Calendar cal2 = Calendar.getInstance();
cal2.setTimeInMillis(/*Second date in millis here*/);
cal2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int year2 = cal2.get(Calendar.YEAR);
int week2 = cal2.get(Calendar.WEEK_OF_YEAR);

if(year1 == year2 && week1 == week2){
    //Do what you want here
}

答案 3 :(得分:1)

这是一个java解决方案。以下代码段检查两个日期是否在同一周内。 它还涵盖了边缘情况,其中一周开始于一个日历年(十二月),另一年结束于明年(一月)。

注意:代码依赖于joda-time:

compile 'joda-time:joda-time:2.3'


public static boolean isSameWeek(final Date d1, final Date d2) {
    if ((d1 == null) || (d2 == null))
        throw new IllegalArgumentException("The date must not be null");

    return isSameWeek(new DateTime(d1), new DateTime(d2));
}

public static boolean isSameWeek(final DateTime d1, final DateTime d2) {
    if ((d1 == null) || (d2 == null))
        throw new IllegalArgumentException("The date must not be null");

    // It is important to use week of week year & week year

    final int week1 = d1.getWeekOfWeekyear();
    final int week2 = d2.getWeekOfWeekyear();

    final int year1 = d1.getWeekyear();
    final int year2 = d2.getWeekyear();

    final int era1 = d1.getEra();
    final int era2 = d2.getEra();

    // Return true if week, year and era matches
    if ((week1 == week2) && (year1 == year2) && (era1 == era2))
        return true;

    // Return false if none of the conditions are satisfied
    return false;
}

测试用例:

public class TestDateUtil {

@Test
public void testIsSameWeek() {

    final DateTime d1 = new DateTime(2014, 12, 31, 0, 0);
    final DateTime d2 = new DateTime(2015, 1, 1, 0, 0);
    final DateTime d3 = new DateTime(2015, 1, 2, 0, 0);
    final DateTime d4 = new DateTime(2015, 1, 8, 0, 0);

    assertTrue(isSameWeek(d1, d2));
    assertTrue(isSameWeek(d2, d1));

    assertTrue(isSameWeek(d2, d3));
    assertTrue(isSameWeek(d3, d2));

    assertFalse(isSameWeek(d2, d4));
    assertFalse(isSameWeek(d4, d2));

    assertFalse(isSameWeek(d1, d4));
    assertFalse(isSameWeek(d4, d1));

}
}

答案 4 :(得分:0)

private int weeksBetween(Calendar startDate, Calendar endDate) {
    startDate.set(Calendar.HOUR_OF_DAY, 0);
    startDate.set(Calendar.MINUTE, 0);
    startDate.set(Calendar.SECOND, 0);
    int start = (int)TimeUnit.MILLISECONDS.toDays(
        startDate.getTimeInMillis())
        - startDate.get(Calendar.DAY_OF_WEEK);
    int end = (int)TimeUnit.MILLISECONDS.toDays(
        endDate.getTimeInMillis());
    return (end - start) / 7;
}

如果此方法返回0,则它们在同一周

如果此方法返回1 endDate是startDate之后的一周

如果此方法返回-1,则endDate是startDate之前的一周

你明白了

答案 5 :(得分:0)

比方说,一周从一年开始,到第二年结束,我们从本周开始选择2个日期,以便它们分别处于不同的年份。然后,接受的答案会产生错误的结果,因为它会检查日期是否在 SAME YEAR!中。

要改进此解决方案,可以获取与一周中某天相对应的日期(可能是Monday),然后检查两个日期是否都属于同一天。但是,在执行此操作之前,请确保两个Calendar对象都在 SAME 时区中。因此,将一个时区设置为另一个时区。另外,以下解决方案适用于每个 API 级别,因为它不需要使用getWeekOfWeekyear()getWeekYear()(这些与 API { {1}}及更高版本。

24

答案 6 :(得分:0)

如果同一周中有两个日期,则为真

fun isEqualWeek(date1: Long, date2: Long):Boolean{
    val cal1 = Calendar.getInstance()
    val cal2 = Calendar.getInstance()
    cal1.time = Date(date1)
    cal2.time = Date(date2)
    return (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) && (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(
        Calendar.WEEK_OF_YEAR
    ))
}

答案 7 :(得分:0)

在 Joda 的图书馆中,我发现一周从星期一开始,但我希望一周从星期日开始,所以只使用了以下函数:

  public static boolean isSameWeek(final Date initDate, final Date finalDate) {
    if (initDate != null && finalDate != null) {
        Calendar initCalender = Calendar.getInstance();
        Calendar finalCalender = Calendar.getInstance();
        initCalender.setTime(initDate);
        finalCalender.setTime(finalDate);
        if (finalCalender.get(Calendar.YEAR) >= initCalender.get(Calendar.YEAR)) {
            //check for same year
            if (finalCalender.get(Calendar.YEAR) == initCalender.get(Calendar.YEAR)) {
                if (finalCalender.get(Calendar.WEEK_OF_YEAR) == initCalender.get(Calendar.WEEK_OF_YEAR)) {
                    return true;
                }
            } else //check whether next corresponding year
                if (finalCalender.get(Calendar.YEAR) - initCalender.get(Calendar.YEAR) == 1) {
                    if (initCalender.get(Calendar.WEEK_OF_YEAR) == 1 || initCalender.get(Calendar.WEEK_OF_YEAR) == 53) {
                        if (finalCalender.get(Calendar.WEEK_OF_YEAR) == 1) {
                            if (initCalender.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && finalCalender.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                                return true;
                            }
                        }
                    }
                }
        } else {
            throw new IllegalArgumentException("Final Date should be greater or equal to Initial Date");
        }
    } else {
        throw new IllegalArgumentException("The date must not be null");
    }
    return false;
}
相关问题