In my database I am getting start date like 2011-11-30(yyyy/mm/dd)format.and duration date like 40 days.How can i calculate the days and get new date format of mm/dd/yyyy.
The modern answer to this question is long overdue. Do consider using java.time, the modern Java date and time API, for your date work.
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
int durationDays = 40;
LocalDate startDate = LocalDate.parse("2011-11-30");
LocalDate newDate = startDate.plusDays(durationDays);
String formattedDate = newDate.format(outputFormatter);
System.out.format(formattedDate);
Output is:
01/09/2012
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 3); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
Toast.makeText(this, "" + dt, 5000).show();
This code is working 100%
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss");
String reg_date = df.format(c.getTime());
showtoast("Currrent Date Time : "+reg_date);
c.add(Calendar.DATE, 3); // number of days to add
String end_date = df.format(c.getTime());
showtoast("end Time : "+end_date);
Just Simple copy this method pass data
public static String ConvertToDate(String dateString,String inputFormater ,String outputFormater) {
String outputText = "" ;
try {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputFormater);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputFormater);
Date parsed = null;
parsed = inputFormat.parse(dateString);
outputText = outputFormat.format(parsed);
Log.d(TAG, " msg : " + outputText);
} catch (ParseException e) {
e.printStackTrace();
Log.e(TAG, "ERROR : " + e.getMessage());
}
return outputText;
}
And call it like
public static String INPUTE_JSON_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static String FORMAT_FOR_JOB_APPLIED = "MMM dd, yyyy";
String date ="2017-10-29";
ConvertToDate(date ,DateUtils.INPUTE_JSON_DATE_FORMAT ,DateUtils.FORMAT_FOR_JOB_APPLIED);
You can put any format you want in your cituation
ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy");