Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
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.
 
Saloon Keeper
Posts: 4280
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the "previous date"? You've constructing a brand-new object.
 
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).
 
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
 
Sheriff
Posts: 21255
87
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
Saloon Keeper
Posts: 4280
106
  • 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
Posts: 21255
87
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.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!