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?
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
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
0
 
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

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

________
radarsh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.