-3

I am trying to convert string of format yyyy-mm-dd to dd-MMM-yy. I am getting correct year and days but for month it is showing only jan irrespective of my input. How to fix it?

String input = "2013-09-14";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
Date date = null;
try {
    date = format1.parse(input);
    String temp = format2.format(date);
    System.out.println(temp);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Output:

14-Jan-13

But I should get:

14-Oct-13
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
Pravin
  • 7
  • 1
  • 1
    yyyy-mm-dd is year-minute-day. You need yyyy-MM-dd. – moggi Aug 29 '17 at 07:22
  • What makes you expect October? I know that `Date`’s deprecated `setMonth` method would understand 9 as October, but `SimpleDateFormat`, albeit troublesome in many respects, I would still expect it to understand `09` as September. – Ole V.V. Aug 29 '17 at 08:11
  • Please, for your own sake and ours, search and research before asking. You will get a good answer faster that way. This question has been asked with smaller variations many times before. – Ole V.V. Aug 29 '17 at 08:13

5 Answers5

1

mm is for minutes. MM is for months.

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Stéphane Ammar
  • 1,454
  • 10
  • 17
1

mm is for minute

you need MM or MMM for month.

See SimpleDateFormat for reference.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

I would suggest latest API LocalDate which not requires try/catch and which is easier to use :

String input = "2013-09-14";

LocalDate inputDate = LocalDate.parse(input, DateTimeFormatter.ISO_DATE);
String format = inputDate.format(DateTimeFormatter.ofPattern("dd-MMM-yy"));

System.out.println(format);

  • DateTimeFormatter.ISO_DATE is shortcut for DateTimeFormatter.ofPattern("yyyy-MM-dd");
  • LocalDate inputDate = LocalDate.parse(input); would also work because ISO_DATE the default format

DateTimeFormatter doc (patterns)

azro
  • 53,056
  • 7
  • 34
  • 70
  • Brilliant! Using the built-in `DateTimeFormatter.ISO_DATE` saves us from specifying an explicit format pattern, thereby eliminating the risk of getting the case wrong. – Ole V.V. Aug 29 '17 at 08:17
  • 1
    @OleV.V. also because ISO_DATE is the standard format (use if you print without specidy a format) it's useless to specify it for parse – azro Aug 29 '17 at 08:27
  • 1
    Citation "not requires try/catch", well, the compiler does not need it but if the input does not match the pattern then parsing can still throw a runtime exception which has to be handled anyway. – Meno Hochschild Aug 29 '17 at 10:19
  • @MenoHochschild yep, like ALL the things in Java, is the input is not good you can have a exception but we don't put try/catch everywhere ;) So using this and not Simple... seems easier to me – azro Aug 29 '17 at 11:39
0

The pattern is case sensitive:

MM is month, mm is Minute:

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

For more Information read The javadoc of SimpleDateFormat

Jens
  • 67,715
  • 15
  • 98
  • 113
-3

personally i used a pattern to do this,

LocalTime w = LocalTime.MIDNIGHT.plus(d);
s = DateTimeFormatter.ofPattern("HH:mm:ss").format(w);

and i advise you to use java 8 date type instead

azro
  • 53,056
  • 7
  • 34
  • 70