作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我怀疑如何在 java (Android) 中迭代这些日子。要求是我显示整周的日期。
注意:“以用户选择的日期开始”。
例如:如果日期为 29-10-2017,则输出将为 29-10-2017、30-10-2017、31-10-2017、1-11-2017、2-11-2017、3- 2017 年 11 月、2017 年 11 月 4 日。这是整整一周。
当日期在该月内时,我能够得到这个结果,但是当日期超过该月或年时,我无法解决它们。请帮忙,我该如何解决这个问题。
下面是我为此使用的代码 fragment :
Calendar startCal = Calendar.getInstance();
startCal.setTime(new Date(Long.MAX_VALUE));
startCal.setTimeInMillis(minDate.getDateInMillis());
Calendar endCal = Calendar.getInstance();
endCal.setTime(new Date(Long.MAX_VALUE));
endCal.setTimeInMillis(maxDate.getDateInMillis());
// Add all weekend days within range to disabled days
for (int i = 0; i < 7; i++) {
while (startCal.before(endCal) || startCal.equals(endCal) || (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)) {
if (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
|| startCal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY
|| startCal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY
|| startCal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY
|| startCal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
int key = Utils.formatDisabledDayForKey(startCal.get(Calendar.YEAR),
startCal.get(Calendar.MONTH), startCal.get(Calendar.DAY_OF_MONTH));
disabledDays.put(key, new MonthAdapter.CalendarDay(startCal));
}
startCal.add(Calendar.DATE, 1);
}
}
int daysInMonth = startCal.getActualMaximum(Calendar.DAY_OF_MONTH); // 31
为了将它们放入一些 TextView 中,我使用下面的代码 fragment :
String date = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
arr1 = new String[7];
datepicker_dailog.setText(date);
// String input = datepicker_dailog.getText().toString();
Log.e(TAG, "Date value1 is:--- " + date);
GregorianCalendar cal=new GregorianCalendar();
if (cal.isLeapYear(year)) {
dayOfMonth++;
}
String ar[] = date.split("[-]");
int day = Integer.parseInt(ar[0]);
int month = Integer.parseInt(ar[1]);
int year1 = Integer.parseInt(ar[2]);
Log.e(TAG, "new value is "+ day + " " + month + " "+ year1);
for(int j = 0; j < 7 ; j++) {
date_exp(day, month, year1);
date = day + "-"+month+"-"+year1;
arr1[j] = date;
Log.e(TAG, "loop is :--- "+ arr1[j]);
Log.e(TAG, "value in loop is :--- "+ day + " " + month + " "+ year1);
day++;
}
Log.e(TAG, "updated value is "+ day + " " + month + " "+ year1);
我引用这个库来补充带有日期的日历:
https://github.com/code-troopers/android-betterpickers
这里我正在修改并存储 TextView 中的日期值,请检查一次:
@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
String date = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
arr1 = new String[7];
datepicker_dailog.setText(date);
// String input = datepicker_dailog.getText().toString();
Log.e(TAG, "Date value1 is:--- " + date);
GregorianCalendar cal=new GregorianCalendar();
if (cal.isLeapYear(year)) {
dayOfMonth++;
}
String ar[] = date.split("[-]");
int day = Integer.parseInt(ar[0]);
int month = Integer.parseInt(ar[1]);
int year1 = Integer.parseInt(ar[2]);
Log.e(TAG, "new value is "+ day + " " + month + " "+ year1);
for(int j = 0; j < 7 ; j++) {
date = day + "-"+month+"-"+year1;
arr1[j] = date;
Log.e(TAG, "loop is :--- "+ arr1[j]);
Log.e(TAG, "value in loop is :--- "+ day + " " + month + " "+ year1);
day++;
}
Log.e(TAG, "updated value is "+ day + " " + month + " "+ year1);
//------------------------------------------------------------------------------------------
listView=(ListView)findViewById(R.id.addtime_container);
dataModels= new ArrayList<>();
try {
for (int i = 0; i < 7; i++) {
dataModels.add(new Model_Addtime(arr1[i]));
adapter = new Adapter_addtime(dataModels, getApplicationContext());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Model_Addtime dataModel = dataModels.get(position);
Snackbar.make(view, dataModel.getDate_text() + "\n", Snackbar.LENGTH_LONG).setAction("No action", null).show();
}
});
}
} catch (NumberFormatException num){
num.printStackTrace(); num.getCause(); num.getMessage();
}
}
最佳答案
您应该使用此代码
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
cal.add(Calendar.DATE, 2);
cal.add(Calendar.DATE, 3);
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(df.format(cal.getTime());
它将自动处理月份更改和年份更改。
在 java 8 中,您可以像这样使用流 example
List<LocalDate> daysRange = Stream.iterate(startDate, date -> date.plusDays(1)).limit(numOfDays).collect(toList());
关于java - 如何在java中迭代日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47507556/
我是一名优秀的程序员,十分优秀!