• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
manish kr. pandey
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the "previous date"? You've constructing a brand-new object.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

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

Cheers,
Claudiu
 
manish kr. pandey
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want subtract 1 day form current date object
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
manish kr. pandey
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2895
46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You better not use that code for the day when the time is switched to/from daylight savings time.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic