0

In an activity, I ask my user about a date using a datepicker. As far as I found, the best way to pass this date to a subsequent activity is to send separately yea, month and day.

In my subsequent activity, I want to display this date, but also this date+ 1 day, + 2 days and so on.

To do it, I taught I can do :

int mDay = origine.getIntExtra("mDay", 0);
int mMonth= origine.getIntExtra("mMonth", 0);
int mYear = origine.getIntExtra("mYear", 0);
calendrier = new GregorianCalendar(mYear,mMonth,mDay);

and then put it in a string using something like

String strcal01 = sdf.format(new calendrier);

This line don't work but it works if I replace "calendrier" by "Date()" (of course with current date and not the date get from the other activity...

What can I do to make it working ?

Thank's a lot for your help.

Alain38200
  • 11
  • 3
  • what is "calendrier" class for? it is Calendar? – xbadal Nov 15 '17 at 12:21
  • you already have date but you need to display in other activity. right ? – Amit Vaghela Nov 15 '17 at 12:22
  • Calendrier is a variable that I declared as Calendar type. The date I have here comes from another activity. I don't want to declare it here because the same date can be used in three different activities. – Alain38200 Nov 15 '17 at 12:37

3 Answers3

1

Use this method for getting date input from user

Calendar myCalendar = Calendar.getInstance();
dateValue.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new DatePickerDialog(DailySalesView.this, datePicker, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

Put this inside Activity class

String date;
DatePickerDialog.OnDateSetListener datePicker = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);


String myFormat = "dd/MM/yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat);
        date= sdf.format(myCalendar.getTime());
            }

        };

By this way you'll get formatted date in the String date You can then pass it to another Activity with Intent. If you need clarification, feel free to ask! Hope it helps!

josh_gom3z
  • 294
  • 1
  • 13
0

Use calendrier.getTime() instead of new calendrier

Your mistake:

The method format is expecting an object of the type Date, You are trying to pass the type Calendar. Also FYI, you have used the new keyword wrongly.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • I don't understand what you mean by "using the new keyword wrong". I'm sorry by making something wrong and if you could explain me what I made as mistake, I'll try to make better next time. – Alain38200 Nov 15 '17 at 13:16
  • `new` is used to create new objects. Correct usage: `new ClassName()`. You are using it as `new objectName`. – Nabin Bhandari Nov 15 '17 at 13:24
0

I don't know if I'm correct by answering to my own question but, thank you Joshua.

I finally, in my "guest" activity, create a calendar named calendrier and display the values using :

calendrier = new GregorianCalendar(mYear,mMonth,mDay);

cal01=calendrier;
String strcal01 = sdf.format(cal01.getTime());
cal02=calendrier;
cal02.add(Calendar.DAY_OF_YEAR,1);
String strcal02 = sdf.format(cal02.getTime());

I think i can be simplified because fore each date, I have 3 lines. But the most important for me now is that it works !

Alain38200
  • 11
  • 3