File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how can i get the date on next sunday Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can i get the date on next sunday" Watch "how can i get the date on next sunday" New topic
Author

how can i get the date on next sunday

vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
hi all,

any way i can do this or i have follow all the process of setting values on the calendar object.


cheers
vijay
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you are just trying to find out what the date is in n days time, you can just add n days to the current date. The Calendar class has an add method for this purpose.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
hi Paul,
i need to get the date on the coming next sunday.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So...you already know the date and you just want to create a Calendar for that date? They you are right, you need to set the individual fields of your Calendar manually (using one of the set methods).
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
i dont know the date of sunday paul. what i only aware is today. i have to create a date object for next sunday.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Use Calendar.get(Calendar.DAY_OF_WEEK) to determine what day of the week it is. Then subtract that from Calendar.SUNDAY, and add 7 if needed. Pass that number to Calendar.add:


If you want the next Sunday on a Sunday as well you should also add 7 if days == 0, making the guard "days <= 0".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

You can also take a look at the Joda Time API which has some great methods to make date and time math a bit more straight forward than the Java date & time APIs.
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
thanks Rob,

it was great i was not able to device a simple algorithm for all the day

cheers

vijay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i get the date on next sunday
 
Similar Threads
moc.hcnaRavaJ.www
JavaRanch is giving two books away this week
Strings are immutable
Passed 1.4 with 100%
Internet Porn is too available...