| Author |
add 10 days to currentDate in java.util.Date or cast to Calendar?
|
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
I know, I can add or substract days, years,.. from an actual Calenar-Instance.
But how can I do it with the java.util.Date?
For example
Date myDate = new Date();
myDate.setMonth(a.getMonth()+1);
But setMonth is depraceted, so I should use Calendar.
But how can I convert /cast a Calendar-Instance to a Date-Instance, when I need the Date?
Or should I better cast a Date-Instance to Calendar-Instance, how?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12921
|
|
You cannot cast a Date to a Calendar, or a Calendar to a Date, because those two classes are not related in the class hierarchy. Trying to cast one into the other will lead to a ClassCastException.
Class Calendar contains setTime() and getTime() methods to set the Calendar to the date and time of a Date object, or to get a Date object that represents the date and time that a Calendar is set to. Lookup those methods in the API documentation of class Calendar.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
hello,
thanks! yes that works!
Do you know, how I convert this Date to a Timestamp.
For example, I want to compare Date and Timestamp and have to convert Date into a Timestamp-Instance.
I have this date:
and this Timestamp:
how can I convert my date into a Timestamp-Instance
|
 |
Tom Johnson
Ranch Hand
Joined: May 11, 2005
Posts: 142
|
|
|
I think SimpleDateFormatter should have something along the lines of what you want
|
<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
Look at this, I have Date-Instance and set the Time of my Calender-Instance via
But there are difference between the year, month,.... Only the getTime are equals:
What is my fault?
|
 |
Tom Johnson
Ranch Hand
Joined: May 11, 2005
Posts: 142
|
|
|
How is myDateInstance declared & initialised?
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
The Value of myDateInstance which is a Instance of Date-Object is set to that date:
20 Feb 2009 00:00:00 GMT
The Calendar-Instance returns exactly that date when using cal.getTime.
But when using cal.MONTH, then it returns a false month.
|
 |
Tom Johnson
Ranch Hand
Joined: May 11, 2005
Posts: 142
|
|
|
Why dont you just post whole code so we can see what you are doing?
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
I do not know why there is a difference between the Year, Month,.. of Date-Instance and Calendar-Instance. However the getTime returns an equal date.
Well, the problem to get a Timestamp-String is solved via SimpleDateFormat.
thanks.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
1) You seem to be printing the contents of the constants, not the fields of the calendar represented by those fields. You should use calendar.get(Calendar.MONTH), not just Calendar.MONTH
2) Months in Date and Calendar are 0-based. 1 does not mean January but February. To make it easier, compare to the constants like Calendar.FEBRUARY instead of comparing to the literal 1
3) Years in Date should have 1900 added. 109 actually means 1900 + 109, being 2009
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
okay,
so I should use this:
instead of this?:
I found out that the second approach works for years, months and minutes, but not for 'seconds':
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
I cannot manipulate the seconds.
I can manipulate Month, Year, Minute, Day but not SECOND.
Why is that so?
|
 |
Tom Johnson
Ranch Hand
Joined: May 11, 2005
Posts: 142
|
|
nimo frey wrote:I cannot manipulate the seconds.
I can manipulate Month, Year, Minute, Day but not SECOND.
Why is that so?
You can manipulate the second:
Output for me was
Fri Jan 30 16:32:10 GMT 2009
Fri Jan 30 16:32:22 GMT 2009
Note that 12 was added to the seconds value
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
nimo frey wrote:okay,
so I should use this:
instead of this?:
If possible, add still has preferecen if you want to add days etc.
|
 |
 |
|
|
subject: add 10 days to currentDate in java.util.Date or cast to Calendar?
|
|
|