java 求时间分组 新人求帮助~~~~~~~~~

郁闷的小生活 2018-08-23 05:41:05
输入开始时间和结束时间,如果时间差大于35天,则按周分组.例如:kssj:2017-08-05 jssj: 2018-08-05 结果:2017-08-05 ~ 2017-08-11 以此类推...................请哪位大佬可以帮忙解决一下....
...全文
160 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 5 楼 u010970951 的回复:
[quote=引用 4 楼 siqi1996 的回复:]
[quote=引用 3 楼 u010970951 的回复:]
按周分组的话需要用到Calendar日历类。将周数拿出来,然后循环七天,一直到结尾,
代码如下:
String a = "2017-12-01";

String b = "2018-02-02";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ad = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ad);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "-" + cl1.get(Calendar.DAY_OF_WEEK));


Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;//结果周数

//一直累加小的日期知道跟大的日期相同
while (true) {

if (cl2.compareTo(cl1) <= 0) {
break;
}

int count = 7;//每周天数

if (num == 0) {//第一次进入需要计算是星期几,从来算出剩下几天
//因为java中周末是算作一周开始是1,以此类推周1-6为2-7,而我们通常是从周一开始,所以我们减掉1,我们从周一开始算
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;
day = day == 0 ? 7 : day;//周末这时候就是0了,我们将周末改成7
count = 7 - day + 1;
}
if (!result.containsKey(num)) {//若是下一周星期一,则新建一个周数
result.put(num, new ArrayList<String>());
}

for (int i = 0; i < count; i++) {//将该周剩余天数添加进去
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_YEAR, 1);//添加一天
if (cl2.compareTo(cl1) <= 0) {
break;
}
}

num ++;//周数增加
}

for (Entry<Integer, List<String>> entry : result.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
结果如下:
0:[2017-12-01, 2017-12-02, 2017-12-03]
1:[2017-12-04, 2017-12-05, 2017-12-06, 2017-12-07, 2017-12-08, 2017-12-09, 2017-12-10]
2:[2017-12-11, 2017-12-12, 2017-12-13, 2017-12-14, 2017-12-15, 2017-12-16, 2017-12-17]
3:[2017-12-18, 2017-12-19, 2017-12-20, 2017-12-21, 2017-12-22, 2017-12-23, 2017-12-24]
4:[2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2017-12-30, 2017-12-31]
5:[2018-01-01, 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-06, 2018-01-07]
6:[2018-01-08, 2018-01-09, 2018-01-10, 2018-01-11, 2018-01-12, 2018-01-13, 2018-01-14]
7:[2018-01-15, 2018-01-16, 2018-01-17, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21]
8:[2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25, 2018-01-26, 2018-01-27, 2018-01-28]
9:[2018-01-29, 2018-01-30, 2018-01-31, 2018-02-01]


老哥,我要怎么测试呢?main方法我要调谁呀,我还得稍微修改一下,您给我的这个是对的,我要的最后格式是
time:[2017-12-01~2017-12-03,
2017-12-04~2017-12-10,
........
]
这种格式的,所以我在IDEA中把您的代码写上了,可是我不知道调谁了.........[/quote]

这是一个完整的方法 ,你可以直接写成一个方法使用,传入二个时间参数即可,因为我是main方法测试的,所以你自己写到单独方法里面就行了
至于格式,就更容易了,你遍历result的时候,把首尾取出来就好,给你一个完整方法
public static List<String> getResults(String a, String b) {
//前一部分代码,省略了
//下面遍历一下,取到你要的结果
List<String> time = new ArrayList<String>();
for (Entry<Integer, List<String>> entry : result.entrySet()) {
List<String> temp = entry.getValue();
String str = temp.get(0) + "~" + temp.get(temp.size() - 1);
time.add(str);
}

return result;
}
使用时 调用 getResults("2017-01-01", "2018-02-02")即可,注意方法上static根据实际情况选择添加与否
结果:
time:[2017-12-01~2017-12-03, 2017-12-04~2017-12-10, 2017-12-11~2017-12-17, 2017-12-18~2017-12-24, 2017-12-25~2017-12-31, 2018-01-01~2018-01-07, 2018-01-08~2018-01-14, 2018-01-15~2018-01-21, 2018-01-22~2018-01-28, 2018-01-29~2018-02-01]



[/quote]最后return time;打错字了、
郁闷的小生活 2018-08-24
  • 打赏
  • 举报
回复
引用 6 楼 u010970951 的回复:
[quote=引用 5 楼 u010970951 的回复:]
[quote=引用 4 楼 siqi1996 的回复:]
[quote=引用 3 楼 u010970951 的回复:]
按周分组的话需要用到Calendar日历类。将周数拿出来,然后循环七天,一直到结尾,
代码如下:
String a = "2017-12-01";

String b = "2018-02-02";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ad = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ad);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "-" + cl1.get(Calendar.DAY_OF_WEEK));


Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;//结果周数

//一直累加小的日期知道跟大的日期相同
while (true) {

if (cl2.compareTo(cl1) <= 0) {
break;
}

int count = 7;//每周天数

if (num == 0) {//第一次进入需要计算是星期几,从来算出剩下几天
//因为java中周末是算作一周开始是1,以此类推周1-6为2-7,而我们通常是从周一开始,所以我们减掉1,我们从周一开始算
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;
day = day == 0 ? 7 : day;//周末这时候就是0了,我们将周末改成7
count = 7 - day + 1;
}
if (!result.containsKey(num)) {//若是下一周星期一,则新建一个周数
result.put(num, new ArrayList<String>());
}

for (int i = 0; i < count; i++) {//将该周剩余天数添加进去
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_YEAR, 1);//添加一天
if (cl2.compareTo(cl1) <= 0) {
break;
}
}

num ++;//周数增加
}

for (Entry<Integer, List<String>> entry : result.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
结果如下:
0:[2017-12-01, 2017-12-02, 2017-12-03]
1:[2017-12-04, 2017-12-05, 2017-12-06, 2017-12-07, 2017-12-08, 2017-12-09, 2017-12-10]
2:[2017-12-11, 2017-12-12, 2017-12-13, 2017-12-14, 2017-12-15, 2017-12-16, 2017-12-17]
3:[2017-12-18, 2017-12-19, 2017-12-20, 2017-12-21, 2017-12-22, 2017-12-23, 2017-12-24]
4:[2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2017-12-30, 2017-12-31]
5:[2018-01-01, 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-06, 2018-01-07]
6:[2018-01-08, 2018-01-09, 2018-01-10, 2018-01-11, 2018-01-12, 2018-01-13, 2018-01-14]
7:[2018-01-15, 2018-01-16, 2018-01-17, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21]
8:[2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25, 2018-01-26, 2018-01-27, 2018-01-28]
9:[2018-01-29, 2018-01-30, 2018-01-31, 2018-02-01]


老哥,我要怎么测试呢?main方法我要调谁呀,我还得稍微修改一下,您给我的这个是对的,我要的最后格式是
time:[2017-12-01~2017-12-03,
2017-12-04~2017-12-10,
........
]
这种格式的,所以我在IDEA中把您的代码写上了,可是我不知道调谁了.........[/quote]

这是一个完整的方法 ,你可以直接写成一个方法使用,传入二个时间参数即可,因为我是main方法测试的,所以你自己写到单独方法里面就行了
至于格式,就更容易了,你遍历result的时候,把首尾取出来就好,给你一个完整方法
public static List<String> getResults(String a, String b) {
//前一部分代码,省略了
//下面遍历一下,取到你要的结果
List<String> time = new ArrayList<String>();
for (Entry<Integer, List<String>> entry : result.entrySet()) {
List<String> temp = entry.getValue();
String str = temp.get(0) + "~" + temp.get(temp.size() - 1);
time.add(str);
}

return result;
}
使用时 调用 getResults("2017-01-01", "2018-02-02")即可,注意方法上static根据实际情况选择添加与否
结果:
time:[2017-12-01~2017-12-03, 2017-12-04~2017-12-10, 2017-12-11~2017-12-17, 2017-12-18~2017-12-24, 2017-12-25~2017-12-31, 2018-01-01~2018-01-07, 2018-01-08~2018-01-14, 2018-01-15~2018-01-21, 2018-01-22~2018-01-28, 2018-01-29~2018-02-01]



[/quote]最后return time;打错字了、[/quote]


谢谢老哥.祝您工作顺心...
  • 打赏
  • 举报
回复
引用 4 楼 siqi1996 的回复:
[quote=引用 3 楼 u010970951 的回复:]
按周分组的话需要用到Calendar日历类。将周数拿出来,然后循环七天,一直到结尾,
代码如下:
String a = "2017-12-01";

String b = "2018-02-02";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ad = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ad);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "-" + cl1.get(Calendar.DAY_OF_WEEK));


Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;//结果周数

