This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes date format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "date format" Watch "date format" New topic

date format

naga raaju

Joined: Mar 14, 2008
Posts: 29
i gave the date format like this

2008-05-02 i need the day of week means 2may day is "friday" so its order is 6

how to get like this in java.

i use calender.get(Calendar.DAY_OF_WEEK); but not working.

any ide

Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

Why is it not working? How have you created the Calendar object you're getting the weekday from?

Also, don't assume weekdays are 1-based or 0-based. Instead, use the constants to convert:

Since Calendar.SUNDAY is guaranteed to be the lowest of these values (by the API), subtracting it from the weekday will guarantee a 0-based weekday.

It's even better to just always compare with the constants like Calendar.FRIDAY. The only reason you'd want to use the above approach is when using the day index as array indexes, where you want absolute guarantees that it is 0-based.

Please be careful though when using DateFormatSymbols. The arrays it returns will only work with the constants; there are often empty values at index 0 if the constants start at 1.

How To Ask Questions How To Answer Questions
naga raaju

Joined: Mar 14, 2008
Posts: 29
sorry not working that code
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by naga raaju:
i use calender.get(Calendar.DAY_OF_WEEK); but not working...
calender should have been instantiated in this way.It's an abstract class.

This will give you 6 as output.
[ May 02, 2008: Message edited by: Vishal Pandya ]
I agree. Here's the link:
subject: date format
It's not a secret anymore!