-2

The string is of the format dd/mm/yyyy

I am parsing using the following code:-

Dateformat dateformatter = new SimpleDateFormat("EEE, MMM d, ''yy");
dateformatter.setLenient(false );

String temp = "7/07/2017"
Date date = null;
try {
  date = new SimpleDateFormat("dd/mm/yyyy").parse(temp);
}
catch(ParseException e){
  e.printStackTrace();
}

dateformatter.format(date);

The value that I get for date is Sat Jan 07 00:07:00 GMT + 10:00 2017

After formatting I get Sat, Jan 7, '17

The value I expect is FRI Jul 07, '17

venutamizh
  • 53
  • 8
  • 1
    Take a look at javadoc: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html - lowercase `m` parses the minutes. To parse the month, use the uppercase `M` –  Jul 07 '17 at 01:39
  • Yes @Hugo I missed the case sensitive data in the date. Thanks for pointing out – venutamizh Jul 07 '17 at 01:54
  • 1
    In case you want a more modern solution to your task, since you are on Android, get [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) and do `LocalDate.parse(temp, DateTimeFormatter.ofPattern("d/MM/yyyy"))`. This would also have caught your (very common) mistake with lowercase `mm` (and given you a rather cryptic error message, but better than no error message). – Ole V.V. Jul 07 '17 at 02:32

1 Answers1

1

The issue is in this line

date = new SimpleDateFormat("dd/mm/yyyy").parse(temp);

In the SimpleDateFormat, Month is denoted with M and not m.

M - Month in year (context sensitive)

m - Minute in hour

Change this to

date = new SimpleDateFormat("dd/MM/yyyy").parse(temp);

For more details, check the official documentation of SimpleDateFormat

Community
  • 1
  • 1
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58