java.time.YearMonth.from() Method Example



Description

The java.time.YearMonth.from(TemporalAmount amount) method obtains an instance of YearMonth from a temporal amount.

Declaration

Following is the declaration for java.time.YearMonth.from(TemporalAmount amount) method.

public static YearMonth from(TemporalAmount amount)

Parameters

amount − the temporal amount to convert, not null.

Return Value

the equivalent YearMonth, not null.

Exception

  • DateTimeException − if unable to convert to a YearMonth.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.YearMonth.from(TemporalAmount amount) method.

package com.tutorialspoint;

import java.time.YearMonth;

public class YearMonthDemo {
   public static void main(String[] args) {

      YearMonth date = YearMonth.from(YearMonth.of(2017,12));
      System.out.println(date);
   }
}

Let us compile and run the above program, this will produce the following result −

2017-12
Advertisements