Home Cryptocurency Exchangesadd 10 days to currentDate in java.util.Date or cast to Calendar? (Beginning Java forum at Coderanch)
add 10 days to currentDate in java.util.Date or cast to Calendar? (Beginning Java forum at Coderanch)

add 10 days to currentDate in java.util.Date or cast to Calendar? (Beginning Java forum at Coderanch)

Java Cowboy

Posts: 16084

posted 10 years ago

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.

Jesper’s BlogPluralsight Author Page

posted 10 years ago

I think SimpleDateFormatter should have something along the lines of what you want

Use Code Tags!!

nimo frey

Ranch Hand

Posts: 580

posted 10 years ago

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

posted 10 years ago

How is myDateInstance declared & initialised?

Use Code Tags!!

Tom Johnson

Ranch Hand

Posts: 142

posted 10 years ago

Why dont you just post whole code so we can see what you are doing?

Use Code Tags!!

Sheriff

Posts: 21783

posted 10 years ago

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 – OCEEJBD 6 – OCEJPAD 6

How To Ask Questions How To Answer Questions

Tom Johnson

Ranch Hand

Posts: 142

posted 10 years ago

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

Use Code Tags!!

Rob Spoor

Sheriff

Posts: 21783

posted 10 years ago

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.

SCJP 1.4 – SCJP 6 – SCWCD 5 – OCEEJBD 6 – OCEJPAD 6

How To Ask Questions How To Answer Questions

Source

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.