jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Date query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date query" Watch "Date query" New topic

Date query

Rebecca Abraham
Ranch Hand

Joined: Feb 20, 2003
Posts: 37
I have a requirement in my project. When I login to my application I need to fetch today's date and the corresponding Saturday's date along with it.
For eg: If I login today i.e. 06-Dec-2004, I should get this week's Sat's date i.e. 11-Dec-2004, if I login on 29-Dec-2004, I should get 01-Jan-2005.

Can someone help.

I was able to fetch today's date, please see the code below
import java.util.Date;
import java.text.SimpleDateFormat;
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat();
df.applyPattern("dd MMM yyyy");
String dateAsText = df.format(d);
Sainath Veepuri
Ranch Hand

Joined: Sep 25, 2003
Posts: 49
Hi Rebecca,

Please check the link below.

Hope this would help you out with minor changes.

- Sai
Rebecca Abraham
Ranch Hand

Joined: Feb 20, 2003
Posts: 37
I was able to acheive it in the following way :
String dateAsText = "";
GregorianCalendar gregCal = new GregorianCalendar();
Calendar cal = new GregorianCalendar();
//integer value of Sat
int satOfWeek = Calendar.SATURDAY;
//integer value of the current day
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
//find the date for sat
gregCal.add(GregorianCalendar.DATE,(satOfWeek - dayOfWeek) );
//date of the approaching saturday
Date time = gregCal.getTime();
SimpleDateFormat df = new SimpleDateFormat();
//format in dd-MMM-yyyy
dateAsText = df.format(time);
Thank you,
I agree. Here's the link: http://aspose.com/file-tools
subject: Date query
jQuery in Action, 3rd edition