• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Date  RSS feed

 
MichaelJ McCabe
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not sure if I should have posted this in the begginer part or the general part, but here we go.

Basically, I need a way of looping through dates.

i have a date of type Date, and i want to cycle through the previous 7 days.
I could do this by taking the day part of the date, cycling through day-7 to day, and then reconstructing the date. But then id have to take into account if it hits a previous month etc etc. This is so long winded, so there obviously must be a better way of doing it. But i cant seem to find anything online which can help me.

All I need is a simple for loop which will cycle through a date, and the previous 7 days, alowing me to output (or use) each date object.

Thanks in advance
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

You can use the java Calendar api it has a method, add, which allows you to add a specific amount to any field.

I'd use this and then get the java.util.Date from this (getTime) if you need a date object.

Sean
 
MichaelJ McCabe
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Clark wrote:Hey,

You can use the java Calendar api it has a method, add, which allows you to add a specific amount to any field.

I'd use this and then get the java.util.Date from this (getTime) if you need a date object.

Sean


Ah great, thanks. So would this work throughout the Date. What i mean is if you subtract 7 days from 02/02/09 would you get 26/01/09 and not 00/02/09?
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Yes it will, it doesn't have a subtract method, but you can add a negative number and that has the same effect.
So todays date 01/03/2010
-7 days
gives 22/02/2010
Sean
 
MichaelJ McCabe
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fantastic, thanks, thats a great help
 
MichaelJ McCabe
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought i would ask another question relating to date within this thread.

I have a field in my database which is of type DATETIME. I have nothing to do with the update and parsing of data into this database, so I dont know how its done, but I need to be able to compare Dates that I am generating, to those in the database.

Problem I am having is that in the database a date will be
01/03/10 00:11:23

and my java date will be
Mon March 01 00:11:23 GMT 2010

Now i can get it in the right format by using SimpleDateFormat, but then I have a string, and casting it back to date, just gives me the date in the format above.

How can I get something into a format that will work?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the database values as instances of java.sql.Timestamp. That class extends java.util.Date so comparing is then easy.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!