永发信息网

java 两个日期之间分组问题

答案:3  悬赏:60  手机版
解决时间 2021-03-07 07:32
有一个起始日期和一个截止日期比如:2010-06-28 -- 2013-09-03
分组成这样
2010-06-28 -- 2011-06-27
2011-06-28 -- 2012-06-27
2012-06-28 -- 2013-06-27
像这样分割成几对然后取系统当前日期看是属于哪一组里面的把那一组日期取出来。
麻烦哪位大侠帮忙写下最好是java的
最佳答案
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {


public static void main(String[] args) {

try {
List<String[]> dateList = groupDate("2010-06-28","2013-09-03");
for(String[] dateArr : dateList){
System.out.println(dateArr[0] + " -- " + dateArr[1]);
}
System.out.println("----------------------------");

Calendar today = Calendar.getInstance();
today.setTimeInMillis(System.currentTimeMillis());

String[] dateGroup = getDateGroup(dateList, today);
System.out.println(dateGroup[0] + " -- " + dateGroup[1]);

} catch (ParseException e) {
System.out.println("日期格式不对!正确格式:yyyy-MM-dd");
e.printStackTrace();
}
}

private static String[] getDateGroup(List<String[]> dateList, Calendar today) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
for(String[] dateArr : dateList){
Date start_d = df.parse(dateArr[0]);
Date end_d = df.parse(dateArr[1]);

Calendar start_c = Calendar.getInstance();
Calendar end_c = Calendar.getInstance();

start_c.setTime(start_d);
end_c.setTime(end_d);

if(today.after(start_c) && today.before(end_c)){
return dateArr;
}
}
return null;
}

public static List<String[]> groupDate(String start, String end) throws ParseException
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date start_d = df.parse(start);
Date end_d = df.parse(end);

Calendar start_c = Calendar.getInstance();
Calendar end_c = Calendar.getInstance();

start_c.setTime(start_d);
end_c.setTime(end_d);

if(start_c.after(end_c)){
start_c.setTime(end_d);
end_c.setTime(start_d);
}

List<String[]> result = new ArrayList<String[]>();
Calendar temp = null;
while(start_c.before(end_c)){
String[] groupArr = new String[2];

temp = (Calendar) start_c.clone();
start_c.roll(Calendar.YEAR, true);
start_c.roll(Calendar.DAY_OF_YEAR, false);

if(start_c.after(end_c)){
groupArr[0] = calendar2String(temp);
groupArr[1] = calendar2String(end_c);
}
else{
groupArr[0] = calendar2String(temp);
groupArr[1] = calendar2String(start_c);
}

result.add(groupArr);

start_c.roll(Calendar.DAY_OF_YEAR, true);
}

return result;
}

public static String calendar2String(Calendar c){
return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH)+1) + "-" + c.get(Calendar.DATE);
}
}
全部回答
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; public class DateCheck { static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] ags) { ArrayList<Calendar> start = new ArrayList<Calendar>(); ArrayList<Calendar> end = new ArrayList<Calendar>(); Calendar dateToCheck; start.add(getStartDate()); end.add(getEndDate()); dateToCheck = getDateToCheck(); groupingDates(start, end); int check = checkDateInGroups(dateToCheck, start, end); if (check == -1) { System.out.println("\nGiven date is not found in all periods."); } else { System.out.println("\nGiven date is in period " + check); } } private static Calendar getStartDate() { Calendar result = Calendar.getInstance(); result.set(2010, Calendar.JUNE, 28); System.out.print("From " + sdf.format(result.getTime())); return result; } private static Calendar getEndDate() { Calendar result = Calendar.getInstance(); result.set(2013, Calendar.SEPTEMBER, 3); System.out.println(" to " + sdf.format(result.getTime())); return result; } private static Calendar getDateToCheck() { Calendar result = Calendar.getInstance(); System.out.println("Date to check is: " + sdf.format(result.getTime()) + "\n"); return result; } private static void groupingDates( ArrayList<Calendar> start, ArrayList<Calendar> end) { boolean finished = false; int position = 0; while (!finished) { Calendar startDate = start.get(start.size() - 1); Calendar endDate = end.get(start.size() - 1); int year = startDate.get(Calendar.YEAR) + 1; int month = startDate.get(Calendar.MONTH); int date = startDate.get(Calendar.DATE) - 1; Calendar nextEndDate = Calendar.getInstance(); nextEndDate.set(year, month, date); if (nextEndDate.compareTo(endDate) < 0) { Calendar nextStartDate = Calendar.getInstance(); nextStartDate.set(year, month, date + 1); start.add(nextStartDate); end.add(position++, nextEndDate); } else { finished = true; } } for (int i = 0; i < start.size(); i++) { System.out.println("Period " + i + " : From " + sdf.format(start.get(i).getTime()) + " to " + sdf.format(end.get(i).getTime())); } } private static int checkDateInGroups(Calendar date, ArrayList<Calendar> start, ArrayList<Calendar> end) { for (int i = 0; i < start.size(); i++) { if (date.compareTo(start.get(i)) >= 0 && date.compareTo(end.get(i)) <= 0) { return i; } } return -1; } }
直接用sql语句 select count(id) as 数量,day(date) as 生产日期 from table group by day(date) order by day(czrq) 如果库里不止一个月的数据,那么再加个where条件
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
某一个DNA分子的碱基总数中,腺嘌呤为200个,
一个四位数加上6后能被6整除,减去5后能被能
肯德基中华北餐厅我想知道这个在什么地方
吸出来奶常温保存多久
行政机关委托其他行政机关实施行政许可的,委
紫凝这是怎么了
求oracle长用和特有的sql语句
口袋妖怪除了XY 还有哪个版本能抓XY的精灵?
爱心水果超市这个地址在什么地方,我要处理点
唱吧里不想让歌曲上榜,怎么设置
当鱼损失了胸鳍、腹鳍的时候,鱼会出现CA. 只
初中毕业(16岁)可以考北舞吗
含机的成语有哪些
水貂皮草公貂和母貂的区别?
哈发怎么去啊,有知道地址的么
推荐资讯
不用烤箱能做披萨吗
守望先锋源氏坐在堡垒上面的是什么宣传片
怎么用pr消除视频logo
阅读法兰西第一帝国极盛时期的欧洲图。回答:
20世纪30年代初,美国的经济状况
热塑性弹性体tpe的密度是多少
CH≡CH 汉子名是什么
酷跑锦标赛黄金特工3有多少奖励
达州跟遂宁那个离重庆近,近的那个,用火车回
饥荒联机怎么保存进度
探探好玩吗?
开一家汗蒸房多少钱
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?