我想从Date Picker对话框中以“ Saturday,6/10/2018”的格式获取日期。问题是,当我从当前月份选择任何日期时,它会显示真实的输出,但是当我从上个月或下个月选择任何日期时,它将显示错误的星期几。
注意:先前的问题对我没有帮助...
这是我的代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/pickDateBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Pick Full Date" />
<TextView
android:id="@+id/displayDateTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Full Date will display here..."
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.fulldateformat
import android.app.DatePickerDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
pickDateBtn.setOnClickListener {
pickDate()
}
}
private fun pickDate() {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, mYear, monthOfYear, dayOfMonth ->
val simpleDateFormat = SimpleDateFormat("EEEE")
val date = Date(mYear, month, dayOfMonth - 1)
val dayString = simpleDateFormat.format(date) //returns true day name for current month only
displayDateTv.text = "$dayString, $dayOfMonth/${monthOfYear + 1}/$mYear"
}, year, month, day)
dpd.show()
}
}
答案 0 :(得分:1)
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, mYear, monthOfYear, dayOfMonth ->
val simpleDateFormat = SimpleDateFormat("EEEE")
val date = Date(mYear, month, dayOfMonth - 1)
val dayString = simpleDateFormat.format(date) //returns true day name for current month only
displayDateTv.text = "$dayString, $dayOfMonth/${monthOfYear + 1}/$mYear"
}, year, month, day)
更改
val date = Date(mYear, month, dayOfMonth - 1)
到val date = Date(mYear, monthOfYear, dayOfMonth - 1)
说明:您使用的是关闭月份变量,而不是初始化Date类时在lambda中获得的变量。
答案 1 :(得分:1)
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, mYear, monthOfYear, dayOfMonth ->
val simpleDateFormat = SimpleDateFormat("EEEE")
val date = Date(mYear, monthOfYear, dayOfMonth - 1)
val dayString = simpleDateFormat.format(date) //returns true day name for current month only
displayDateTv.text = "$dayString, $dayOfMonth/${monthOfYear + 1}/$mYear"
}, year, month, day)
您使用的是封闭月变量,而不是初始化Date类时在lambda中获得的变量。检查此修改后的代码。