Array of dates in Java

I know how to create an array of strings or integers, but how to create an array of dates: /

+7
java date arrays
source share
5 answers

Just like for String and Int, you just place different types inside:

Date [] dates = { new Date(), new Date() }; 

An array of size two with two dates has been declared.

You can also initialize null values:

  Date [] dates = new Date[2]; 

Or add more meaningful 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 still you can finish what you did in the getDateFromString method?

Of course, I didn’t initially, because I wanted to show that you can put something like β€œDate” there.

You just need to use the SimpleDateFormate.parse () method (inherited from the DateFormat class)

  simpleDateFormatInstance.parse( "24/12/2009" ); // returns christmas 2009. 

Here's the full 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 ) ); } } } 
+21
source share

you can use java.util.Date array (API docs here )

 Date[] dates = new Date[] { new Date(), new Date(), }; 

You can create an array of any type of object in java - all reference and primitive types

+4
source share

Or you can use the Collections API and Calendar class,

 import java.util.*; List<Calendar> dates = new ArrayList<Calendar>(5); // initial size dates.add( Calendar.getInstance() ); 
+1
source share

You meant entering an array of dates. This code will help ..

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; public class Datinput { public static void main(String args[]) { int n; ArrayList<String> al = new ArrayList<String>(); Scanner in = new Scanner(System.in); n = in.nextInt(); String da[] = new String[n]; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); sdf.setLenient(false); Date date[] = new Date[n]; in.nextLine(); for (int i = 0; i < da.length; i++) { da[i] = in.nextLine(); } for (int i = 0; i < da.length; i++) { try { date[i] = sdf.parse(da[i]); } catch (ParseException e) { e.printStackTrace(); } } in.close(); } } 
+1
source share

You can consider (and it’s not reality, but it kind of works like this) that primitives are something like this (I get to reality later ... so keep reading):

int.7, int.42 (will not compile), where int is the class (this is not the case), and 7 and 42 are public static final variables (they are not).

and that the lines are like this:

String "Hello", String. "world" (will not compile), where String is the class (it is) and "Hello" and "world" are public static final variables (they are not).

If my fake reality were true, you would need something like:

 // again, won't compile. public class int { public static final int 7 = new int(7); public static final int 42 = new int(42); private final ??? data; public int(??? val) { data = val; } } 

and

 // also will not compile public class String { public final String "Hello" = new String("Hello); public final String "world" = new String("world); private final ??? data; public String(final ??? val) { data = val; } } 

now you make an array like (still not compiling):

 int[] array = new int[] { int.7, int.42 }; String[] array = new String[] {String."Hello", String."world" }; 

In the case of String, my alternate reality would be very stupid, since the String class cannot know in advance every possible String (for int it is possible).

So, we will get rid of public static final variables in String and do it instead:

 String[] array = new String[] { new String("Hello"), new String("world") }; 

Now to reality:

When the java compiler, when it sees "Hello" or "world", it does something similar to "new String (" Hello ")" - it's a little smarter, so if you have "Hello" 20 times to a file, which contains only one copy (and some other things).

When you speak:

 new int[100]; you get an array of 100 ints all set to 0. new String[100]; you get an array of 100 Strings all pointing to null. new Data[100]; you get 100 Dates all pointing to null. 

Since the String and Date strings point to null, you need to allocate a new object for each of them. The reason you don't need to say β€œnew” with String is because the compiler treats it specifically. The reason you don't need to say "new" with int is because it is a primitive, not an object.

So, a simple answer to your question: you need to assign a new date for each element of the array :-)

0
source share

All Articles