This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how can i get previous Date from java.sql.Date object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can i get previous Date from java.sql.Date object " Watch "how can i get previous Date from java.sql.Date object " New topic
Author

how can i get previous Date from java.sql.Date object

manish kr. pandey
Greenhorn

Joined: Aug 30, 2011
Posts: 14
i am creating a java.sql.Date object .

And i want the previous Date of object.

Help Me!
the code is following.............

int vgd=Integer.parseInt((String)gd.getSelectedItem());
int vgm=Integer.parseInt((String)gm.getSelectedItem())-1;
int vgy=Integer.parseInt((String)gy.getSelectedItem());
sdate=new java.sql.Date(vgy, vgm, vgd);


how can i find previous date from sdate object.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What is the "previous date"? You've constructing a brand-new object.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

What do you mean by "previous date"? A Date object only holds a single date.

Edit: if what you mean is that you want to recreate the String value you parsed the date from, then I'd suggest looking at the SimpleDateFormat class - that's the easiest way of converting from Strings to Dates and back again (because a java.sql.Data IS-A java.util.Date it can be used with those as well).
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Hi Manish,

Use a Calendar, I'm sure you'll find at least one useful method in there.

Cheers,
Claudiu
manish kr. pandey
Greenhorn

Joined: Aug 30, 2011
Posts: 14
i want subtract 1 day form current date object
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Then follow Claudiu's link. Remember that java.sql.Date extends java.util.Date, so you can use your java.sql.Date instance with Calendar's setTime method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
manish kr. pandey
Greenhorn

Joined: Aug 30, 2011
Posts: 14
i have solved problem....


correct code is:

int vgd=Integer.parseInt((String)gd.getSelectedItem());
int vgm=Integer.parseInt((String)gm.getSelectedItem())-1;
int vgy=Integer.parseInt((String)gy.getSelectedItem());
sdate=new java.sql.Date(vgy, vgm, vgd);
int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;

java.sql.Date pdate=new java.sql.Date(sdate.getTime()-MILLIS_IN_DAY);


thanks for help me
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You better not use that code for the day when the time is switched to/from daylight savings time.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Tim is right. In several parts of the world there is one day each year that only has 23 hours, and one day that has 25 hours. Your code doesn't take that into account, Calendar does.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i get previous Date from java.sql.Date object
 
Similar Threads
cewolf charts
NumberFormatException
Formatting Date Object
How to check if the given date is valid?using SimpleDateFormat.Is this short and rig
formatting a SQL date in MS Access