• 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

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

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call the getTime() method on the Calendar object - that will return a Date object set to the time and date of the Calendar.
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic