14

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
  • 21,337
  • 9
  • 51
  • 89
Nagarjuna Reddy
  • 4,083
  • 14
  • 41
  • 85
  • Please search Stack Overflow before posting. These basic date-time questions have been answered many times over. – Basil Bourque Mar 20 '16 at 15:55
  • If all you need is a year and month, consider using the [`YearMonth`](https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html) class. Passing and using objects of this class rather than strings gets you type-safety, valid values, and self-documenting code. Built into Java, so you can use throughout your code base. Can be formatted in the same manner as shown in the [correct Answer by Hajaj](http://stackoverflow.com/a/36113554/642706). – Basil Bourque Mar 20 '16 at 16:11
  • Whoops… I re-opened this Question thinking it was about parsing a year-month string which would *not* be a duplicate of the linked dup. But I misread this Question here, as it is about generating a year-month string from a year-month-date object. I should not have re-opened. – Basil Bourque Dec 02 '17 at 21:00

3 Answers3

27

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);
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("MMMM yyyy", Locale.ENGLISH);
LocalDate ld = LocalDate.parse(actualDate, dtf);
String month_name = dtf2.format(ld);
System.out.println(month_name); // Mar 2016
String fullMonthAndYear = dtf3.format(ld);
System.out.println(fullMonthAndYear); // March 2016
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
6

Try this code:

DateFormat df = new SimpleDateFormat("dd-MMMM HH:mm a");
Date date = new Date(System.currentTimeMillis());
String infi = df.format(date);

And the output should be:

11-May 4:47 PM
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Moin
  • 61
  • 1
  • 2
  • 1
    FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 05 '19 at 22:13
5

tl;dr

YearMonth.from( 
    LocalDate.parse( "2016-03-20" )
) 
.format(
    DateTimeFormatter.ofPattern( 
        "MMM uuuu" , 
        Locale.US 
    )
)

Mar 2016

java.time

Avoid the troublesome old date-time classes. Entirely supplanted by the java.time classes.

YearMonth

Java has a class to represent a year and month. Oddly named, YearMonth.

LocalDate ld = LocalDate.parse( "2016-03-20" ) ;
YearMonth ym = YearMonth.from( ld ) ;

Define a formatting pattern for your desired output.

Specify a Locale. A Locale determines (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM uuuu" , Locale.US ) ;
String output = ym.format( f ) ;

Mar 2016

You could use that formatter on the LocalDate. But I assume you may have further need of the year-month as a concept, and so the YearMonth class may be useful.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154