有一个起始日期和一个截止日期比如: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的
java 两个日期之间分组问题
答案:3 悬赏:60 手机版
解决时间 2021-03-07 07:32
- 提问者网友:别再叽里呱啦
- 2021-03-06 16:26
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-03-06 17:16
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.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);
}
}
全部回答
- 1楼网友:洒脱疯子
- 2021-03-06 19:38
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;
}
}
- 2楼网友:妄饮晩冬酒
- 2021-03-06 18:28
直接用sql语句
select count(id) as 数量,day(date) as 生产日期 from table group by day(date) order by day(czrq)
如果库里不止一个月的数据,那么再加个where条件
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