Android 从DatePickerDialog更轻松地显示日期

Android 从DatePickerDialog更轻松地显示日期,android,kotlin,calendar,simpledateformat,Android,Kotlin,Calendar,Simpledateformat,现在按如下方式显示日期: val calendar = Calendar.getInstance() val currentYear = calendar.get(Calendar.YEAR) val currentMonth = calendar.get(Calendar.MONTH) val currentDay = calendar.get(Calendar.DAY_OF_MONTH) pickDate.setOnClickListener {

现在按如下方式显示日期:

    val calendar = Calendar.getInstance()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentMonth = calendar.get(Calendar.MONTH)
    val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

    pickDate.setOnClickListener {
        val datePickDialog = DatePickerDialog(
            activity,
            DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                val dateFormat = SimpleDateFormat("d MMMM yyyy")
                calendar.set(year, month, dayOfMonth)
                val dateString = dateFormat.format(calendar.time)
                currentDateView.text = dateString
            },
            currentYear,
            currentMonth,
            currentDay
        )
        datePickDialog.show()
    }
有可能做得更容易些吗?

做得再容易不过了。两项简化建议:

  • 使用java.time,现代java日期和时间API。在本例中,我们用一个新类,
    LocalDate
    替换了两个旧类,
    Calendar
    Date
    。因此,它为我们节省了一个转换。此外,您使用的类早已过时,通常使用java.time会更好
  • 对用户的区域设置使用内置的本地化日期格式,而不是编写格式模式字符串。后者往往容易出错,使用内置格式更有利于国际化
  • 下面的代码没有经过测试,可能有一两个输入错误,但是你应该明白

        val date = LocalDate.now(ZoneId.of("Africa/Abidjan"))
        val currentYear = date.year
        val currentMonth = date.monthValue
        val currentDay = date.dayOfMonth
    
        val dateFormatter = DateTimeFormatter.ofLocalizedDate(TextStyle.MEDIUM)
    
        pickDate.setOnClickListener {
            val datePickDialog = DatePickerDialog(
                activity,
                DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                    val selectedDate = LocalDate.of(year, month + 1, dayOfMonth)
                    val dateString = selectedDate.format(dateFormatter)
                    currentDateView.text = dateString
                },
                currentYear,
                currentMonth - 1,
                currentDay
            )
            datePickDialog.show()
        }
    
    在我放置非洲/阿比让的地方插入您想要的时区。使用
    ZoneId.systemDefault
    作为默认时区(这是您问题中的代码所使用的)。我还将格式化程序从事件侦听器中取出。没有必要每次都构造一个新的格式化程序

    问题:我可以在Android上使用java.time吗?我在项目中使用min API 21 是的,java.time在较旧和较新的Android设备上运行良好。它至少需要Java6

    • 在Java8和更高版本以及更新的Android设备上(API级别26),现代API是内置的
    • 在Java6和Java7中,获取三个后端口,即现代类的后端口(三个十用于JSR310;参见底部的链接)
    • 在(较旧的)Android上使用Android版本的ThreeTen Backport。它叫ThreeTenABP。并确保从带有子包的
      org.threeten.bp
      导入日期和时间类
    链接
    • 解释如何使用java.time
    • ,其中首先描述了
      java.time
    • ,java.time的后端口到Java6和Java7(JSR-310为三十)
    • ,Android版Three Ten Backport
    • ,解释得非常透彻

    考虑扔掉早已过时且臭名昭著的
    SimpleDataFormat
    和朋友,并添加到您的Android项目中,以便使用现代java日期和时间API
    java.time
    。与@OleV.V一起工作会更好。可能是@OleV.V的副本。我没有看到一个代码示例。有一些示例说明了如何实现这个库?嘿,你刚刚改变了你的问题?我的回答对你没有帮助吗?现在我的答案没有意义了:/t中建议的原始问题的第一个片段使用java.time。在ThreeTenABP中使用backport的代码是相同的。另见。希望有帮助?java time中的LocalDate使用min Api 26,但我在项目中使用min 21 Mb作为变量,我可以使用
    org.joda.time.LocalDate
    ?是的,您可以使用joda time,代码可能会有一些小的更改。但是,如果你对外部依赖还满意,我看不出有什么理由让你更喜欢维护模式下的Joda TIme,而不是它的后继版本java.TIme,它在ThreeTenABP中为Android进行了后端口和调整,你也可以在低于26的API级别上使用它。您可能想阅读以下部分:我可以在Android上使用java.time吗?Thx,但我不确定这个解决方案是否更容易,因为有必要添加额外的依赖项并创建类应用程序,但也许在将来它将真正有助于避免许多问题。