43

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.

Can anyone help me

Thanks

user1061793
  • 1,047
  • 8
  • 19
  • 27

10 Answers10

97

You can try something like this,

String dt = "2012-01-04";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
    c.setTime(sdf.parse(dt));
} catch (ParseException e) {
    e.printStackTrace();
}
c.add(Calendar.DATE, 40);  // number of days to add, can also use Calendar.DAY_OF_MONTH in place of Calendar.DATE
SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
String output = sdf1.format(c.getTime()); 
Mike
  • 4,550
  • 4
  • 33
  • 47
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 7
    A side note, Calendar.DATE is synonymous with Calendar.DAY_OF_MONTH, which might be more readable – Tyler Jan 05 '12 at 06:14
  • @Styler added in the comment. Thanks. – Lalit Poptani Jan 05 '12 at 06:31
  • While this was a fine answer in 2012, I recommend we no longeruse `SimpleDateFormat` and `Calendar`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 02 '20 at 04:07
28

Step-1 Get Calendar instance from the specified string

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(dateInString));

Step-2 use add() to add number of days to calendar

c.add(Calendar.DATE, 40); 

Step-3 Convert the dtae to the resultant date format

sdf = new SimpleDateFormat("MM/dd/yyyy");
            Date resultdate = new Date(c.getTimeInMillis());
            dateInString = sdf.format(resultdate);

Source Code

String dateInString = "2011-11-30";  // Start date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(dateInString));
        c.add(Calendar.DATE, 40);  
        sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date resultdate = new Date(c.getTimeInMillis());
        dateInString = sdf.format(resultdate);
        System.out.println("String date:"+dateInString);
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
8
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();
Roadies
  • 3,309
  • 2
  • 30
  • 46
8

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);
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
6

This piece of code should do the job well!!!

public static String addDay(String oldDate, int numberOfDays) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    try {
        c.setTime(dateFormat.parse(oldDate));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    c.add(Calendar.DAY_OF_YEAR,numberOfDays);
    dateFormat=new SimpleDateFormat("MM-dd-YYYY");
    Date newDate=new Date(c.getTimeInMillis());
    String resultDate=dateFormat.format(newDate);
    return resultDate;
}

For more functionality do please checkout this link
Add days,months,years to a date

Aashis Shrestha
  • 342
  • 5
  • 13
3

java.time

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

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1
Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);

    // display the current date
    String CurrentDate = mYear + "/" + mMonth + "/" + mDay;

    String dateInString = CurrentDate; // Start date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    c = Calendar.getInstance();

    try {
        c.setTime(sdf.parse(dateInString));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    c.add(Calendar.DATE, 7656);//insert the number of days you want to be added to the current date
    sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date resultdate = new Date(c.getTimeInMillis());
    dateInString = sdf.format(resultdate);

    //Display the Result in the Edit Text or Text View your Choice
    EditText etDOR = (EditText)findViewById(R.id.etDateOfReturn);
    etDOR.setText(dateInString);
Alex Muriithi
  • 1,831
  • 1
  • 13
  • 11
0

It works for me.

Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);

String CurrentDate = mYear + "/" + mMonth + "/" + mDay;
String dateInString = dob; // Select date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dateInString));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, 14);    //14 dats add
sdf = new SimpleDateFormat("dd/MM/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
dateInString = sdf.format(resultdate);
System.out.println(dateInString);
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

Don't use:

Calendar c = Calendar.getInstance();

Use this instead:

GregorianCalendar calendar =new GregorianCalendar();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH , 30);
MasterOne Piece
  • 413
  • 5
  • 5
-1

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");  
u32i64
  • 2,384
  • 3
  • 22
  • 36
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55