Weekly correspondence method of java and mysql

MYSQL方法:
select dt,if(month(dt)=1 and weekofyear(dt)>=52,year(dt)-1,year(dt)) as year,lpad(if(weekofyear(dt)<=1 and month(dt) >= 11,weekofyear(dt)+52,weekofyear(dt)),2,'0') as week from n_match;

JAVA方法
public WeekReportPeriod doPeriod(String dt) {
if (StringUtils.isEmpty(dt)) {
return this;
}
dt = dt.substring(0, 10);
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setMinimalDaysInFirstWeek(4);// 每年第一周需要4天在本年度内
cal.setTime(VOUtil.parse(dt));
int week = cal.get(Calendar.WEEK_OF_YEAR);
if (week <= 1) {
int month = cal.get(Calendar.MONTH);
if (month >= 11) {
week += 52;
}
}
int year = cal.get(Calendar.YEAR);
if (week >= 52) {
int month = cal.get(Calendar.MONTH);
if (month <= 1) {
year = year - 1;
}
}
return this;
}

validates the data from 2010-01-01 to 2029-12-16, the year and week match exactly

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326070240&siteId=291194637