• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Date

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fantastic, thanks, thats a great help
 
MichaelJ McCabe
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic