Java: Convert String Date to Month Name Year (MMM yyyy)

NNR

I am new to Java. I am trying to convert date from string to MMM yyyy format (Mar 2016). I tried this

Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy");
String month_name = month_date.format(cal.getTime());
System.out.println("Month :: " + month_name);  //Mar 2016

It is working fine. But when I use

String actualDate = "2016-03-20";

It is not working. Help me, how to solve this.

Yassin Hajaj

Your format must match your input

for 2016-03-20

the format should be (just use a second SimpleDateFormat object)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Full answer

SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String actualDate = "2016-03-20";

Date date = sdf.parse(actualDate);

String month_name = month_date.format(date);
System.out.println("Month :" + month_name);  //Mar 2016

Using java.time

String actualDate = "2016-03-20";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MMM yyyy", Locale.ENGLISH);
LocalDate ld = LocalDate.parse(actualDate, dtf);
String month_name = dtf2.format(ld);
System.out.println(month_name); // Mar 2016

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert string to Date in R: month abbreviated "dd-mmm-YYYY"

Format Date to only Month and Year - "MMM-yyyy"

Convert date to month name & year

How to convert string to DateTime object with month name, Date and Year flutter?

Convert abbreviated month name (MMM) to month number or to any date in SQL

SQL convert String date of style d-MMM-yyyy to DATE

Convert date format from "month_name day, year" to "yyyy-mm-dd" in BigQuery

Convert month(name) year to date in dataframe

convert date in php day/month name, year

How to format a DatePicker in WinUI 3 to display date format as: day/month/year (dd/MMM/yyyy)?

Convert MM/DD/YYYY date to Month Day Year

Convert numeric date string (dd/mm/yyyy) to newline-separated string with short month name

How to get int Month, Day, And Year in String format "EEE, MMM d, yyyy"

How to convert Java String "EEE MMM dd HH:mm:ss zzz yyyy"date type to Java util.Date "yyyy-MM-dd"

Month Year In String to Date Java 8

Convert Month name and date to yyyy-mm-dd Python pandas

Date convert dd-MMM-yyyy to dd-MM-yyyy in java

Pandas DF convert date string to date year and month

Convert a string to date Format: DD-MMM-YYYY to date time in Python

Using EXTRACT YEAR, EXTRACT MONTH and CONCAT functions to get the date in "Month Name, YYYY" format

VBA convert string with day and month name to date

SQL Server: Convert a month name (string) to a date

Correct way to convert Month Name and Year to complete date SQL Server

How to convert month number to name between date and year in php?

How to convert/change date format from MM/DD/YYYY to Month Date, Year?

How to get dd MMM yyyy format? (Month name in capital)

How to convert a string to a date from dd mmm yyyy format in SQL Server

Convert Timestamp string (dd MMM yyyy HH:mm:ss) to Date (DD/MM/YY) in Google Sheets

"EEE, dd MMM yyyy hh:mm:ss zzz" string convert to Date

TOP Ranking

HotTag

Archive