如何检查一天是在Java的当前周?

时间:2012-04-25 10:26:17

标签: java date

是否有最简单的方法可以找到当天的任何一天? (此函数返回true或false,与给定日期相关,是否在当前周内)。

5 个答案:

答案 0 :(得分:18)

您肯定想使用Calendar类:http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

这是一种方法:

public static boolean isDateInCurrentWeek(Date date) {
  Calendar currentCalendar = Calendar.getInstance();
  int week = currentCalendar.get(Calendar.WEEK_OF_YEAR);
  int year = currentCalendar.get(Calendar.YEAR);
  Calendar targetCalendar = Calendar.getInstance();
  targetCalendar.setTime(date);
  int targetWeek = targetCalendar.get(Calendar.WEEK_OF_YEAR);
  int targetYear = targetCalendar.get(Calendar.YEAR);
  return week == targetWeek && year == targetYear;
}

答案 1 :(得分:4)

使用Calendar类获取YEAR和WEEK_OF_YEAR字段,看看两者是否相同。

请注意,根据区域设置,结果会有所不同,因为不同的文化对于哪一天是一周的第一天有不同的意见。

答案 2 :(得分:1)

TL;博士

一周是几周?星期天星期六?星期一星期天?一个ISO 8601 week

对于标准ISO 8601周......

YearWeek.now( ZoneId.of( "America/Montreal" ) )
        .equals( 
            YearWeek.from( 
                LocalDate.of( 2017 , Month.JANUARY , 23 ) 
            ) 
        )

java.time

现代方法使用java.time类来取代麻烦的旧日期时间类,例如Date& Calendar

此外,ThreeTen-Extra项目为我们的目的提供了一个方便的YearWeek课程,假设标准的ISO 8601周是“周”这个词。该类的文档总结了标准周的定义:

  

ISO-8601将星期定义为始终从星期一开始。第一周是包含该日历年的第一个星期四的一周。因此,本课程中使用的以周为基础的年份与日历年不一致。

时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因地区而异。例如,在Paris France午夜后的几分钟是新的一天,而Montréal Québec中仍然是“昨天”。

continent/region的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用诸如ESTIST之类的3-4字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" );
YearWeek ywNow = YearWeek.now( z ) ;  // Get the current week for this specific time zone.

LocalDate类表示没有时间且没有时区的仅限日期的值。

LocalDate ld = LocalDate.of( 2017 , Month.JANUARY , 23 ) ;  // Some date.

要查看特定日期是否包含在目标YearWeek对象的日期中,请获取该日期的YearWeek并通过调用equals进行比较。

YearWeek ywThen = YearWeek.from( ld ) ;  // Determine year-week of some date.
Boolean isSameWeek = ywThen.equals( ywNow ) ;

关于java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和& SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore


约达时间

更新:Joda-Time项目现在位于maintenance mode,团队建议迁移到java.time课程。留下这部分完整的历史记录。

在Joda-Time 2.3中检查ISO周很容易。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

DateTimeZone parisTimeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime now = new DateTime( parisTimeZone );
DateTime yesterday = now.minusDays( 1 );
DateTime inThree = now.plusDays( 3 );
DateTime inFourteen = now.plusDays( 14 );

System.out.println( "Now: " + now + " is ISO week: " + now.weekOfWeekyear().get() );
System.out.println( "Same ISO week as yesterday ( " + yesterday + " week "+ yesterday.weekOfWeekyear().get() +" ): " + ( now.weekOfWeekyear().get() == yesterday.weekOfWeekyear().get() ) );
System.out.println( "Same ISO week as inThree ( " + inThree + " week "+ inThree.weekOfWeekyear().get() +" ): " + ( now.weekOfWeekyear().get() == inThree.weekOfWeekyear().get() ) );
System.out.println( "Same ISO week as inFourteen ( " + inFourteen + " week "+ inFourteen.weekOfWeekyear().get() +" ): " + ( now.weekOfWeekyear().get() == inFourteen.weekOfWeekyear().get() ) );

跑步时......

Now: 2013-12-12T06:32:49.020+01:00 is ISO week: 50
Same ISO week as yesterday ( 2013-12-11T06:32:49.020+01:00 week 50 ): true
Same ISO week as inThree ( 2013-12-15T06:32:49.020+01:00 week 50 ): true
Same ISO week as inFourteen ( 2013-12-26T06:32:49.020+01:00 week 52 ): false

答案 3 :(得分:0)

此答案适用于Android。将您的选定日期日历对象通过以下方法放入检查,您选择的日期是否为当前星期日期

private boolean isCurrentWeekDateSelect(Calendar yourSelectedDate) {
    Date ddd = yourSelectedDate.getTime();
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);

    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    Date monday = c.getTime();
    Date nextMonday= new Date(monday.getTime()+7*24*60*60*1000);

    return ddd.after(monday) && ddd.before(nextMonday);
}

Android已经为任何与日期和时间相关的查询提供了java.util.Calendar类。

答案 4 :(得分:0)

简单的Kotlin解决方案:

fun isDateInCurrentWeek(date: DateTime): Boolean {
         val cal = Calendar.getInstance()
         cal.add(Calendar.DAY_OF_WEEK, 0)
         var dt= DateTime(cal.timeInMillis)
         return date in dt..dt.plusDays(6)
        }