【问题标题】:Using Android Studio, how to display Day(mon,tue,wed etc.), Date, Month and Year from selected date in Datepicker使用 Android Studio,如何在 Datepicker 中显示选定日期的日期(星期一、星期二、星期三等)、日期、月份和年份
【发布时间】:2020-02-12 22:56:11
【问题描述】:

我希望创建一个简单的代码,该代码将从我的日期选择器中获取一个日期,并在单独的文本视图中显示星期几、星期几、月份和年份。

然后在输入天数后显示一天。

示例:选择 12/02/2020 并输入 2

输出 2020 年 12 月 2 日,星期三 2020 年 2 月 15 日星期五 我有日期选择器和输出来给自己输出日期,但我不知道如何让它计算星期几。

【问题讨论】:

  • 请发布您当前的代码。

标签: android kotlin datepicker


【解决方案1】:
    @SuppressLint("NewApi") // Selected Date From Date Picker Format
    val resultFromDatePicker = SimpleDateFormat("dd/MM/yyyy", Locale.US)

    @SuppressLint("NewApi") // Convert Date To This Format
    val convertDateTo = SimpleDateFormat("EEEE, dd MM yyyy", Locale.US)

    // Result - Wednesday, 12 02 2020
    val result = ""+parseDate("12/02/2020", resultFromDatePicker, convertDateTo)

    // Parsing Date
    @SuppressLint("NewApi")
    fun parseDate(
        inputDateString: String?,
        inputDateFormat: SimpleDateFormat,
        outputDateFormat: SimpleDateFormat
    ): String? {
        val date: Date?
        var outputDateString: String? = null
        try {
            date = inputDateFormat.parse(inputDateString)
            outputDateString = outputDateFormat.format(date)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return outputDateString
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多