Java中的日期数组 [英] Array of Dates in Java
问题描述
与String和Int相同的方式,您只需在其中放置不同的类型:
Date [] dates = {
new Date(),
new Date()
};
声明一个大小为二的数组,两个日期。
您还可以使用空值初始化:
Date [] dates = new Date [2];
或添加更多重要值:
Date [] dates = {
getDateFromString(25/11/2009),
getDateFromString(24/12/2009)
};
....
public Date getDateFromString(String s){
Date result = ... //解析字符串设置值等
返回结果;
}
编辑
...但是,无论如何,您可以在getDateFromString方法中完成您正在做什么?
当然,我最初没有因为我的观点来表现,你可以把任何类型Date的东西放在那里。
您只需使用 SimpleDateFormate.parse()方法(继承自DateFormat类)
simpleDateFormatInstance.parse(24/12/2009); //返回圣诞节2009.
这是一个完整的工作示例:
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date
import static java.lang.System.out;
public class DateArrayTest {
private static final SimpleDateFormat dateFormat
= new SimpleDateFormat(dd / MM / yyyy);
private static final Date invalidDate = new Date(0);
//测试从字符串创建日期。
public static void main(String [] args){
Date [] randomDates = {
fromString(01/01/2010),// new year
fromString( 16/09/2010),// 200年Mex indepence
fromString(21/03/2010),//嗯,明年春天?
fromString(this / should / fail),// invalid date。
};
for(Date date:randomDates){
print(date);
}
}
/ **
*根据给定的字符串规范创建一个日期。
*日期格式必须为dd / MM / yyyy ie。
* 2009年12月24日将是:24/12/2009
* @return invalidDate如果格式无效。
* /
private static final DateString(String spec){
try {
return dateFormat.parse(spec);
} catch(ParseException dfe){
return invalidDate;
}
}
private static final void print(Date date){
if(date == invalidDate){
out.println(Invalid date);
} else {
out.println(dateFormat.format(date));
}
}
}
I know how to create an array of strings or integers, but how does one create an array of dates :/
The same way you do for String and Int , you just place different types inside:
Date [] dates = {
new Date(),
new Date()
};
Declared an array of size two with two dates.
You can also initialize with null values:
Date [] dates = new Date[2];
Or add more significant values:
Date [] dates = {
getDateFromString("25/11/2009"),
getDateFromString("24/12/2009")
};
....
public Date getDateFromString( String s ) {
Date result = ...// parse the string set the value etc.
return result;
}
EDIT
...but is there anyway you can finish up what you were doing in the getDateFromString method?
Sure, I didn't initially because my point was to show, that you could put anything that is of type "Date" there.
You just have to use the SimpleDateFormate.parse() method ( inherited from DateFormat class )
simpleDateFormatInstance.parse( "24/12/2009" ); // returns christmas 2009.
Here's a complete working sample:
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import static java.lang.System.out;
public class DateArrayTest {
private static final SimpleDateFormat dateFormat
= new SimpleDateFormat("dd/MM/yyyy");
private static final Date invalidDate = new Date(0);
// test creating a date from a string.
public static void main( String [] args ) {
Date [] randomDates = {
fromString("01/01/2010"), // new year
fromString("16/09/2010"), // 200 yrs Mex indepence
fromString("21/03/2010"), // uhhmm next spring?
fromString("this/should/fail"), // invalid date.
};
for( Date date: randomDates ) {
print( date );
}
}
/**
* Creates a date from the given string spec.
* The date format must be dd/MM/yyyy ie.
* 24 december 2009 would be: 24/12/2009
* @return invalidDate if the format is invalid.
*/
private static final Date fromString( String spec ) {
try {
return dateFormat.parse( spec );
} catch( ParseException dfe ) {
return invalidDate;
}
}
private static final void print( Date date ) {
if( date == invalidDate ) {
out.println("Invalid date");
} else {
out.println( dateFormat.format( date ) );
}
}
}
这篇关于Java中的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!