1

Im trying to convert a string

2010-03-09

into a date, to furthermore get the Day of year.

String dt = "2010-03-09";
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = new GregorianCalendar();
Date d = date.parse(dt);
cal.setTime(d);
int doy = cal.get(Calendar.DAY_OF_YEAR); 

Im not sure why i get this error

Exception in thread "main" java.text.ParseException: Unparseable date: "0"

Is there something wrong with the SimpleDateFormat string pattern as my parameter?

My conditions are strict with the string being the format of

2010-03-09

Bc i have an array of 4000 dates in this format

Any help would be kindly appreciated, Im new to the Calendar, Date and SimpleDateFormat Classes

I am dealing with arrays, the example above is a general case. here is the actual case i am dealing with

public static int[] todoy(String[] dt) {        // input array of String (dates) and will return array of DOY's
    int[] re = new int[size];
    Date d = null;
    for (int i=0;i<size;i++){
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = new GregorianCalendar();
        try {
            d = date.parse(dt[i]);
        } catch (ParseException e) {
                e.printStackTrace();
        }
        cal.setTime(d);
        re[i] = cal.get(Calendar.DAY_OF_YEAR); 

    }//fill in array of DOY's 
    return re;
}//inputs date such as 5/2 and returns integer for which day of the year it is
7
  • Why are you parsing dt[i] instead of dt? Is that a typo? Commented May 9, 2015 at 0:19
  • Did you simplify the code example compared to your real code that may pull the date strings from somewhere? I don't see anything wrong and am suspecting other code to be responsible. Commented May 9, 2015 at 0:22
  • Yea my code deals with an array of strings, but that shouldnt change anything because the input is essentially the same. Commented May 9, 2015 at 0:23
  • Probably what @SebastiaanvandenBroek says. I would recommend putting a conditional breakpoint in there and see the exact date causing the problem. Since you have 4000 of those, and based on the original typo, I'm guessing that you're iterating over all the dates, and one of them might be somehow corrupted. Commented May 9, 2015 at 0:25
  • The exact date causing the problem is the one i posted in this example. I know this because i had a system.out statement to print out that date through the array, then i had another statement to print out the day of year with that same array and its corresponding indices. Commented May 9, 2015 at 0:27

1 Answer 1

1

Can you please try this code?

  public static int[] todoy(String[] dt) {        // input array of String (dates) and will return array of DOY's
    int[] re = new int[size];
    Date d = null;
    if(dt == null) return null;

    for (int i=0;i<size;i++){
        if(dt[i] != null && dt[i] != "0") {
            SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = new GregorianCalendar();
            try {
                d = date.parse(dt[i]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(d != null ) {
                cal.setTime(d);
                re[i] = cal.get(Calendar.DAY_OF_YEAR);
            }
        }
    }//fill in array of DOY's
    return re;
}

You need to surround your parse method with try-catch clause. It seems one of the entries is not in correct format. And when you catch that exception you can check what exactly was the cause.

Sign up to request clarification or add additional context in comments.

8 Comments

I got the same error, this time the error displayed 4 times instead of just once, and at the end, a 78 appeared. 78 is the correct answer but i dont understand why these error messages are appearing in quantities now lol. @akhil_mittal
Did you debug to check what input caused the exception?
Its all my inputs now. I get the right answer in the output, but these error messages are displaying right before the correct answer is outputed
Im trying a bunch of different inputs and the same thing keeps happening. 4 messages of that same error, then the correct answer
Actually your code works fine for input 2010-03-09 and problem is somewhere else. Are you using it in some kind of loop? If yes please share the code for that as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.