I am facing the following problem, I have a date dd-mm-yyyy
and I need to convert it to yyyy-MM-dd HH:mm
(i.e. with the given date and current hours and minutes).
Example :
Given date : 11-06-2014
, I should get 2014-06-11 09:30
public String previousDateString(String dateString)
throws ParseException {
// Create a date formatter using your format string
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
// Parse the given date string into a Date object.
// Note: This can throw a ParseException.
Date myDate = dateFormat.parse(dateString);
// Use the Calendar class to subtract one day
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -1);
// Use the date formatter to produce a formatted date string
Date previousDate = calendar.getTime();
String result = dateFormat.format(previousDate);
return result;
}
I have tried this code but I am getting 2014-06-10 00:00