//一直累加小的日期知道跟大的日期相同
while (true) {

if (cl2.compareTo(cl1) <= 0) {
break;
}

int count = 7;//每周天数

if (num == 0) {//第一次进入需要计算是星期几,从来算出剩下几天
//因为java中周末是算作一周开始是1,以此类推周1-6为2-7,而我们通常是从周一开始,所以我们减掉1,我们从周一开始算
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;
day = day == 0 ? 7 : day;//周末这时候就是0了,我们将周末改成7
count = 7 - day + 1;
}
if (!result.containsKey(num)) {//若是下一周星期一,则新建一个周数
result.put(num, new ArrayList<String>());
}

for (int i = 0; i < count; i++) {//将该周剩余天数添加进去
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_YEAR, 1);//添加一天
if (cl2.compareTo(cl1) <= 0) {
break;
}
}

num ++;//周数增加
}

for (Entry<Integer, List<String>> entry : result.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
结果如下:
0:[2017-12-01, 2017-12-02, 2017-12-03]
1:[2017-12-04, 2017-12-05, 2017-12-06, 2017-12-07, 2017-12-08, 2017-12-09, 2017-12-10]
2:[2017-12-11, 2017-12-12, 2017-12-13, 2017-12-14, 2017-12-15, 2017-12-16, 2017-12-17]
3:[2017-12-18, 2017-12-19, 2017-12-20, 2017-12-21, 2017-12-22, 2017-12-23, 2017-12-24]
4:[2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2017-12-30, 2017-12-31]
5:[2018-01-01, 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-06, 2018-01-07]
6:[2018-01-08, 2018-01-09, 2018-01-10, 2018-01-11, 2018-01-12, 2018-01-13, 2018-01-14]
7:[2018-01-15, 2018-01-16, 2018-01-17, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21]
8:[2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25, 2018-01-26, 2018-01-27, 2018-01-28]
9:[2018-01-29, 2018-01-30, 2018-01-31, 2018-02-01]


老哥,我要怎么测试呢?main方法我要调谁呀,我还得稍微修改一下,您给我的这个是对的,我要的最后格式是
time:[2017-12-01~2017-12-03,
2017-12-04~2017-12-10,
........
]
这种格式的,所以我在IDEA中把您的代码写上了,可是我不知道调谁了.........[/quote]

这是一个完整的方法 ,你可以直接写成一个方法使用,传入二个时间参数即可,因为我是main方法测试的,所以你自己写到单独方法里面就行了
至于格式,就更容易了,你遍历result的时候,把首尾取出来就好,给你一个完整方法
public static List<String> getResults(String a, String b) {
//前一部分代码,省略了
//下面遍历一下,取到你要的结果
List<String> time = new ArrayList<String>();
for (Entry<Integer, List<String>> entry : result.entrySet()) {
List<String> temp = entry.getValue();
String str = temp.get(0) + "~" + temp.get(temp.size() - 1);
time.add(str);
}

return result;
}
使用时 调用 getResults("2017-01-01", "2018-02-02")即可,注意方法上static根据实际情况选择添加与否
结果:
time:[2017-12-01~2017-12-03, 2017-12-04~2017-12-10, 2017-12-11~2017-12-17, 2017-12-18~2017-12-24, 2017-12-25~2017-12-31, 2018-01-01~2018-01-07, 2018-01-08~2018-01-14, 2018-01-15~2018-01-21, 2018-01-22~2018-01-28, 2018-01-29~2018-02-01]



郁闷的小生活 2018-08-24
  • 打赏
  • 举报
回复
引用 3 楼 u010970951 的回复:
按周分组的话需要用到Calendar日历类。将周数拿出来,然后循环七天,一直到结尾,
代码如下:
String a = "2017-12-01";

String b = "2018-02-02";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ad = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ad);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "-" + cl1.get(Calendar.DAY_OF_WEEK));


Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;//结果周数

//一直累加小的日期知道跟大的日期相同
while (true) {

if (cl2.compareTo(cl1) <= 0) {
break;
}

int count = 7;//每周天数

if (num == 0) {//第一次进入需要计算是星期几,从来算出剩下几天
//因为java中周末是算作一周开始是1,以此类推周1-6为2-7,而我们通常是从周一开始,所以我们减掉1,我们从周一开始算
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;
day = day == 0 ? 7 : day;//周末这时候就是0了,我们将周末改成7
count = 7 - day + 1;
}
if (!result.containsKey(num)) {//若是下一周星期一,则新建一个周数
result.put(num, new ArrayList<String>());
}

for (int i = 0; i < count; i++) {//将该周剩余天数添加进去
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_YEAR, 1);//添加一天
if (cl2.compareTo(cl1) <= 0) {
break;
}
}

num ++;//周数增加
}

for (Entry<Integer, List<String>> entry : result.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
结果如下:
0:[2017-12-01, 2017-12-02, 2017-12-03]
1:[2017-12-04, 2017-12-05, 2017-12-06, 2017-12-07, 2017-12-08, 2017-12-09, 2017-12-10]
2:[2017-12-11, 2017-12-12, 2017-12-13, 2017-12-14, 2017-12-15, 2017-12-16, 2017-12-17]
3:[2017-12-18, 2017-12-19, 2017-12-20, 2017-12-21, 2017-12-22, 2017-12-23, 2017-12-24]
4:[2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2017-12-30, 2017-12-31]
5:[2018-01-01, 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-06, 2018-01-07]
6:[2018-01-08, 2018-01-09, 2018-01-10, 2018-01-11, 2018-01-12, 2018-01-13, 2018-01-14]
7:[2018-01-15, 2018-01-16, 2018-01-17, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21]
8:[2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25, 2018-01-26, 2018-01-27, 2018-01-28]
9:[2018-01-29, 2018-01-30, 2018-01-31, 2018-02-01]


老哥,我要怎么测试呢?main方法我要调谁呀,我还得稍微修改一下,您给我的这个是对的,我要的最后格式是
time:[2017-12-01~2017-12-03,
2017-12-04~2017-12-10,
........
]
这种格式的,所以我在IDEA中把您的代码写上了,可是我不知道调谁了.........
  • 打赏
  • 举报
回复
按周分组的话需要用到Calendar日历类。将周数拿出来,然后循环七天,一直到结尾,
代码如下:
String a = "2017-12-01";

String b = "2018-02-02";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ad = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ad);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "-" + cl1.get(Calendar.DAY_OF_WEEK));


Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;//结果周数

//一直累加小的日期知道跟大的日期相同
while (true) {

if (cl2.compareTo(cl1) <= 0) {
break;
}

int count = 7;//每周天数

if (num == 0) {//第一次进入需要计算是星期几,从来算出剩下几天
//因为java中周末是算作一周开始是1,以此类推周1-6为2-7,而我们通常是从周一开始,所以我们减掉1,我们从周一开始算
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;
day = day == 0 ? 7 : day;//周末这时候就是0了,我们将周末改成7
count = 7 - day + 1;
}
if (!result.containsKey(num)) {//若是下一周星期一,则新建一个周数
result.put(num, new ArrayList<String>());
}

for (int i = 0; i < count; i++) {//将该周剩余天数添加进去
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_YEAR, 1);//添加一天
if (cl2.compareTo(cl1) <= 0) {
break;
}
}

num ++;//周数增加
}

