Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

add 10 days to currentDate in java.util.Date or cast to Calendar?

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think SimpleDateFormatter should have something along the lines of what you want
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is myDateInstance declared & initialised?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you just post whole code so we can see what you are doing?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot manipulate the seconds.

I can manipulate Month, Year, Minute, Day but not SECOND.

Why is that so?
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic