Convert to date

I am trying to create a number of Evenement instances and set a date for them:

   for (int i=2004; i<2009; i++){
           evenementen.add(new Evenement("Rock Werchter", "Rock", "Werchter", 200000,
                           (Date)formatter.parse(i+"/07/03")));

      

But I can't get it to work,

Any ideas?

+1


source to share


2 answers


You might want to use Calendar to create your dates.

for (int i=2004; i<2009; i++) {
    Calendar cal = Calendar.getInstance();
    cal.clear();
    // Calendar.JULY may be different depending on the JDK language
    cal.set(i, Calendar.JULY, 3); // Alternatively, cal.set(i, 6, 3); 
    evenementen.add(new Evenement("Rock Werchter", "Rock", "Werchter", 200000,
            cal.getTime()));
}

      



Note that months are zero-based, so July is 6.

+3


source


Beware of the language used to format the date ( Locale.ENGLISH

your OS may be the default , i.e. the year is at the end, not the beginning of the line)

You must be sure that you are creating the formatting:



formatter = new SimpleDateFormat("yyyy/MM/DD");

      

+2


source







All Articles