This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to manipulate date / time using Calendar and then put the modified data inside the Date class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to manipulate date / time using Calendar and then put the modified data inside the Date class?" Watch "How to manipulate date / time using Calendar and then put the modified data inside the Date class?" New topic
Author

How to manipulate date / time using Calendar and then put the modified data inside the Date class?

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Hi there,

I am trying to do the following:

(1) Get today's date.

(2) Subtract one month from it.

(3) Check and see if the actual day is less than a specific number (e.g. 15).

(4) Set it back as a Date object with the new day and previous month.

For example, if today's date is:

April 1, 2010

I want my method to return a Date object which contains this:

March 15, 2010

Here's what I have so far:



Output:


As you can see that I am printing out to stdout to check my values... I need it to return a date object (the SimpleDateFormat was only to see my printlns).
Any help will be greatly appreciated...
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Change line 16 to:
cal.set(Calendar.MONTH, lastMonthInt - 1);

Months in Calendar starts with 0 for January - if you set month to 3, Calendar teats this value as April.

You also can use:
cal.roll(Calendar.MONTH, - 1);
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Ireneusz,

Thank you so much for the response... I think you didn't understand my question. I know how to manipulate date and time using the Calendar class but how do I set the changed date and / or time into a Date object?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

Call the getTime() method on the Calendar object - that will return a Date object set to the time and date of the Calendar.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Jesper,

I know this, on line 5, I am getting the time like this:



I am not seeking to get the current time, use the Calendar class to manipulate the current time's data, and set it as a String (the SimpleDateFormat is already in there so I can print to console what the values are).

What I am seeking is to get the current time, use the Calendar class to change the current date's values (specifically month and day) and store in back into a Date object.

With thanks and kindess...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

James Dekker wrote:What I am seeking is to get the current time, use the Calendar class to change the current date's values (specifically month and day) and store in back into a Date object.

But you can do that exactly with the getTime() method of class Calendar:

Isn't that what you mean?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to manipulate date / time using Calendar and then put the modified data inside the Date class?
 
Similar Threads
Date format using Caendar
Problem with adding months to Gregorian Calender
Holiday in date
SQL Show Previous Month Date Question
Need to change Calendar to dd/mm/yyyy format