Skip to content Skip to sidebar Skip to footer

How To Get Number Of Days Between Today's Date And Last Date Of The Current Month?

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 curr

Solution 1:

    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

LastDay: 30CurrentDay : 1
There are29 days leftin the month.

Solution 2:

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(!).

ZoneIdz= ZoneId.of( "Africa/Casablanca" ) ; 
LocalDatetoday= 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.

LocalDatefirstOfNextMonth= 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.

Periodp= 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?

Solution 3:

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

Post a Comment for "How To Get Number Of Days Between Today's Date And Last Date Of The Current Month?"