Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

date  RSS feed

 
daisy kagia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i get the dates of the days seven days before the actual date?e.g.like today is 4th march,i would like to get 3rd march,2nd march,1st march,31st january,30th january,29th january,28th january.using java code
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the probable solutions, there might be better approaches too.

 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
daisy kagia wrote:how do i get the dates of the days seven days before the actual date?e.g.like today is 4th march,i would like to get 3rd march,2nd march,1st march,31st january,30th january,29th january,28th january.using java code

What do you mean by "the actual date"? If it's the current date, one of the ways to get that using System.currentTimeMillis(). You can also make use of the java.util.Calender class.
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik,
We do not encourage giving ready made solutions. DontBeACodeMill.
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies for the same.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing UI related here. Moving thread.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16047
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik, your solution has a bug: it isn't daylight savings proof. A day does not always have 24 hours; when switching from winter time to summer time it's 23 hours and when switching from summer time to winter time it's 25 hours.

It's better to use class Calendar's date arithmetic methods, which take daylight savings into account. And you can use SimpleDateFormat to format dates, which is more convenient than doing it yourself with a StringBuffer.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!