We help IT Professionals succeed at work.

Parsing date - URGENT

AndriesKeun
AndriesKeun asked
on
Medium Priority
312 Views
Last Modified: 2010-04-01
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
Comment
Watch Question

Commented:
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

Commented:
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
Principal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

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

Strange...

Author

Commented:
i tried them but still was getting weird stuff. eventually sorted it out myself using what shivaspk posted.  sorry mate.

Commented:
That's alright! Never mind :-)

________
radarsh
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.