在Firebase实时数据库中保存日期 [英] Save date in Firebase Realtime Database

查看:88
本文介绍了在Firebase实时数据库中保存日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的工作代码是将日期保存为String的位置,此处

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

现在,我希望将日期另存为ServerValue.TIMESTAMP,如 answer

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")
    }

但是从未使用参数timestamp.

在片段中我也需要Map,但是需要String,因为我从dateView文本中获取

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

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

在方法中:

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: 这是我从DatePickerDialog获取日期的方式:

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() }
        }
    }

然后我就这样

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

推荐答案

您可以像这样解析所选的日期并获取纪元毫秒

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()

然后可以将毫秒保存到数据库. atStartOfDay()会将时间设置为午夜.

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

这篇关于在Firebase实时数据库中保存日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