-4

So I am making an Android app and I need to do calculations on number of days between current date and last date of current month.

Suppose today is 1/11/2017 and last date of current month is 30/11/2017. So number of days would 30. Because after 30 days next month will start.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ticker
  • 15
  • 1
  • 4
  • 2
    Please include some of what you've already tried so we can help debug your problem. – Michael Platt Nov 01 '17 at 16:34
  • Calendar instance with Gregorian calendar will help you here. – letsCode Nov 01 '17 at 16:36
  • 1
    Before you can write any code you need to define what you mean by "number of days between", as that phrase is ambiguous. – Jim Garrison Nov 01 '17 at 16:39
  • @JimGarrison, Suppose today is 1/11/2017 and last date of current month is 30/11/2017. So number of days would 30. Because after 30 days next month will start. – Ticker Nov 01 '17 at 16:42
  • 3
    Hi @DeepPomal I get that you are a beginner, everyone starts there :-) However, one of your strongest tools as a future developer will be able to troubleshoot your problems without someone spelling out the exact answer. For instance, have you gone onto Google and searched for "android, calculate days between two dates"? Doing so gives plenty of results to get you started and give the info you need. If you run into odd problems then post the question and we can help more. – Michael Platt Nov 01 '17 at 16:42
  • 2
    @DeepPomal Be sure of what you want. If today is 30/11/2017 you want the result to be 1? That's not strictly "days between" as it includes both endpoints of the range. When dealing with ranges of any kind you have to specify very clearly if you include one or both endpoints in the range. – Jim Garrison Nov 01 '17 at 16:44

3 Answers3

6
    Calendar calendar = Calendar.getInstance();
    int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
    int daysLeft = lastDay - currentDay;


    System.out.println("Last Day: " + lastDay);
    System.out.println("Current Day : " + currentDay);
    System.out.println("There are " + daysLeft + " days left in the month.");

output

Last Day: 30
Current Day : 1
There are 29 days left in the month.
letsCode
  • 2,774
  • 1
  • 13
  • 37
  • 1
    working is one thing. you mentioned you were a new developer. UNDERSTAND how and why it works is another. dont just steal the code and move on with your life, LEARN! – letsCode Nov 01 '17 at 16:56
  • 2
    This Answer uses troublesome old date-time classes that are now legacy, supplanted by the java.time classes. For Android, see the ThreeTen-Backport and ThreeTenABP projects. – Basil Bourque Nov 01 '17 at 21:10
3

tl;dr

ChronoUnit.DAYS.between( today ,  firstOfNextMonth )

java.time

Determining a current date requires a time zone. For any given moment, the date varies around the globe by time zone.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Africa/Casablanca" ) ; 
LocalDate today = LocalDate.now( z ) ;

From that, get an object to represent the entire month, the current YearMonth.

YearMonth ym = YearMonth.from( today ) ;

From that, ask for first of next month. Generally the best practice for handling a span of time is the Half-Open approach. The beginning is inclusive while the ending is exclusive. So the current month runs up to, but does include, the first of the following month.

LocalDate firstOfNextMonth = ym.plusMonths( 1 ).atDay( 1 ) ;

To get a count of days in total, use the ChronoUnit enum.

long days = ChronoUnit.DAYS.between( today ,  firstOfNextMonth ) ;

Dump to console.

System.out.println( days + " days between " + today + " and " + firstOfNextMonth ) ;

See this code run live at IdeOne.com.

30 days between 2017-11-01 and 2017-12-01

FYI, you can use a Period to track the span of time. Useful for other purposes, but not for a count of days.

Period p = Period.between( today , firstOfNextMonth ) ;

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

I am not quite sure if java.time is available on Android, but if it is you can use LocalDate and ChronoUnit:

LocalDate today = LocalDate.now();
LocalDate lastDayOfMonth = today.withDayOfMonth(today.lengthOfMonth());

System.out.println(today); // 2017-11-01
System.out.println(lastDayOfMonth); // 2017-11-30
System.out.println(ChronoUnit.DAYS.between(today,lastDayOfMonth)); // 29
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • 1
    Much of the java.time functionality is back-ported to Java 6 and Java 7 in the ThreeTen-Backport project. Further adapted for Android in the ThreeTenABP project. – Basil Bourque Nov 01 '17 at 19:43
  • You are misusing the `getDays` method. That method returns only *part* of a `Period`, the number of days *after* counting the number of years and months. In this particular case, your code works but only because we happen to have less than a full month. But that is coincidental good luck, not good programming. The `Period` class is simply the wrong class for this problem. – Basil Bourque Nov 01 '17 at 21:06
  • @BasilBourque, yes you are totally right, it is better to us `ChronoUnit.DAYS` – Anton Balaniuc Nov 01 '17 at 22:17