Parsing date - URGENT

I am using this code:

    List okDates = new ArrayList() ;
    Calendar current = new GregorianCalendar();
    current.setTime( startDate ) ;
    while( current.getTime().compareTo( endDate ) <= 0 )
    {
        current.add( Calendar.DATE, 1 ) ;
       
        // CHECK IF THE current is a weekend...
        int dow = current.get( Calendar.DAY_OF_WEEK ) ;
        if( dow != Calendar.SATURDAY &&
            dow != Calendar.SUNDAY )
        {
                 // NOT SATURDAY, SUNDAY, or bank holiday...  Add it to the list of OK dates :-D
                okDates.add( current.getTime() ) ;
        }
    }

The dates i get in the okDates ArrayList are String and in the format "Fri Apr 07 00:00:00 BST 2006" and I need it in the format "2006-04-01" and it must be a date not string.

Help please
AndriesKeunAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

radarshCommented:
Hi AndriesKeun,

You can just store dates in your okDates ArrayList. However when you want to display or whatever, you
can format it in the YYYY-MM-DD pattern.

 List okDates = new ArrayList() ;
    Calendar current = new GregorianCalendar();
    current.setTime( startDate ) ;
    while( current.getTime().compareTo( endDate ) <= 0 )
    {
        current.add( Calendar.DATE, 1 ) ;
       
        // CHECK IF THE current is a weekend...
        int dow = current.get( Calendar.DAY_OF_WEEK ) ;
        if( dow != Calendar.SATURDAY &&
            dow != Calendar.SUNDAY )
        {
                 // NOT SATURDAY, SUNDAY, or bank holiday...  Add it to the list of OK dates :-D
                okDates.add(new Date(current.getTime())) ;
        }
    }

To format, you can use a method like this:

private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

public String formatDate(Date date)
{
    return format.format(date);
}


________
radarsh
radarshCommented:
Remember one thing:

You CAN NOT have a date object stored in a certain pattern. i.e, date objects are
instances of java.util.Date (or any other) and if you format them, you'll get a
string (java.lang.String).

You can also do the other way round, parsing a string into a date object using the
parse method of SimpleDateFormat class.

________
radarsh
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

radarshCommented:
My posts didn't help you at all??

Strange...
AndriesKeunAuthor Commented:
i tried them but still was getting weird stuff. eventually sorted it out myself using what shivaspk posted.  sorry mate.
radarshCommented:
That's alright! Never mind :-)

________
radarsh
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.