1

Am implementing a default range picker and I would like the user to specify the default duration range

So in my ranges I have

1 day = 24 hours
3 days = 24 * 3 hours
1 week = 24 * 7 hours
1 month

My logic is using the total hours in a day and multiply by total days

But now the problem comes in when the range becomes more than 2 months, since some months have 31 day, 28 days and even 30days

SO how do I go about determining example the last two months from now the number of hours, so that in my calendar I can simply

 Calendar cal = Calendar.getInstance();
 cal.add(Calendar.HOUR, cal.get(Calendar.HOUR) - hours) //stuck here

SO in simple terms example

today being april 2, how do I get the number of hours from january 2 to april 2.

SO in my own logic it for more than one month it should look like:

 Date today = Date.now();    
 LocalDate userday = LocalDate.of(less number of months); 
 Period diff = Period.between(userday, today); 
Geoff
  • 6,277
  • 23
  • 87
  • 197

3 Answers3

2

Try this

Calendar calendar = Calendar.getInstance();
calendar.set(2018,Calendar.JANUARY, 2);
Long min = calendar.getTimeInMillis();
calendar.set(2018,Calendar.APRIL, 2);
Long max = calendar.getTimeInMillis();
Long range = (max - min) / (60*60*1000); //no_of_hours

Updated Answer:

Calendar calendar = Calendar.getInstance();
    Long max = calendar.getTimeInMillis();
    int currMonth = calendar.get(Calendar.MONTH);
    int currYear = calendar.get(Calendar.YEAR);
    int currDate = calendar.get(Calendar.DAY_OF_MONTH);
    int expMonth = currMonth + 3/*no_of_month*/;
    int expYear = currDate;
    int expDate = calendar.get(Calendar.DAY_OF_MONTH);
    while (expMonth > 11) {
        expMonth = expMonth - 12;
        expYear = expYear + 1; // will increment year as next month falls in next year 
    }
    calendar.set(expYear, expMonth, expDate);
    Long min = calendar.getTimeInMillis();
    Long range = (max - min) / (60 * 60 * 1000); //no_of_hours
Ashish Pardhiye
  • 510
  • 4
  • 13
  • January was just an example the range is dynamic that is , Jnuary, feb augusut ... etc not a must it be january – Geoff Apr 02 '18 at 08:38
  • You can set it as required or as selected by user – Ashish Pardhiye Apr 02 '18 at 08:39
  • the user is specifying a range eg 3 months so am trying to get from the code which are the three months from the current date, the user is not selecting the months – Geoff Apr 02 '18 at 08:45
2

You can substract month like below,

Date referenceDate = new Date();
Calendar c = Calendar.getInstance(); 
c.setTime(currentDate); 
c.add(Calendar.MONTH, -3);
return c.getTime();

Then get hours from following code.

long diff = currentDate.getTime() - previousDate.getTime();
long hours = minutes / (1000 * 60 * 60);
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • now how do i get the previous date, Since as in the question the user is specifying the duration eg 3 months which mean its 3 months from today, so how will i get the previous date 3 months back from today. – Geoff Apr 02 '18 at 08:51
  • @GEOFFREYMWANGI, any progress ? – Lucifer Apr 02 '18 at 10:17
0

You need the number of days within a month.

Its easy once you determine which month it is:

Take a look at this: Number of days in particular month of particular year?

Once you have the month just calculate number of hours for it and keep adding until you satisfy your range.

bko
  • 1,038
  • 1
  • 9
  • 17