日历类型元素在列表中
我必须创建一个打印日期列表(年,月,日)的程序,直到用户选择日期( end_date
,后来转换为end_cal
)。
例如,如果今天是2017-09-30 Saturday
和用户输入的日期2017-10-30
,程序必须打印出这些日期:
2017-09-30,2017-10-07,2017-10-14,2017-10-21,2017-10-28。
问题:
当我尝试打印它时,输出只是同一日期的一组重复。
public class Weekdays {
static Scanner input = new Scanner(System.in);
static Calendar temp_cal = Calendar.getInstance(); //temporary calendar object. it's value is being chaged in the process
static Calendar start_cal = Calendar.getInstance(); // current day when the program is executed
static Calendar end_cal = Calendar.getInstance(); //end date that the user inputs
static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
public static boolean date_validation(String date){ //partial validation: whether the input date is in a correct format
Date test_date;
try {
test_date = format.parse(date);
}catch (ParseException e){
return false;
}
return true;
}
//created list of dates that are of the same day of the week (for example all Sundays)
static private List<Calendar> getListOfDates(){
List<Calendar> dates = new ArrayList<>();
while (!((temp_cal.get(Calendar.YEAR)>= end_cal.get(Calendar.YEAR))&&(temp_cal.get(Calendar.MONTH) >= end_cal.get(Calendar.MONTH))&&(temp_cal.get(Calendar.DAY_OF_YEAR) >= end_cal.get(Calendar.DAY_OF_YEAR))))
{
temp_cal.add(Calendar.DATE, 7);
dates.add(temp_cal); }
return dates;
}
static private void printListOfDates(List<Calendar> dates){
System.out.println(Arrays.toString(dates.toArray()));
}
public static void main(String[] str) throws ParseException{
String end_date = input.next();
while(!(date_validation(end_date))){
end_date = input.next();
}
end_cal.setTime(format.parse(end_date));
printListOfDates(getListOfDates());
}
输入: 2018/01/01
输出(仅复制一个示例,整个输出只是其中的几个重复项):
java.util.GregorianCalendar中的[时间= 1515233525518,areFieldsSet = TRUE,areAllFieldsSet = TRUE,宽松= TRUE,区= sun.util.calendar.ZoneInfo [ID = “欧洲/赫尔辛基”,偏移量= 7200000,dstSavings = 3600000,useDaylight =真,过渡= 118,lastRule = java.util.SimpleTimeZone中[ID =欧洲/赫尔辛基,偏移= 7200000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 2,startMonth = 2,朝九特派= -1,startDayOfWeek = 1,开始时间= 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endday指定= -1,一个endDayOfWeek = 1,结束时间= 3600000,endTimeMode = 2]],Firstdayofweek可= 2,minimalDaysInFirstWeek = 4,ERA = 1, YEAR = 2018,MONTH = 0,WEEK_OF_YEAR = 1,WEEK_OF_MONTH = 1,DAY_OF_MONTH = 6,DAY_OF_YEAR = 6,DAY_OF_WEEK = 7,DAY_OF_WEEK_IN_MONTH = 1,AM_PM = 1,HOUR = 0,HOUR_OF_DAY = 12,MINUTE = 12,SECOND = 5,微差= 518,ZONE_OFFSET = 7200000,DST_OFFSET = 0]]
我不确定这是否是适合您的格式(我将把它作为您的一项任务),但您可以使用LocalDate做类似下面的操作:
import java.util.Scanner;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate endDate;
while(true){
System.out.print("Enter the endDate in the format of yyyy/MM/dd:");
String date = scanner.next();
try {
endDate = LocalDate.parse(date, formatter);
break;
} catch (Exception e) {
System.err.println("ERROR: Please input the date in the correct format");
}
}
System.out.println("Below are the days between the current day and " + endDate);
printDaysBetween(endDate);
}
public static void printDaysBetween(LocalDate end){
LocalDate start = LocalDate.now();
while (!start.isAfter(end)) {
System.out.println(start);
start = start.plusDays(7);
}
}
}
用法示例:
Enter the endDate in the format of yyyy/MM/dd: 2017-10-30
ERROR: Please input the date in the correct format
Enter the endDate in the format of yyyy/MM/dd: 2017/10/30
Below are the days between the current day and 2017-10-30
2017-09-30
2017-10-07
2017-10-14
2017-10-21
2017-10-28
第一个问题是你打印结果的方式:
System.out.println(Arrays.toString(dates.toArray()));
如果你想格式化日期,你需要使用格式化程序。
private static SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
static private void printListOfDates(List<Calendar> dates){
for (Calendar date : dates) {
System.out.println(outputFormat.format(date));
}
}
第二个问题是,您似乎在getListOfDates的循环中重复使用了相同的temp_cal对象。 这会导致您的列表包含同一个Calendar对象的多个副本(多次修改)。 您需要为循环中的每个迭代创建一个新实例。
import java.util.Date;
import java.time.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class days
{
public static void main (String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter Dates (Start(yyyy/mm/dd)-End)");
//example 2017 1 5 -> 5 jan 2017
getDates(Integer.parseInt(input.next()),Integer.parseInt(input.next()),
Integer.parseInt(input.next()),Integer.parseInt(input.next()),
Integer.parseInt(input.next()),Integer.parseInt(input.next()));
/*the input needed example
* 2017
* 1
* 1 -Start date
* 2017
* 2
* 10 -End date
*/
}
public static void getDates(int pYearStart, int pMonthStart, int pDayStart,
int pYearEnd, int pMonthEnd, int pDayEnd){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd");
int year = pYearStart;
int month = pMonthStart-1;
int day = pDayStart;
Calendar start = new GregorianCalendar(year,month,day);
int yearEnd = pYearEnd;
int monthEnd = pMonthEnd-1;
int dayEnd = pDayEnd;
Calendar end = new GregorianCalendar(yearEnd,monthEnd,dayEnd);
System.out.print("Start Date: ");
System.out.println(sdf.format(start.getTime()));
System.out.print("End Date: ");
System.out.println(sdf.format(end.getTime()));
System.out.println("");
System.out.println("All Dates:");
boolean sameDate = false;
int amount = 0;
do{
System.out.println(sdf.format(start.getTime()));
start.add(Calendar.DAY_OF_MONTH, 7);
amount++;
if(start.get(Calendar.DAY_OF_MONTH) >= end.get(Calendar.DAY_OF_MONTH) &&
start.get(Calendar.MONTH) == end.get(Calendar.MONTH)) {
sameDate = true;}
}while(sameDate != true);
System.out.println("No of dates: "+amount);
}
}
我认为这就是你想要的,我没有使用列表,只是改变它,只要你喜欢。
链接地址: http://www.djcxy.com/p/401.html