[英]Get Date of Specific Week
I'm trying to get the start and end date of specific week of a month.我正在尝试获取一个月中特定一周的开始和结束日期。 However the date is incorrect.
但是日期不正确。 Can anyone identify what's the issue?
谁能确定是什么问题?
public class DateUtils
{
getWeeklyDateList(2020,5, 3);
public static void getWeeklyDateList(int year, int month, int week)
{
Calendar calendar = Calendar.getInstance(Locale.getDefault());
// setting year, month, week
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.WEEK_OF_MONTH,week);
// setting day of week to first --> Sunday
calendar.set(Calendar.DAY_OF_WEEK, 1);
int year1 = calendar.get(Calendar.YEAR);
int month1 = calendar.get(Calendar.MONTH);
int day1 = calendar.get(Calendar.DAY_OF_MONTH);
// setting day of week to last --> Saturday
calendar.set(Calendar.DAY_OF_WEEK, 7);
int year7 = calendar.get(Calendar.YEAR);
int month7 = calendar.get(Calendar.MONTH);
int day7 = calendar.get(Calendar.DAY_OF_MONTH);
Log.e("date_start", String.valueOf(year1) + "-" + String.valueOf(month1) + "-" + String.valueOf(day1));
Log.e("date_end", String.valueOf(year7) + "-" + String.valueOf(month7) + "-" + String.valueOf(day7));
} }
If you want to use java.time, the modern Java date and time API, it can be done with this simple method:如果要使用java.time,现代的Java日期和时间API,可以用这个简单的方法来完成:
private static WeekFields wf = WeekFields.of(Locale.forLanguageTag("ne-NP"));
public static void getWeeklyDateList(int year, Month month, int week) {
LocalDate someDateInTheWeek = LocalDate.of(year, month, 10).with(wf.weekOfMonth(), week);
LocalDate start = someDateInTheWeek.with(wf.dayOfWeek(), 1);
LocalDate end = someDateInTheWeek.with(wf.dayOfWeek(), 7);
System.out.println("date_start: " + start);
System.out.println("date_end: " + end);
}
Trying it out with your example arguments:使用您的示例 arguments 进行尝试:
getWeeklyDateList(2020, Month.JUNE, 3);
Output is: Output 是:
date_start: 2020-06-14 date_end: 2020-06-20
How it works:这个怎么运作:
First, weeks are defined differently in different cultures.首先,不同文化对周的定义不同。 In Nepal (since you give Kathmandu, Nepal as your location) weeks start on Sunday and are numbered in a way where the 1st of the month is in week 1 of the month.
在尼泊尔(因为您将尼泊尔加德满都作为您的位置),周从星期日开始,并且以每月 1 日为每月第 1 周的方式编号。 To handle this week scheme I am initializing a
WeekFields
object for Nepalese culture.为了处理本周计划,我正在为尼泊尔文化初始化一个
WeekFields
object。
LocalDate
is the java.time class for a date without time of day. LocalDate
是没有时间的日期的 java.time class。 I don't think it matters which day of the month I pick as a starting point;我认为我选择一个月中的哪一天作为起点并不重要。 I took the 10th.
我拿了第10个。 From that date I get a date in the correct week, using the
WeekFields
object and the supplied week number.从该日期开始,我使用
WeekFields
object 和提供的周数得到正确周的日期。 From there in turn I get the first and the last day of the week, again according to the Nepalese definition of weeks: from Sunday June 14 through Saturday June 20 2020.从那里依次得到一周的第一天和最后一天,同样根据尼泊尔对周的定义:从 2020 年 6 月 14 日星期日到 2020 年 6 月 20 日星期六。
What went wrong in your code I cannot tell.我不知道你的代码出了什么问题。 In any case the
Calendar
class you used is poorly designed and long outdated.无论如何,您使用的
Calendar
class 设计不佳且早已过时。 It also default uses the default locale of the JVM for its week definition, which may have given you a different week scheme from what you wanted.它还默认使用 JVM 的默认语言环境作为其周定义,这可能会为您提供与您想要的不同的周计划。 A final point that may have confused you:
Calendar
unnaturally numbers months from 0 for January through 11 for December.最后一点可能让您感到困惑:
Calendar
不自然地从一月的 0 到十二月的 11 对月份进行编号。 So when you specified 5, you got June (not May).因此,当您指定 5 时,您得到的是六月(而不是五月)。 You printed out the month numbers of your result dates, which probably again printed 5 (not 6) for June.
您打印了结果日期的月份编号,这可能再次打印了 5(而不是 6)六月。
java.time works nicely on both older and newer Android devices. java.time 适用于较旧和较新的 Android 设备。 It just requires at least Java 6 .
它只需要至少Java 6 。
org.threeten.bp
with subpackages.org.threeten.bp
导入日期和时间类。java.time
was first described. java.time
。java.time
to Java 6 and 7 (ThreeTen for JSR-310). java.time
到 Java 6 和 7 的 backport(ThreeTen for JSR-310)。Its because of this line, calendar.set(Calendar.DAY_OF_WEEK, 1);因为这条线,calendar.set(Calendar.DAY_OF_WEEK, 1);
this can take month to previous month if week starts in previous one.如果一周从上一个开始,这可能需要一个月到上个月。 If there is a month difference then this is the problem.
如果有一个月的差异,那么这就是问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.