Method to add the number of years and get the end of the month

Method to get the end of month in Java


	public static String getLastYmd(String ymd) {
		try {
			//Date check
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			sdf.setLenient(false);
			sdf.parse(ymd);

			//Get year / month
			int y = Integer.parseInt(ymd.substring(0, 4));
			int m = Integer.parseInt(ymd.substring(4, 6));

			//Get the last date of the acquired date
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.YEAR, y);
			cal.set(Calendar.MONTH, m - 1);
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));

			//Convert to YYYYMMDD format and return
			return sdf.format(cal.getTime());

		} catch (Exception ex) {
			//Returns null when an exception occurs
			return null;
		}
	}

Method to add the number of years and get the end of the month

	public static String addYear(String fromYear, int year) {

		//Generate SimpleDateFormat object for format conversion
		DateFormat df = new SimpleDateFormat("yyyyMMdd");

		//Create a Calendar object that holds the current date and time
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(df.parse(fromYear));
		} catch (ParseException e) {
			//TODO auto-generated catch block
			e.printStackTrace();
		}

		//Add years
		cal.add(Calendar.YEAR, year);

		return getLastYmd(df.format(cal.getTime()));
	}

Recommended Posts

Method to add the number of years and get the end of the month
Program to find the number of days per month including leap years
[Ruby] Questions and verification about the number of method arguments
Command to check the number and status of Java threads
I want to call a method and count the number
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
How to determine the number of parallels
I wanted to add @VisibleForTesting to the method
How to change the maximum and maximum number of POST data in Spark
Add empty data to the top of the list
How to get today's day of the week
Output of how to use the slice method
[Java] How to get the authority of the folder
[Swift] How to get the number of elements in an array (super basic)
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
I want to recursively get the superclass and interface of a certain class
[Java] How to get the URL of the transition source
[Swift] Get the number of steps with CMP edometer
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
Convert the array of errors.full_messages to characters and output
It's easy to get rid of it, but I've summarized the times method again.
[Ruby] Method to easily get the receiver type .class
[Java] How to get the maximum value of HashMap
Pass arguments to the method and receive the result of the operation as a return value
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
[Swift] How to get the document ID of Firebase
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
Implementation method of linking multiple images to one post and posting at the same time
How to get the class name / method name running in Java
Setting method to link Java of Eclipse and Github / September 2017
The permission specification of the FileUtils method is an octal number.
I tried to summarize the basics of kotlin and java
Get to the abbreviations from 5 examples of iterating Java lists
I want to expand the clickable part of the link_to method
How to add elements without specifying the length of the array
Add a shadow to the Swift Button (and also the circle)
How to derive the last day of the month in Java
Aggregate the number of people every 10 years from List <Person>
How to check the extension and size of uploaded files
[Java] Delete the specified number of characters from the end of StringBuilder
[jsoup] How to get the full amount of a document
Find the number of days in a month with Kotlin
4 Add println to the interpreter
I tried to express the result of before and after of Date class with a number line
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Java] Various summaries attached to the heads of classes and members
End of Docker and Kubernetes: Grace period from SIGTERM to SIGKILL
[Java] Get the dates of the past Monday and Sunday in order
How to set the IP address and host name of CentOS8
[Swift UI] How to get the startup status of the application [iOS]
How to get the contents of Map using for statement Memorandum
[Rails] How to omit the display of the character string of the link_to method
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Abuse strictly prohibited] Get the number of views of your Qiita article
[Rails] Read the RSS of the site and return the contents to the front
I tried to summarize the methods of Java String and StringBuilder