[JAVA] Convert from ○ months to ○ years ○ months


It is a method to calculate the number of years and the number of remaining months from the number of months. (Written in Java)

35 months is 2 years 11 months

It feels like this.

Conversion method

The code looks like this.


public class YearMonth{
    public static void main(String args[]){
        int monthCount = 35;
        System.out.println(monthCount + "Months" + monthCount/12 + "Year" + monthCount%12 + "Month");

The 35 months stored in the variable monthCount have been converted to years.

Years and months were calculated separately.

For the year, divide 35 months by 12 months (1 year) and round off the decimal point with Math.floor. And if it is left as it is, the decimal point will be .0, so put (int) in front of it to make it an integer.

For months, it was possible to calculate by dividing 35 months to 12 months (1 year).

Recommended Posts

Convert from ○ months to ○ years ○ months
[Rails] How to convert from erb to haml
Convert from java UTC time to JST time
Convert from color temperature to RGB (sRGB)
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Convert from C String pointer to Swift String type
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migrate from JUnit 4 to JUnit 5
From Java to Ruby !!
Launch Docker from Java to convert Office documents to PDF
Convert Java enum enums and JSON to and from Jackson
[Urgent recruitment] I can't convert from java.util.date to java.sql.date ...
Moved from iBATIS to MyBatis3
Try Spring Boot from 0 to 100.
Convert Java Powerpoint to XPS
Moving from AWS to PaizaCloud
I want to convert characters ...
Convert String type to Timestamp type
New features from Java7 to Java8
Migrating from vargrant to docker
Convert to Ruby Leet string
From studying programming for 2 months to releasing a web application
Convert Serializable Object to byte []
[Java] How to convert a character string from String type to byte type
Connect from Java to PostgreSQL
Rewriting from applet to application
How to convert Java radix
[Java] Convert ArrayList to array
Change from SQLite3 to PostgreSQL
From Ineffective Java to Effective Java
How to migrate from JUnit4 to JUnit5
Use twitterscraper-ruby to search tweets from several years ago by date
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
Use twitterscraper-ruby to search tweets from several years ago by date
How to convert param to hash with Rails controller (updated from time to time)
[Java] How to convert from String to Path type and get the path
Convert formulas to reverse Polish notation
Convert alphabet to 26 base + array length
Programming from 51 years old Note --reference
Convert Map <K, V1> to Map <K, V2> (Convert Map Value)
How to push from Tarminal to GitHub
protocol buffeer migration from 2.x to 3.x
Convert ruby object to JSON format
[Note] Download from S3, upload to S3
Stop resending from client to server
Convert Markdown to HTML with flexmark-java
How to convert erb file to haml
I want to convert InputStream to String
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Migrate from on-premise Pukiwiki to esa.io \ (⁰⊖⁰) /
From introduction to use of ActiveHash
[Java] Convert 1-to-N List to Map
How to convert LocalDate and Timestamp
Switch from JSP + JSTL to Thymeleaf
Java to be involved from today
Introduction to Ruby (from other languages)
Convert iso-2022-jp character string to utf-8
Java, interface to start from beginner
Change DB from SQLite to MySQL