for (Entry<Integer, List<String>> entry : result.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
结果如下:
0:[2017-12-01, 2017-12-02, 2017-12-03]
1:[2017-12-04, 2017-12-05, 2017-12-06, 2017-12-07, 2017-12-08, 2017-12-09, 2017-12-10]
2:[2017-12-11, 2017-12-12, 2017-12-13, 2017-12-14, 2017-12-15, 2017-12-16, 2017-12-17]
3:[2017-12-18, 2017-12-19, 2017-12-20, 2017-12-21, 2017-12-22, 2017-12-23, 2017-12-24]
4:[2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2017-12-30, 2017-12-31]
5:[2018-01-01, 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-06, 2018-01-07]
6:[2018-01-08, 2018-01-09, 2018-01-10, 2018-01-11, 2018-01-12, 2018-01-13, 2018-01-14]
7:[2018-01-15, 2018-01-16, 2018-01-17, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21]
8:[2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25, 2018-01-26, 2018-01-27, 2018-01-28]
9:[2018-01-29, 2018-01-30, 2018-01-31, 2018-02-01]
郁闷的小生活 2018-08-24
  • 打赏
  • 举报
回复
引用 1 楼 tang_li 的回复:
感觉这个应该不难,我首先想到的方法是转化,将两个日期转化为格林威治时间,然后取他们的差,与35天的格林威治时间相比较,最后直接进行操作即可


我能将时间差算出,可是按周分组我弄不出来.这才过来这里请教.
郁闷的小生活 2018-08-24
  • 打赏
  • 举报
回复
引用 6 楼 u010970951 的回复:
[quote=引用 5 楼 u010970951 的回复:]
[quote=引用 4 楼 siqi1996 的回复:]
[quote=引用 3 楼 u010970951 的回复:]
按周分组的话需要用到Calendar日历类。将周数拿出来,然后循环七天,一直到结尾,
代码如下:
String a = "2017-12-01";

String b = "2018-02-02";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ad = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ad);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "-" + cl1.get(Calendar.DAY_OF_WEEK));


Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;//结果周数

//一直累加小的日期知道跟大的日期相同
while (true) {

if (cl2.compareTo(cl1) <= 0) {
break;
}

int count = 7;//每周天数

if (num == 0) {//第一次进入需要计算是星期几,从来算出剩下几天
//因为java中周末是算作一周开始是1,以此类推周1-6为2-7,而我们通常是从周一开始,所以我们减掉1,我们从周一开始算
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;
day = day == 0 ? 7 : day;//周末这时候就是0了,我们将周末改成7
count = 7 - day + 1;
}
if (!result.containsKey(num)) {//若是下一周星期一,则新建一个周数
result.put(num, new ArrayList<String>());
}

for (int i = 0; i < count; i++) {//将该周剩余天数添加进去
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_YEAR, 1);//添加一天
if (cl2.compareTo(cl1) <= 0) {
break;
}
}

num ++;//周数增加
}

for (Entry<Integer, List<String>> entry : result.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
结果如下:
0:[2017-12-01, 2017-12-02, 2017-12-03]
1:[2017-12-04, 2017-12-05, 2017-12-06, 2017-12-07, 2017-12-08, 2017-12-09, 2017-12-10]
2:[2017-12-11, 2017-12-12, 2017-12-13, 2017-12-14, 2017-12-15, 2017-12-16, 2017-12-17]
3:[2017-12-18, 2017-12-19, 2017-12-20, 2017-12-21, 2017-12-22, 2017-12-23, 2017-12-24]
4:[2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2017-12-30, 2017-12-31]
5:[2018-01-01, 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-06, 2018-01-07]
6:[2018-01-08, 2018-01-09, 2018-01-10, 2018-01-11, 2018-01-12, 2018-01-13, 2018-01-14]
7:[2018-01-15, 2018-01-16, 2018-01-17, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21]
8:[2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25, 2018-01-26, 2018-01-27, 2018-01-28]
9:[2018-01-29, 2018-01-30, 2018-01-31, 2018-02-01]


老哥,我要怎么测试呢?main方法我要调谁呀,我还得稍微修改一下,您给我的这个是对的,我要的最后格式是
time:[2017-12-01~2017-12-03,
2017-12-04~2017-12-10,
........
]
这种格式的,所以我在IDEA中把您的代码写上了,可是我不知道调谁了.........[/quote]

这是一个完整的方法 ,你可以直接写成一个方法使用,传入二个时间参数即可,因为我是main方法测试的,所以你自己写到单独方法里面就行了
至于格式,就更容易了,你遍历result的时候,把首尾取出来就好,给你一个完整方法
public static List<String> getResults(String a, String b) {
//前一部分代码,省略了
//下面遍历一下,取到你要的结果
List<String> time = new ArrayList<String>();
for (Entry<Integer, List<String>> entry : result.entrySet()) {
List<String> temp = entry.getValue();
String str = temp.get(0) + "~" + temp.get(temp.size() - 1);
time.add(str);
}

return result;
}
使用时 调用 getResults("2017-01-01", "2018-02-02")即可,注意方法上static根据实际情况选择添加与否
结果:
time:[2017-12-01~2017-12-03, 2017-12-04~2017-12-10, 2017-12-11~2017-12-17, 2017-12-18~2017-12-24, 2017-12-25~2017-12-31, 2018-01-01~2018-01-07, 2018-01-08~2018-01-14, 2018-01-15~2018-01-21, 2018-01-22~2018-01-28, 2018-01-29~2018-02-01]



[/quote]最后return time;打错字了、[/quote]

老哥,不用了,您的对,我的不知道哪里出问题了,现在又好了...
郁闷的小生活 2018-08-24
  • 打赏
  • 举报
回复
引用 6 楼 u010970951 的回复:
[quote=引用 5 楼 u010970951 的回复:]
[quote=引用 4 楼 siqi1996 的回复:]
[quote=引用 3 楼 u010970951 的回复:]
按周分组的话需要用到Calendar日历类。将周数拿出来,然后循环七天,一直到结尾,
代码如下:
String a = "2017-12-01";

String b = "2018-02-02";

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ad = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ad);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "-" + cl1.get(Calendar.DAY_OF_WEEK));


