0

Now my working code is where I save the date as String is here

Now i want save date as a ServerValue.TIMESTAMP as in this answer

So first i tried to modified my model:

data class Target(
    val guid: String = "",
    val name: String = "",
    val description: String = "",
    val timestamp: MutableMap<String, Any>? = mutableMapOf()
)

after in presenter write smth like this:

fun addTarget(name: String, description: String, timestamp: ServerValue) {
        if (!TextUtils.isEmpty(name)) {
            val id: String = databaseReference?.push()?.key.toString()
            val map = mutableMapOf<String, Any>()
            map.put("timestamp", ServerValue.TIMESTAMP)
            val target = Target(guid = id, name = name, description = description, timestamp = map)
            targetsRef?.push()?.setValue(target)
        } else Log.d("some", "Enter a name")
    }

But parameter timestamp is never used.

Also in fragment i need Map, but get String, because i take it from dateView text

val date = dateView?.text.toString().trim()

in method:

override fun editTarget(targetGuid: String) {
        val name = nameEditText?.text.toString().trim()
        val description = descriptionEditText?.text.toString().trim()
        val date = dateView?.text.toString().trim()
        presenter.addTarget(name, description, date)
    }

UPD: This is how i take date from my DatePickerDialog:

private fun showDatePickerDialog() {
        val date = LocalDate.now(ZoneId.systemDefault())
        val currentYear = date.year
        val currentMonth = date.monthValue
        val currentDay = date.dayOfMonth

        val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)

        pickDate.setOnClickListener {
            val datePickDialog = DatePickerDialog(
                activity,
                R.style.DatePickerDialogTheme,
                DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
                    val selectedDate = LocalDate.of(year, month + 1, dayOfMonth)
                    val dateString = selectedDate.format(dateFormatter)
                    dateView.text = dateString
                },
                currentYear,
                currentMonth - 1,
                currentDay
            )
            datePickDialog.show()

            datePickDialog.setOnCancelListener { dialog -> dialog.dismiss() }
        }
    }

And after this i take smth like this

val date = dateView?.text.toString().trim()
Morozov
  • 4,968
  • 6
  • 39
  • 70

1 Answers1

1

You can parse the selected date like this and get the epoch milliseconds

val parsedDate = LocalDate.parse("your-date-here", dateFormatter)
val milliseconds = parseDate.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli()

you can then save the milliseconds to database. atStartOfDay() will set the time to midnight.

Sasi Kanth
  • 709
  • 8
  • 17
  • in presenter i haven't a problem, i have in fragment, when i need take text from dateView. I added more information about problem check please method editTarget. I need a date which i should to push in firebase – Morozov Sep 20 '19 at 08:44
  • So you want to save a date and timestamp separately into firebase database? – Sasi Kanth Sep 20 '19 at 09:07
  • No I want to create a date for my target. Follow this advice: https://stackoverflow.com/a/58012986/6387618 – Morozov Sep 20 '19 at 09:09
  • I need "consider storing the date as a ServerValue.TIMESTAMP" – Morozov Sep 20 '19 at 09:12
  • I cannot understand why you need ServerValue.TIMESTAMP. Because if you want to save the selected date as a epoch timestamp you can parse the selected date and convert it to milliseconds. ServerValue.TIMESTAMP will save the server timestamp to database. – Sasi Kanth Sep 20 '19 at 09:18
  • Maybe I didn't quite understand how to do it. Let us on order. I have a DatePickerDialog from where I take the date as a string. How can I further convert this String to a Date? – Morozov Sep 20 '19 at 09:23
  • You can use SimpleDateFormatter to parse the string into date. What is the selected date string format? I will update the code occordingly – Sasi Kanth Sep 20 '19 at 09:24
  • You can use this to parse date. It ``` val parsedDate = LocalDate.parse("your-date-here", dateFormatter) ``` – Sasi Kanth Sep 20 '19 at 09:36
  • Ok, i get it parsedDate: `2019-09-21`. what are my next steps? – Morozov Sep 20 '19 at 09:48
  • 1
    Since you only need the date and not time, you can do something like this `val parsedDate = LocalDate.parse("your-date-here", dateFormatter) ` `parsedDate.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli()` That would give you the milliseconds for the selected date, which can be saved to firebase database – Sasi Kanth Sep 20 '19 at 09:51
  • 1
    @SasiKanth Nice solution Sasi. Using that will help Morozon query the database using that long value in `whereEqual(parsedDate)`. – Alex Mamo Sep 20 '19 at 09:53