求算法。我已经有一种方法,但是貌似不对

本文探讨了一种使用Java进行周数计算的方法,并针对特定日期范围内的异常结果进行了深入分析。通过示例代码展示了从2014年第1周到第12周的计算过程及存在的问题,提出使用Joda-Time库作为潜在解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题:】时间格式yyyymm,(mm为周一年有52周,01,01.。。。)求201401 201412之间的周数

========================我的算法


package com.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TestTime {
public static void main(String[] args) {

getWeek("201401","201412");
}

public static String getWeek(String startDate,String endDate)
{
String[] weeks = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyww");
try {
Date start = sdf.parse(startDate);
Date end = sdf.parse(endDate);

Long l = end.getTime() - start.getTime();
Long l2= l/(1000*3600*24*7);
int length = l2.intValue()+1;
Calendar cal = Calendar.getInstance();
cal.setTime(start);
weeks = new String[length];
for(int i = 0;i<length;i++)
{
cal.add(Calendar.WEEK_OF_YEAR, i);
weeks[i] = sdf.format(cal.getTime());
System.out.println("the week:"+weeks[i]);
if(i!=0)
{
cal.add(Calendar.WEEK_OF_YEAR, -i);
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}

}

====================打印结果==================

the week:201301
the week:201402
the week:201403
the week:201404
the week:201405
the week:201406
the week:201407
the week:201408
the week:201409
the week:201410
the week:201411
the week:201412





如果换成201402 201412就很正常。

不知道为什么?


joda time jar这个jar谁用过,这个jar能解决上面的问题不?求解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值