FabianCook
FabianCook

Reputation: 20557

Changing calender time ( java.util.Calendar )

Is it possible to subtract time from a calendar?

e.g.

.....
Calendar DueTime      = Calendar.getInstance();
Calendar ReminderTime = Calendar.getInstance();

int ReminderMinute = 5;

DueTime.set(DueYear, DueMonth, DueDay, DueHour, DueMinute);

    Day   = Day   - reminderDays   ;
    Day   = Day   - (Week*7)       ;
    Month = Month - reminderMonths ;
    Year  = Year  - reminderYears  ;
    Hour  = Hour  - reminderHours  ;
    Minute= Minute- reminderMinutes;

    ReminderTime.set(Year, Month, Day, Hour, Minute  );
    ReminderTime.add(Calendar.DAY_OF_MONTH , - Day   );
    ReminderTime.add(Calendar.MONTH        , - Month );
    ReminderTime.add(Calendar.YEAR         , - Year  );
    ReminderTime.add(Calendar.HOUR         , - Hour  );
    ReminderTime.add(Calendar.MINUTE       , - Minute);

If the above is possible another question I would like to know is if...

reminderDays = 60;

Then would that go into the month before it as well?

Say its the 31st of the month that the due date is and the month before it has 30 days would the reminder day be the 1st of that month?


On another note that's related:

I'm not sure if I am right or not but does a calendar instance store time in milliseconds?

e.g. 1 minute would be 600 milliseconds and an hour would be 36000 milliseconds

So if I did:

ReminderTime.add(Calendar.HOUR         , - 1  );

all it would be doing is adding -36000 to the total time stored in that calendar object

so if that's true would doing:

ReminderTime.add(Calendar.DAY_OF_MONTH , - 60   );

result in it subtracting 51840000 milliseconds? Or would I need to change

ReminderTime.add(Calendar.DAY_OF_MONTH , - 60   );

to

ReminderTime.add(Calendar.DAY_OF_YEAR  , - 60   ); 

This would be after I call:

ReminderTime.set(Year, Month, Day, Hour, Minute  );

What I expect from this above is that my activity will calculate which day of the year that calendar object is and remove 51840000 milliseconds from it.


Can anyone help?

Three different questions here, separated by lines

Upvotes: 0

Views: 1349

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338574

tl;dr

ZonedDateTime.now()
             .minusMonths( 2 )

…versus…

ZonedDateTime.now()
             .minusDays( 60 )

Avoid legacy date-time classes

The troublesome old date-time classes are now legacy, supplanted by the java.time classes.

java.time

Yes, you can easily do math with dates in java.time. The various classes include plus… and minus… methods.

Get current moment, as a ZonedDateTime.

ZonedDateTime now = ZonedDateTime.now() ;

Better to specify the desired/expected time zone than rely implicitly on the JVM’s current default.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;

Subtract an hour.

ZonedDateTime zdt = now.minusHour( 1 ) ;

Note that anomalies such as Daylight Saving Time (DST() cut-over are handled for you automatically.

If you want to subtract exactly 60 days…

ZonedDateTime zdt = now.minusDays( 60 ) ;

But if you meant two months, that would be different logic. Read the documentation for exact behavior.

ZonedDateTime zdt = now.minusMonths( 2 ) ;

You can also use the Period class for a span of time in granularity of days-month-years. These are generic months or calendar-based days, not 24-hour days.

Period twoMonths = Period.ofMonths( 2 ) ;
ZonedDateTime zdt = now.minus( twoMonths ) ;

If you meant to represent sixty generic (24-hours) days as a number of nanoseconds, use Duration.

Duration d = Duration.ofDays( 60 ) ;
ZonedDateTime zdt = now.minus( d ) ;

Tip: If you only need the date portion, and not the time-of-day, use LocalDate class.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Upvotes: 0

NickT
NickT

Reputation: 23873

Three questions, two statements which should enable you to answer them yourself.

1) Yes you can subtract by adding a negative number as you have written.

2) The fields carry, just like units,tens,hundreds do. So taking 60 off the DAY_OF_MONTH will carry into the previous month or months or year if necessary. It will adjust the calendar by 60 days

P.S. 1 minute isn't 60 milliseconds! It's 60 x 1000 = 60,000 milliseconds

Upvotes: 2

Related Questions