aspose file tools*
The moose likes JSP and the fly likes Adding/Subtracting days from TIMESTAMP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Adding/Subtracting days from TIMESTAMP" Watch "Adding/Subtracting days from TIMESTAMP" New topic
Author

Adding/Subtracting days from TIMESTAMP

Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64
Hi there,
Could u help me to solve one problem...
I have a TIMESTAMP created on my desire like dd.MM.yy. And it is stored in a database.
I did it with this code:
*****************************************
<%@ page import="java.util.Date , java.text.SimpleDateFormat"%>
<%
SimpleDateFormat formatter = new SimpleDateFormat ("dd.MM.yy");
Date currentTime_1 = new Date();
String pDate = formatter.format(currentTime_1); %>
*****************************************
Now i need to retrieve this TIMESTAMP from database and add/subtract few days than put it to the database.
I dont want to use NOW() function which provided with MySql, or give a TIMESTAMP type to the database field coz I dont like its format...
Any suggestions?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

There are 24 * 60 * 60 * 1000 milliseconds in one standard calendar day. So:
final long ONE_DAY_MILLISCONDS = 25 * 60 * 60 * 1000;
java.util.Date today = new java.util.Date();
java.util.Date yesterday = new java.util.Date( today - ONE_DAY_MILLISECONDS);
java.util.Date lastWeek = new java.util.Date( today -
(7 * ONE_DAY_MILLISECONDS ));
java.sql.Date inherites from java.util.Date, so the same applies to it.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64
Originally posted by Tim Holloway:
There are 24 * 60 * 60 * 1000 milliseconds in one standard calendar day. So:
final long ONE_DAY_MILLISCONDS = 25 * 60 * 60 * 1000;
java.util.Date today = new java.util.Date();
java.util.Date yesterday = new java.util.Date( today - ONE_DAY_MILLISECONDS);
java.util.Date lastWeek = new java.util.Date( today -
(7 * ONE_DAY_MILLISECONDS ));
java.sql.Date inherites from java.util.Date, so the same applies to it.


Thx a lot for ur reply, but there is one problem in code:
Incompatible type for -. Can't convert java.util.Date to long.
java.util.Date yesterday = new java.util.Date( today - ONE_DAY_MILLISECONDS);
It needs to be converted..
any ideas?
Ilja Smoli
Ranch Hand

Joined: Dec 02, 2001
Posts: 64

Hehe, at last i solved my problem with adding/subtracting days...
To all who have same problem:
here is the example of the code:
******************************************
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int num =5; // here is a number of days to add/subtract
// add number of days
cal.add(Calendar.DATE ,num);
Date ed = cal.getTime();
//and now formatting the date to meet ur requirements,I prefer
// dd.MM.yy
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy");
String end_date = formatter.format(ed);
out.println(end_date);
Thats all folks
C ya and have fun
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

Whoops. I forgot something!
Add:
final long TODAY_MILLISECONDS = today.getTime();
and change the expressions that reference "today" to reference "TODAY_MILLISECONDS", instead.
Although technically, the Calendar class approach is more appropriate, just in case someone slips in some Leap Seconds.
[This message has been edited by Tim Holloway (edited December 21, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding/Subtracting days from TIMESTAMP