本文介绍了如何检查一天是在本周的Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有最简单的方法可以找到目前的一周中的任何一天? (这个函数返回true或false,与给定的一天相关的是当前的一周)。

解决方案

你一定要使用日历类:



这里有一种方法:

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


Is there easiest way to find any day is in the current week? (this function returns true or false, related to given day is in current week or not).

解决方案

You definitely want to use the Calendar class: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

Here's one way to do it:

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

这篇关于如何检查一天是在本周的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:23