-2

How do I take date as input and add 3 in selected date then set label with updated date in java swings? My date format is YYYY-MM-DD.

eg if I enter 2016-04-04 then change date to 2016-04-07 and set a label to it.

i Tried this . . gives me wring output. 1970-03-01 is ouput plz help

muchie
  • 15
  • 1
  • 2
  • 1
    http://stackoverflow.com/questions/33537545/add-days-to-a-java-textfield-through-combobox/33537680#33537680 Possibly? – Steven Apr 15 '16 at 05:30
  • How many questions do you have? – shmosel Apr 15 '16 at 05:31
  • just 1 ...how to add +3 from inputed date in specified fomat.. i mean only date should increment – muchie Apr 15 '16 at 05:35
  • Calendar c = Calendar.getInstance(); Date d = new Date(0); SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD"); c.setTime(d); c.add(Calendar.DAY_OF_MONTH, 2); tfDueDate.setText(sdf.format(c.getTime())); – muchie Apr 15 '16 at 05:58
  • gives wrong output – muchie Apr 15 '16 at 05:58
  • @muchie see my answer below it's work. –  Apr 15 '16 at 06:16
  • If you use JAVA 6 or 7, JodaTime Api could help you http://www.joda.org/joda-time/, if use java 8, take a look at this url : http://www.tutorialspoint.com/java8/java8_datetime_api.htm – JHDev Apr 15 '16 at 07:33

2 Answers2

0

You can add 72 hours in date format to create new date with +3 days addition.

public String nextDate(String date) { 
  LocalDate parsedDate = LocalDate.parse(date); 
  LocalDate addedDate = parsedDate.plusDays(3); 
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
  return addedDate.format(formatter); 
} 
RaviM
  • 62
  • 14
0

To use this code:

Declare the following variables:

String youdate = "2016-04-04";
    Date mmdate;    
    TextView tv;

and use this code:

 try {
             mmdate = new SimpleDateFormat("yyyy-MM-dd").parse(youdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(mmdate);
        c.add(Calendar.DATE, 3);
        Date newdate = c.getTime();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");


        tv = (TextView)findViewById(R.id.tv);
        tv.setText(format.format(newdate));

tv contains 2016-04-07

tak3shi
  • 2,305
  • 1
  • 20
  • 33