1

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 Answers3

3

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.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
2

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 (at the time of writing, 2008, Java6, as in this answer):

formatter = new SimpleDateFormat("yyyy/MM/DD");
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I'm pretty sure the default for the date formatter is whatever you've got your operating system set to. – Paul Tomblin Nov 14 '08 at 14:24
  • The code is wrong. I tried parsing `2004/07/03` (the first string from the question) with your formatter, expected 3 July 2004, but got `Sat Jan 03 00:00:00 CET 2004`. – Ole V.V. May 30 '21 at 12:04
  • 1
    @OleV.V. 13 years later, something might have changed indeed. I was using Java 6 at the time. Let me read your own answer https://stackoverflow.com/a/67742568/6309 and educate myself. – VonC May 30 '21 at 13:24
2

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern API:

import java.time.LocalDate;
import java.time.Month;

public class Main {
    public static void main(String[] args) {
        for (int i = 2004; i < 2009; i++) {
            System.out.println(LocalDate.of(i, Month.JULY, 3));
        }
    }
}

If you want to do it by parsing the string (the way you have posted in the question), use DateTimeFormatter.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u/M/d", Locale.ENGLISH);
        for (int i = 2004; i < 2009; i++) {
            LocalDate date = LocalDate.parse(i + "/07/03", dtf);
            System.out.println(date);
        }
    }
}

Output:

2004-07-03
2005-07-03
2006-07-03
2007-07-03
2008-07-03

Learn more about java.time, the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110