How to derive the last day of the month in Java

I want to find the last day of the month in Java

I had to ask for the last day of the month in Java, but I was surprised, so I made a note.

Use java.util.Calender

It is possible to use a date-based library, but it is possible by using the getActualMaximum () method of the java.util.Calender class as shown below.

test.java


//Find the last day of January 2019
int year = 2019;
int month = 1;

Calendar cal = Calendar.getInstance();
cal.clear();//Required in some cases

cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1); //January is 0 for the month

int lastDayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

Note 1: clear () when setting the month

When you first instantiate a calendar, getInstance () with no arguments will create an instance with the current date. If the method is called on the 31st, then the next time you set a month that has only 30 days, it will be strange. This can be avoided by calling clear () in advance.

Note 2: Value when setting the month

In the Calrender class, Calendar.Month is the actual value of the month minus one. When setting the month, it is necessary to set a number that is one less than the actual number of months.

Recommended Posts

How to derive the last day of the month in Java
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
[Java] How to get the authority of the folder
How to find the total number of pages when paging in Java
How to get the absolute path of a directory running in Java
[Java] How to get the URL of the transition source
[Java] How to omit the private constructor in Lombok
How to write Scala from the perspective of Java
[Java] How to get the maximum value of HashMap
Summary of how to implement default arguments in Java
How to get the class name / method name running in Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to name variables in Java
How to switch Java in the OpenJDK era on Mac
Summary of how to use the proxy set in IE when connecting with Java
How to concatenate strings in java
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
How to get the id of PRIMAY KEY auto_incremented in MyBatis
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
How to solve the unknown error when using slf4j in Java
How to check for the contents of a java fixed-length string
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
Java reference to understand in the figure
[Java] How to use the hasNext function
[Java] Get the day of the specific day of the week
[java] Summary of how to handle char
[Java] How to use the HashMap class
How to do base conversion in Java
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[day: 5] I summarized the basics of Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to install the legacy version [Java]
Output of the book "Introduction to Java"
[Processing × Java] How to use the function
The story of writing Java in Emacs
[Java] How to use the Calendar class
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
How to get the value after "_" in Windows batch like Java -version
How to constrain the action of the transition destination when not logged in
How to change the value of a variable at a breakpoint in intelliJ
[Java improvement case] How to reach the limit of self-study and beyond
Android development, how to check null in the value of JSON object
How to create your own annotation in Java and get the value
Summary of how to select elements in Selenium
How to find the cause of the Ruby error