MonthDay getMonthValue() method in Java

Java 8Object Oriented ProgrammingProgramming

<p>The month of the year is obtained using getMonthValue() method in the MonthDay class in Java. This method requires no parameter and it returns the month of the year which may be in the range of 1 to 12.</p><p>A program that demonstrates this is given as follows</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/n7FEwD" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">import java.time.*; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; MonthDay md = MonthDay.parse(&quot;--02-22&quot;); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The MonthDay is: &quot; + md); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The month is: &quot; + md.getMonthValue()); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">The MonthDay is: --02-22 The month is: 2</pre><p>Now let us understand the above program.</p><p>First the MonthDay is displayed. Then the month of the year is obtained using getMonthValue() method and this is displayed. A code snippet that demonstrates this is as follows:</p><pre class="prettyprint notranslate">MonthDay md = MonthDay.parse(&quot;--02-22&quot;); System.out.println(&quot;The MonthDay is: &quot; + md); System.out.println(&quot;The month is: &quot; + md.getMonthValue());</pre>
raja
Updated on 30-Jul-2019 22:30:25

Advertisements