0

先生,我有一个像“08-25-2013”​​这样的日期作为字符串,我从这个字符串日期得到了天数,现在我在其中添加了几个天数,比如 long days=days+250; 现在的问题是我怎样才能得到像“05-02-2014”这样的日期作为字符串。请尽快回复我,请提前谢谢尊敬的先生。Qustions:天数转换为日期。

4

2 回答 2

2

这样做..

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);
于 2013-10-08T08:44:06.047 回答
0

使用此代码

            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Date dateparsed = sdf.parse("29-08-2013");
        Calendar c = Calendar.getInstance();
        c.setTime(dateparsed); 
        c.add(Calendar.DATE, 250); // Adding n number of days 
        String output = sdf.format(c.getTime());
        System.out.println(output);
于 2013-10-08T08:48:02.190 回答