-3

I would store dates as timestamp into a database from datepicker into database with SimpleDateFormat("EEEE dd MMM yyyy"); this is my datepicker code :

 private void setDateTimeField() {
    Date.setOnClickListener(this);
    Waktu.setOnClickListener(this);
    Calendar calendar = new GregorianCalendar(); 
    int reslut = calendar.get(Calendar.DAY_OF_WEEK);
    switch (reslut) {
        case Calendar.MONDAY:
            System.out.println("It's Monday !");
            break;
    }

    fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int dayOfMonth, int monthOfYear, int year) {



            Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth-1); 
            int reslut = calendar.get(Calendar.DAY_OF_WEEK);
            switch (reslut) {
                case Calendar.MONDAY:
                    System.out.println("It's Monday !");
                    break;
            }
           calendar.set(dayOfMonth, monthOfYear, year);
            Date.setText(dateFormatter.format(calendar.getTime()));

        }

    },calendar.get(GregorianCalendar.YEAR), calendar.get(GregorianCalendar.MONTH), calendar.get(GregorianCalendar.DAY_OF_MONTH));

And its failed, the value can be store into database if i change the SimpleDateFormat like ("dd MMM yyyy"), perhaps something wrong with format(newDate.getTime()));i dont know however.. can any body help me?

  • I would store dates as timestamp into a database. From that you can then retrieve all the information you need (day, month, year, etc.). – Century Oct 03 '17 at 08:29
  • i just wanna store a value from datepicker into database like this " Monday 02-10-2017 – Rizki Deddy Susanto Oct 03 '17 at 09:20
  • What's the error message then? please post the stack trace – Century Oct 03 '17 at 09:22
  • i got nothing error message, just forceclose – Rizki Deddy Susanto Oct 03 '17 at 09:24
  • I know this is not exactly what you asked but, I would suggest: store the time in the database as milliseconds, [calendar.getTimeInMillis()](https://developer.android.com/reference/java/util/Calendar.html#getTimeInMillis()), then when you retrieve it from db, format it is as you please. For example. [calendar.setTimeInMillis(long millis)](https://developer.android.com/reference/java/util/Calendar.html#setTimeInMillis(long)) and finally format it with [DateFormat](https://developer.android.com/reference/java/text/DateFormat.html) – ArtiomLK Oct 04 '17 at 04:50

2 Answers2

0

Try Using VARCHAR for saving such data.

SQLite having some specific date formats to save date.

Refer How do I insert datetime value into a SQLite database?

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
0

Finally I know whats make my app force close, nothing wrong with datepicker code from android which I displayed above, an ignorance I had, I used "str_to_date" in my php code insert data. And, after i delete it, i got result that i want, my table in database date column can like this "Monday, 3-10-2017". Btw, thank you guys for your attention

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459