• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current date - 10

 
Naresh Talluri
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a small problem with dates, like i have one date object,
i want to go back to 10 days before of that date,
ex : current date = 10/1/2008
i need date = 1/12/2007

can anyone let me know how can we do this.
appriciate for any helps...

-Naresh

 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look in the Calendar class, specially on the add(int field, int amount)method
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naresh,

have a look at class java.util.Calendar! There you have a method add() to add (or subtract) arbitrary values to or from arbitrary fields of a date (seconds, minutes etc.).

Marco
 
Naresh Talluri
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanks alot i have tried like add(Calender.MONTH,-10);
now it is working fine..

 
Bill Cruise
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to keep things simple you can do this using the Java Date class. Just create a new Date object with the default constructor, get the time in milliseconds from it, subtract 10 days (converted to millis) from that time, then use that time to create a new Date.

Put this in a main loop to see what I mean:




If you need to do anything more complicated with Dates in Java you probably want to check out the Calendar and GregorianCalendar classes.
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't agree that this is simpler than

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naresh Talluri:
thanks alot i have tried like add(Calender.MONTH,-10);

That will ofcourse subtract 10 months, not 10 days (as you asked) from the date.
 
Bill Cruise
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't agree that this is simpler than

code:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -10);


Rodrigo, you're right that code is simpler by one line (two of mine were print statements). I think that the extra line of code using Date objects makes it more explicit and serves as a clearer example to learn from without looking into the API.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill, I have three letters for you which will invalidate your code: DST. Remember, not all days of the year have 24 hours - one has 23 and one has 25. That's why when performing date calculations you should always use Calendar, since it knows how to handle DST.
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Cruise:


Rodrigo, you're right that code is simpler by one line (two of mine were print statements). I think that the extra line of code using Date objects makes it more explicit and serves as a clearer example to learn from without looking into the API.


I didn't mean simpler because of the number of lines.
I just think that is better to use the API than making those miliseconds calculation. And even easier to understand.
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason (not that you should need one) to avoid the Date + 10 * 24 * 60 * 60 * 1000 solution is that the entire class has been deprecated.

Calendar is what to use going forward, you might as well get used to using it now.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bill Shirley]: Another reason (not that you should need one) to avoid the Date + 10 * 24 * 60 * 60 * 1000 solution is that the entire class has been deprecated.

No, the Date class has certainly not been deprecated. Many of its methods (and constructors) have been deprecated. But the class as a whole is definitely not deprecated, and it remains the preferred way to refer to dates and times in most Java programming contexts.

[Bill Shirley]: Calendar is what to use going forward, you might as well get used to using it now.


Calendar is what to use when you absolutely have to, because you can't find what you need in Date or DateFormat. Generally that means you need it for anything that might depend on knowing how many days are in a given month, or when daylight savings takes effect, or when leap years occur. Unfortunately Calendar has a very poorly-designed API, so I recommend avoiding it except when you really need it. The current problem of subtracting ten days probably qualifies - depending on what we mean by a "day". Is it 24 hours, or is it a calendar day? Usually people mean the latter, in which case you need Calendar. Or you could use Joda-Time, which is much nicer.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Shirley:
Calendar is what to use going forward, you might as well get used to using it now.

The Java date and time APIs are one of those things that a lot of people are not really happy with, and that's why the Joda Time library that Jim already mentioned is popular.

In a future version of Java, there will probably be a new date and time API that will look approximately like Joda Time - see JSR 310. So I wouldn't say that the Calendar class is the future, but rather Joda Time.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic