如何输出日期数组列表?
How can I output an arrraylist of dates?
我是 java 的新手。我的 objective 是让用户以“dd/MM/yyyy”格式输入日期,并且我能够将这些日期添加到日历数组列表,然后显示包含所有日期的数组。
ArrayList<Calendar> dob = new ArrayList<Calendar>();
Calendar dobs = Calendar.getInstance();
//traveller dobs
for (int count = 0; count < tAmount; count++)
{
System.out.println("---Please write the dates in the following format dd/mm/yyyy--- ");
System.out.println("Write dates in same order as names!- ");
System.out.println("Day such as '09':");
day = keyboard.nextInt();
System.out.println("Month such as '11':");
month = keyboard.nextInt();
System.out.println("Year such as '1998':");
year = keyboard.nextInt();
//format & add to arraylist
dobs.set(year, month, day);
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
df.format(dobs);
dob.add(dobs);
};
System.out.println(id +"\n"+ name+"\n" + tAmount+"\n" + travellers+"\n" +dob+"\n" + dateCreated);
我尝试在这里和其他地方寻找答案,但找不到我要找的东西,而且我确实研究过的线索让我更加困惑。这是我 运行 此代码时得到的输出:
如果我删除日期格式的东西,我会得到这个:
...
我正在寻找的是这样的东西:
[20-10-1999,12-03-1998,...]
我不太了解如何正确使用日历,希望得到任何帮助。
tl;博士
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
List < LocalDate > dates =
List
.of( "23/01/2021" , "17/02/2021" )
.stream()
.map(
( String s ) -> LocalDate.parse( s , formatter )
)
.toList();
System.out.println( "dates = " + dates );
[2021-01-23, 2021-02-17]
详情
切勿使用 Date
和 SimpleDateFormat
类。这些在几年前被 JSR 310 中定义的现代 java.time 类 所取代。
List < String > inputs = List.of( "23/01/2021", "17/02/2021" ) ;
List< LocalDate > dates = new ArrayList<>( inputs.size() ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
for( String input : inputs )
{
LocalDate date = LocalDate.parse( input , f ) ;
dates.add( date ) ;
}
如果您将年、月和日组件作为单独的数字,请使用其他工厂方法。
LocalDate.of( y , m , d )
要显示 LocalDate
个对象,让 java.time 自动本地化。
Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( locale) ;
for( LocalDate date : dates )
{
String output = date.format( f ) ;
…
}
或者您可以编写另一种自定义格式,如第一个代码片段所示,使用 DateTimeFormatter.ofPattern
。
我是 java 的新手。我的 objective 是让用户以“dd/MM/yyyy”格式输入日期,并且我能够将这些日期添加到日历数组列表,然后显示包含所有日期的数组。
ArrayList<Calendar> dob = new ArrayList<Calendar>();
Calendar dobs = Calendar.getInstance();
//traveller dobs
for (int count = 0; count < tAmount; count++)
{
System.out.println("---Please write the dates in the following format dd/mm/yyyy--- ");
System.out.println("Write dates in same order as names!- ");
System.out.println("Day such as '09':");
day = keyboard.nextInt();
System.out.println("Month such as '11':");
month = keyboard.nextInt();
System.out.println("Year such as '1998':");
year = keyboard.nextInt();
//format & add to arraylist
dobs.set(year, month, day);
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
df.format(dobs);
dob.add(dobs);
};
System.out.println(id +"\n"+ name+"\n" + tAmount+"\n" + travellers+"\n" +dob+"\n" + dateCreated);
我尝试在这里和其他地方寻找答案,但找不到我要找的东西,而且我确实研究过的线索让我更加困惑。这是我 运行 此代码时得到的输出:
如果我删除日期格式的东西,我会得到这个:
我正在寻找的是这样的东西: [20-10-1999,12-03-1998,...]
我不太了解如何正确使用日历,希望得到任何帮助。
tl;博士
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
List < LocalDate > dates =
List
.of( "23/01/2021" , "17/02/2021" )
.stream()
.map(
( String s ) -> LocalDate.parse( s , formatter )
)
.toList();
System.out.println( "dates = " + dates );
[2021-01-23, 2021-02-17]
详情
切勿使用 Date
和 SimpleDateFormat
类。这些在几年前被 JSR 310 中定义的现代 java.time 类 所取代。
List < String > inputs = List.of( "23/01/2021", "17/02/2021" ) ;
List< LocalDate > dates = new ArrayList<>( inputs.size() ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
for( String input : inputs )
{
LocalDate date = LocalDate.parse( input , f ) ;
dates.add( date ) ;
}
如果您将年、月和日组件作为单独的数字,请使用其他工厂方法。
LocalDate.of( y , m , d )
要显示 LocalDate
个对象,让 java.time 自动本地化。
Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( locale) ;
for( LocalDate date : dates )
{
String output = date.format( f ) ;
…
}
或者您可以编写另一种自定义格式,如第一个代码片段所示,使用 DateTimeFormatter.ofPattern
。