Map<Integer, List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;//结果周数

//一直累加小的日期知道跟大的日期相同
while (true) {

if (cl2.compareTo(cl1) <= 0) {
break;
}

int count = 7;//每周天数

if (num == 0) {//第一次进入需要计算是星期几,从来算出剩下几天
//因为java中周末是算作一周开始是1,以此类推周1-6为2-7,而我们通常是从周一开始,所以我们减掉1,我们从周一开始算
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;
day = day == 0 ? 7 : day;//周末这时候就是0了,我们将周末改成7
count = 7 - day + 1;
}
if (!result.containsKey(num)) {//若是下一周星期一,则新建一个周数
result.put(num, new ArrayList<String>());
}

for (int i = 0; i < count; i++) {//将该周剩余天数添加进去
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_YEAR, 1);//添加一天
if (cl2.compareTo(cl1) <= 0) {
break;
}
}

num ++;//周数增加
}

for (Entry<Integer, List<String>> entry : result.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
结果如下:
0:[2017-12-01, 2017-12-02, 2017-12-03]
1:[2017-12-04, 2017-12-05, 2017-12-06, 2017-12-07, 2017-12-08, 2017-12-09, 2017-12-10]
2:[2017-12-11, 2017-12-12, 2017-12-13, 2017-12-14, 2017-12-15, 2017-12-16, 2017-12-17]
3:[2017-12-18, 2017-12-19, 2017-12-20, 2017-12-21, 2017-12-22, 2017-12-23, 2017-12-24]
4:[2017-12-25, 2017-12-26, 2017-12-27, 2017-12-28, 2017-12-29, 2017-12-30, 2017-12-31]
5:[2018-01-01, 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-06, 2018-01-07]
6:[2018-01-08, 2018-01-09, 2018-01-10, 2018-01-11, 2018-01-12, 2018-01-13, 2018-01-14]
7:[2018-01-15, 2018-01-16, 2018-01-17, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21]
8:[2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25, 2018-01-26, 2018-01-27, 2018-01-28]
9:[2018-01-29, 2018-01-30, 2018-01-31, 2018-02-01]


老哥,我要怎么测试呢?main方法我要调谁呀,我还得稍微修改一下,您给我的这个是对的,我要的最后格式是
time:[2017-12-01~2017-12-03,
2017-12-04~2017-12-10,
........
]
这种格式的,所以我在IDEA中把您的代码写上了,可是我不知道调谁了.........[/quote]

这是一个完整的方法 ,你可以直接写成一个方法使用,传入二个时间参数即可,因为我是main方法测试的,所以你自己写到单独方法里面就行了
至于格式,就更容易了,你遍历result的时候,把首尾取出来就好,给你一个完整方法
public static List<String> getResults(String a, String b) {
//前一部分代码,省略了
//下面遍历一下,取到你要的结果
List<String> time = new ArrayList<String>();
for (Entry<Integer, List<String>> entry : result.entrySet()) {
List<String> temp = entry.getValue();
String str = temp.get(0) + "~" + temp.get(temp.size() - 1);
time.add(str);
}

return result;
}
使用时 调用 getResults("2017-01-01", "2018-02-02")即可,注意方法上static根据实际情况选择添加与否
结果:
time:[2017-12-01~2017-12-03, 2017-12-04~2017-12-10, 2017-12-11~2017-12-17, 2017-12-18~2017-12-24, 2017-12-25~2017-12-31, 2018-01-01~2018-01-07, 2018-01-08~2018-01-14, 2018-01-15~2018-01-21, 2018-01-22~2018-01-28, 2018-01-29~2018-02-01]



[/quote]最后return time;打错字了、[/quote]

老哥,我就是按着你的代码来的,为什么结果跟你的不一样呢,我的结果不是你给的那种,我跟你写的这个对了好几遍了.

public static void main(String[] args) throws ParseException {
System.out.println(getResults("2017-12-01","2018-02-02"));
}


public static List<String> getResults(String a, String b) throws ParseException {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date ab = sdf.parse(a);
Date bd = sdf.parse(b);

Calendar cl1 = Calendar.getInstance();
cl1.setTime(ab);
Calendar cl2 = Calendar.getInstance();
cl2.setTime(bd);

System.out.println(cl1.get(Calendar.YEAR) + "~" + cl1.get(Calendar.DAY_OF_WEEK));

Map<Integer,List<String>> result = new HashMap<Integer, List<String>>();

int num = 0;

while(true){
if(cl2.compareTo(cl1) <= 0){
break;
}

int count = 7;

if(num == 0){
int day = cl1.get(Calendar.DAY_OF_WEEK) - 1;

day = day == 0 ? 7 : day;
count = 7 - day + 1;
}

if(!result.containsKey(num)){
result.put(num,new ArrayList<String>());
}

for(int i=0;i < count;i++){
result.get(num).add(sdf.format(cl1.getTime()));
cl1.add(Calendar.DAY_OF_WEEK,1);
if(cl2.compareTo(cl1) <= 0){
break;
}
}

num++;
}

List<String> time = new ArrayList<String>();
for(Map.Entry<Integer,List<String>> entry:result.entrySet()){
List<String> temp = entry.getValue();
String str = temp.get(0) + "~" + temp.get(temp.size() - 1);
time.add(str);
}
return time;
}

结果是:2017~1
[2017-12-01~2017-12-01, 2017-12-02~2017-12-08, 2017-12-09~2017-12-15, 2017-12-16~2017-12-22, 2017-12-23~2017-12-29, 2017-12-30~2017-12-05, 2017-12-06~2017-12-12, 2017-12-13~2017-12-19, 2017-12-20~2017-12-26, 2017-12-27~2017-12-05, 2017-12-06~2017-12-12, 2017-12-13~2017-12-19, 2017-12-20~2017-12-26, 2017-12-27~2017-12-02, 2017-12-03~2017-12-09, 2017-12-10~2017-12-16, 2017-12-17~2017-12-23, 2017-12-24~2017-12-30, 2017-12-01~2017-12-07, 2017-12-08~2017-12-14, 2017-12-15~2017-12-21, 2017-12-22~2017-12-28, 2017-12-29~2017-12-04, 2017-12-05~2017-12-11, 2017-12-12~2017-12-18, 2017-12-19~2017-12-25, 2017-12-26~2017-12-02, 2017-12-03~2017-12-09, 2017-12-10~2017-12-16, 2017-12-17~2017-12-23, 2017-12-24~2017-12-30, 2017-12-31~2017-12-06, 2017-12-07~2017-12-13, 2017-12-14~2017-12-20, 2017-12-21~2017-12-27, 2017-12-28~2017-12-03, 2017-12-04~2017-12-10, 2017-12-11~2017-12-17, 2017-12-18~2017-12-24, 2017-12-25~2017-12-01, 2017-12-02~2017-12-08, 2017-12-09~2017-12-15, 2017-12-16~2017-12-22, 2017-12-23~2017-12-29, 2017-12-30~2017-12-05, 2017-12-06~2017-12-12, 2017-12-13~2017-12-19, 2017-12-20~2017-12-26, 2017-12-27~2017-12-03, 2017-12-04~2017-12-10, 2017-12-11~2017-12-17, 2017-12-18~2017-12-24, 2017-12-25~2017-12-31, 2018-12-01~2018-12-01]

我不知道是哪出问题了,我还在看我的代码和你的代码哪里不一样呢..
tang_li 2018-08-23
  • 打赏
  • 举报
回复
感觉这个应该不难,我首先想到的方法是转化,将两个日期转化为格林威治时间,然后取他们的差,与35天的格林威治时间相比较,最后直接进行操作即可

50,535

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