是否可以列出给定两个日期范围的所有星期/日期,例如:
日期从 1/1/2013 到 1/1/2020 结果将是:
答案 0 :(得分:0)
请尝试使用数周的时间(检查是否可以优化)。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to provide month range dynamically
Calendar calendar = Calendar.getInstance();
Date minDate = calendar.getTime();
calendar.add(Calendar.MONTH, 5); // current month + 5 months calendar
Date maxDate = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String startDate = dateFormat.format(minDate);
String endDate = dateFormat.format(maxDate);
List<Date> dates = getDates(startDate, endDate); // to get dates between range
int prevIdentifier = 0;
int identifier;
String initDate, finalDate;
List<WeekDay> weekDays = getListOfWeeksFromListOfDates(dates);
SimpleDateFormat dformatter = new SimpleDateFormat("dd");
SimpleDateFormat yformatter = new SimpleDateFormat("yyyy");
initDate = dformatter.format(weekDays.get(0).getDate());
finalDate = dformatter.format(weekDays.get(0).getDate());
String outputData = "";
for (WeekDay weekDay : weekDays) {
identifier = Integer.parseInt(weekDay.getWeekIdentifier()); // this value will be same for all days in same week
if (prevIdentifier != 0 && identifier != prevIdentifier) {
if (outputData.equalsIgnoreCase(""))
outputData += initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
else
outputData += " * " + initDate + "-" + finalDate + "," + yformatter.format(weekDay.getDate());
initDate = dformatter.format(weekDay.getDate());
} else {
finalDate = dformatter.format(weekDay.getDate());
}
prevIdentifier = identifier;
}
System.out.println("OUTPUT DATA :" + outputData);
}
public List<WeekDay> getListOfWeeksFromListOfDates(List<Date> listOfDates) {
List<WeekDay> listOfWeeks = new ArrayList<>();
WeekDay weekDay;
for (Date date : listOfDates) {
weekDay = new WeekDay(date, new SimpleDateFormat("w").format(date));
listOfWeeks.add(weekDay);
}
return listOfWeeks;
}
public class WeekDay {
Date date;
String weekIdentifier;
public WeekDay(Date Date, String WeekIdentifier) {
this.date = Date;
this.weekIdentifier = WeekIdentifier;
}
public Date getDate() {
return date;
}
public String getWeekIdentifier() {
return weekIdentifier;
}
}
private static List<Date> getDates(String dateString1, String dateString2) {
ArrayList<Date> dates = new ArrayList<Date>();
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = df1.parse(dateString1);
date2 = df1.parse(dateString2);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
while (!cal1.after(cal2)) {
dates.add(cal1.getTime());
cal1.add(Calendar.DATE, 1);
}
return dates;
}
答案 1 :(得分:0)
我将LocalDate
用作开始日期(例如1/1/2013),将1用作结束日期(1/1/2020)。为了表示所需的时间段(周,月或年),我可以使用适当的ChronoUnit
常数,也可以使用Period
来更灵活。提到的类来自java.time,这是现代的Java日期和时间API。一个非常简单的循环将遍历您的开始日期(1月1日,1月8日,1月15日等)。从每个开始日期(第一个开始日期除外)中减去1即可得出结束日期(1月8日减去1天得出1月7日,依此类推)。快乐的编码。正如可怕的袋熊所说,如果您有任何问题,请在此处提出另一个更好的问题。
java.time
吗?是的,java.time
在Android设备上运行良好。它只需要至少 Java 6 。
org.threeten.bp
和子包中导入日期和时间类。java.time
。LocalDate
,ChronoUnit
和Period
的文档。