0

I am trying to convert a string date in the format dd/mm/yyyy to a timestamp. My code below does not work, with the part "formatter.parse(eventDate1)" throwing a java.text.ParseException.

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(eventDate1);
long output=date.getTime()/1000L;
String str=Long.toString(output);
timestamp = Long.parseLong(str) * 1000;

Below is my code from a DatePicker where I get the date. Variable eventDate1 contains the date converted to a string.

                final Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR); // current year
                int mMonth = c.get(Calendar.MONTH); // current month
                int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
                // date picker dialog
                DatePickerDialog datePickerDialog = new DatePickerDialog(AddEventActivity.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                // set day of month , month and year value in the edit text
                                txtDate.setText(dayOfMonth + "/"
                                        + (monthOfYear + 1) + "/" + year);

                            }
                        }, mYear, mMonth, mDay);
                datePickerDialog.show();
                eventDate1 = String.valueOf(txtDate.getText().toString()); 

//convert date to string Can anyone understand why this is happening?

Zoe
  • 27,060
  • 21
  • 118
  • 148
minikate
  • 305
  • 1
  • 6
  • 16

0 Answers0