• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date query

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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);
__________________________________________________________________________
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rebecca,

Please check the link below.
http://www.javaalmanac.com/egs/java.util/GetDateFromCalendar.html

Hope this would help you out with minor changes.

- Sai
 
Rebecca Abraham
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
df.applyPattern("dd-MMM-yyyy");
dateAsText = df.format(time);
_______________________________________________________________________
Thank you,
Rebecca
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic