Close

Java Date Time - YearMonth.parse() Examples

Java Date Time Java Java API 


Class:

java.time.YearMonth

java.lang.Objectjava.lang.Objectjava.time.YearMonthjava.time.YearMonthjava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public static YearMonth parse(CharSequence text)

Obtains an instance of YearMonth from a text string of format uuuu-MM.



public static YearMonth parse(CharSequence text,
                              DateTimeFormatter formatter)

Obtains an instance of YearMonth from a text string using a specific formatter.


Examples


package com.logicbig.example.yearmonth;

import java.time.YearMonth;

public class ParseExample {

public static void main(String... args) {
YearMonth y = YearMonth.parse("2015-10");
System.out.println(y);
}
}

Output

2015-10




package com.logicbig.example.yearmonth;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class ParseExample2 {

public static void main(String... args) {

DateTimeFormatter f = DateTimeFormatter.ofPattern("uuuu/M");
YearMonth y = YearMonth.parse("1990/10", f);
System.out.println(y);
}
}

Output

1990-10




package com.logicbig.example.yearmonth;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class ParseExample3 {

public static void main(String... args) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("MMM-uuuu");
String text = "Jun-2017";
YearMonth ym = YearMonth.parse(text, f);

YearMonth now = YearMonth.now();
System.out.println(now);
int i = ym.compareTo(now);
System.out.printf("Is %s current month of year = %s%n", text, i == 0);
}
}

Output

2017-06
Is Jun-2017 current month of year = true




See Also