Подтвердить что ты не робот

Как добавить дни в дату в android

В моей базе данных я получаю дату начала, например, 2011-11-30 (yyyy/mm/dd) format.and продолжительность, например, 40 дней. Как я могу рассчитать дни и получить новый формат даты mm/dd/yyyy.

Может ли кто-нибудь помочь мне

Спасибо

4b9b3361

Ответ 1

Вы можете попробовать что-то вроде этого,

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()); 

Ответ 2

Шаг-1 Получить экземпляр календаря из указанной строки

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

Шаг-2 используйте add(), чтобы добавить количество дней в календарь

c.add(Calendar.DATE, 40); 

Шаг-3 Преобразование dtae в формат результирующей даты

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

Исходный код

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);

Ответ 3

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();

Ответ 4

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);

Ответ 5

Этот код работает 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);

Ответ 6

Просто просто скопируйте этот метод для передачи данных

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;
    } 

И назовите его как

 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);  

Вы можете поместить любой формат, который вы хотите, в свою среду

ConvertToDate("2011-11-30","yyyy/mm/dd" ,"mm/dd/yyyy");  

Ответ 7

Это работает для меня.

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);