I have the following function to convert date:
public String dateConvert(String D){
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-DD");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMMM-yyyy");
Date date = null;
try {
date = format1.parse(D);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dateString = format2.format(date);
dateString = dateString.replace("-", " ");
System.out.println(dateString);
return ((dateString));
}
However what ever date I pass to this, the month is always converted to January. I cant understand where I am going wrong!
Uppercase D
is the day of the year, not the day of the month.
If you take any day of the month (1-31) and treat it as the day of the year, it will fall in January.
Use lowercase d
for format1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With