tags:

views:

163

answers:

2
+1  Q: 

Convert to date

Hi,

I'm trying to create number of Evenement instances and set the 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 seem to get it to work,

Any ideas?

+3  A: 

You may 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 the months are zero-based, so July is 6.

R. Bemrose
+2  A: 

Beware of the locale used for the date formatter (default can be Locale.ENGLISH is your OS is set that way, meaning the year is at the end, not at the beginning of the string)

You need to be sure to have a formatter build as:

formatter = new SimpleDateFormat("yyyy/MM/DD");
VonC
I'm pretty sure the default for the date formatter is whatever you've got your operating system set to.
Paul Tomblin