如何在 java 中获取连续的周数和年份?

How to get successive week number and year in java?

我正在存储一些用周数引用的数据。现在,我有两个按钮来请求数据。一个用于前一周,另一个用于下一周 week.And 默认获取当前周的数据

我刚刚完成年终例外,我了解到有第 53 周明年(2020)也是如此。现在每当有第 53 周时它就会中断。(它只是跳过第 53 周)

显然,必须有更好的方法来做到这一点.. 我一直在添加异常,下面是我最终得到的代码。 我是菜鸟,所以请忍受丑陋的逻辑。

LocalDate wks, mon, tue, wed, thu, fri, sat, wke;
try {
    week = Integer.parseInt(request.getParameter("week"));
    year = Integer.parseInt(request.getParameter("year"));
} catch (NumberFormatException e) {
    week = getWeek();
    year = getCurrYear();
    flag = 1;
}
LocalDate date = LocalDate.now()
.withYear(year) // year
.with(WeekFields.ISO.weekOfWeekBasedYear(), week) // week of year
.with(WeekFields.ISO.dayOfWeek(), 7);
wks = date.plusDays(-7);
if ((date + "").substring(0, 4).equals((wks + "").substring(0, 4))) {
} else if (flag == 1) {
    year++;
}
date = LocalDate.now()
.withYear(year) // year
.with(WeekFields.ISO.weekOfWeekBasedYear(), week) // week of year
.with(WeekFields.ISO.dayOfWeek(), 7);
wks = date.plusDays(-7);
mon = wks.plusDays(1);
tue = wks.plusDays(2);
wed = wks.plusDays(3);
thu = wks.plusDays(4);
fri = wks.plusDays(5);
sat = wks.plusDays(6);
wke = date;
System.out.println(week + " " + year);
heading = "<div class='row'>"
        + "<form action=\"";
if (week == 1) {
    heading += "javascript:setContent('/getData?week=52&year=" + (year - 1) + "')\" >";
} else {
    heading += "javascript:setContent('getData?week=" + (week - 1) + "&year=" + (year) + "')\" >";
}
heading += "<button type='submit'>Previous</button></form><form action=\"";
if (week == 52) {
    heading += "javascript:setContent('getData?week=1&year=" + (year + 1) + "')\" >";
} else {
    heading += "javascript:setContent('getData?week=" + (week + 1) + "&year=" + (year) + "')\" >";
}
heading += "<button type='submit'Next</button></form>";
out.print(heading);

函数:getWeek()

public static int getWeek() {
        java.util.Date date = new java.util.Date();
        SimpleDateFormat ft = new SimpleDateFormat("w");
        return (Integer.parseInt(ft.format(date)));
    }

函数:getCurrYear() {

public static int getCurrYear() {
         Date date = new Date();
         return (date.getYear() + 1900);
    }

我必须打印星期几的日期,这就是为什么星期一到星期六的变量。

I just want a function like below

function int[] getWeekYear(){
    return [prev week,prev week's year,current week,current year,next week,next week's year];
}

套餐java.time会给你很大的满足:

LocalDate.now().get(WeekFields.ISO.weekOfWeekBasedYear())

这将return一年中的第一个星期(现在returns 52执行),但整个包包含了你需要的一切,你只需要探索。

tl;博士

YearWeek
.now( 
    ZoneId.of( "Asia/Tokyo" ) 
)
.minusWeeks( 1 ) 
.getWeek()         // Returns an `int` in range of 1-52 or 1-53, the week number within a year. 

详情

永远不要使用这两个 Date class 中的任何一个。它们很糟糕,现在已成为遗产。多年前,它们被 JSR 310 中定义的现代 java.time classes 所取代。

是正确的。

YearWeek

此外,如果您对周的定义是标准 ISO 8601 定义(周从星期一开始,第 1 周包含日历年的第一个星期四),则添加 ThreeTen-Extra library to your project. This library has classes extending the functionality of java.time. In particular, you get the YearWeek class.

获取本周。这需要一个时区。对于任何给定时刻,日期在全球各地因时区而异。

ZoneId z = ZoneId.of( "Africa/Tunis" );
YearWeek currentWeek = YearWeek.now( z ) ;

要获得前一周或下周,只需调用 plus 或 minus 方法即可。

YearWeek priorWeek = currentWeek.minusWeeks( 1 ) ;
YearWeek followingWeek = currentWeek.plusWeeks( 1 ) ;

如果您想要一周中的日期,请调用 atDay 并从 DayOfWeek 枚举中传递星期几。

LocalDate localDate = priorWeek.atDay( DayOfWeek.MONDAY ) ;

您可以查询周数及其年份(即基于周的年份)。

int weekNumber = priorWeek.getWeek() ;
int weekBasedYearNumber = priorWeek.getYear() ;