简体   繁体   中英

How to get the month name and year in String by passing Month and year in Java

Example:

Eg: String str="11/20" (MM/yy) Want to get the month name and year(Eg: November/20) by passing String in above format in Java

tl;dr

YearMonth
.parse( 
    "11/20" , 
    DateTimeFormatter.ofPattern( "MM/uu" ) 
)
.format( 
    DateTimeFormatter.ofPattern( "MMM/uu" ).withLocale( Locale.US ) 
)  

YearMonth

Parse a year-with-month string as a YearMonth object.

Specify a formatting pattern for your non-standard format.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/uu" ) ;

Parse.

YearMonth ym = YearMonth.parse( input , f ) ;

Generate text in the same manner. Define a formatting pattern. Specify a Locale to determine the human language to use in translation, and the cultural norms to use in deciding abbreviation, capitalization, ordering, etc.

DateTimeFormatter f = DateTimeFormatter. ofPattern( "MMM/uu" ).withLocale( Locale.US ) ;
String result = ym.format( f ) ;

ISO 8601

The ISO 8601 standard was invented to facilitate exchange of date-time values as text.

The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

The standard format for a year-with-month is four-digit year, HYPHEN, two-digit month: YYYY-mm .

YearMonth.parse( "2020-11" ) 

And:

myYearMonth.toString() ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM