MonthDay parse(CharSequence, DateTimeFormatter) method in Java
The parse(CharSequence text, DateTimeFormatter formatter) method of the MonthDay class in Java is used to get an instance of MonthDay from a text string using a specific formatter.
Syntax:
public static MonthDay parse(
CharSequence text,
DateTimeFormatter formatter)
Parameters: This method accepts
- text as a parameter to parse and
- formatter as a parameter to use.
Return value: This method returns the parsed month-day.
Exceptions: This method throws DateTimeParseException if the text cannot be parsed.
Below programs illustrate the parse(CharSequence text, DateTimeFormatter formatter) method of MonthDay in Java:
Program 1:
import java.time.*;
import java.time.temporal.*;
import java.time.format.*;
public class GFG {
public static void main(String[] args)
{
DateTimeFormatter datetimeformatter
= DateTimeFormatter.ofPattern( "--MM-dd" );
MonthDay monthday = MonthDay.parse(
"--05-09" , datetimeformatter);
System.out.println( "MonthDay: "
+ monthday);
}
}
|
Output:
MonthDay: --05-09
Program 2:
import java.time.*;
import java.time.temporal.*;
import java.time.format.*;
public class GFG {
public static void main(String[] args)
{
DateTimeFormatter datetimeformatter
= DateTimeFormatter.ofPattern( "--dd-MM" );
MonthDay monthday = MonthDay.parse(
"--05-09" , datetimeformatter);
System.out.println( "MonthDay: "
+ monthday);
}
}
|
Output:
MonthDay: --09-05
References: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#parse(java.lang.CharSequence, java.time.format.DateTimeFormatter)
Last Updated :
12 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...