This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding/Subtracting days from TIMESTAMP

 
Ilja Smoli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18162
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Smoli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 18162
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic