如何在kotlin/android中仅显示时间而不是日、月、时间和GMT。。?
我已经写了显示日期、日期、时间和格林尼治标准时间的代码。我只想显示今天的时间以及随着时间的推移日期的时间变化。我正在创建脱机应用程序 我找了很多,但什么也没找到 Inbox活动文件如何在kotlin/android中仅显示时间而不是日、月、时间和GMT。。?,android,android-layout,kotlin,Android,Android Layout,Kotlin,我已经写了显示日期、日期、时间和格林尼治标准时间的代码。我只想显示今天的时间以及随着时间的推移日期的时间变化。我正在创建脱机应用程序 我找了很多,但什么也没找到 Inbox活动文件 private fun refreshSmsInbox() { val smsList = ArrayList<SmsData>() val cursor = contentResolver.query(Uri.parse("content://sms/in
private fun refreshSmsInbox() {
val smsList = ArrayList<SmsData>()
val cursor = contentResolver.query(Uri.parse("content://sms/inbox"),null,null,null,null)
cursor?.let{
if(it!!.moveToFirst()){
val dateID = it.getColumnIndex("date")
do{
val dateString = it.getString(dateID)
val sms = SmsData(getContactName(this,it.getString(nameID!!.toInt()).toString()),it.getString(messageID),Date(dateString.toLong()).toString())
smsList.add(sms)
}while (it.moveToNext())
}
it.close()
}
class ListAdapter (val context: Context, val list : ArrayList<SmsData>): BaseAdapter(){
@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.rowlayout,parent,false)
view.sms_date.text = list[position].date
return view
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/sender"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginRight="16dp"
android:background="@drawable/circle"
android:textSize="16sp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/sms_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
第二天更改为日期<代码>2019年7月30日
实际值
Mon Jul 29 11:44:01 GMT + 05:00 2019
如果我理解正确,您需要显示:
12:33 am/pm
我认为这样做应该对你有所帮助:
class ListAdapter(val context: Context, val list: ArrayList<SmsData>) : BaseAdapter() {
@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.rowlayout, parent, false)
val date: Date = list[position].date
val dateFormat = SimpleDateFormat("hh:mm a")
view.sms_date.text = dateFormat.format(date)
return view
}
类ListAdapter(val-context:context,val-list:ArrayList):BaseAdapter(){
@SuppressLint(“视图持有者”)
覆盖视图(位置:Int,转换视图:View?,父视图:ViewGroup?):视图{
val view=LayoutInflater.from(上下文)。充气(R.layout.rowlayout,父级,false)
val日期:日期=列表[职位].日期
val dateFormat=SimpleDateFormat(“hh:mm a”)
view.sms_date.text=dateFormat.format(日期)
返回视图
}
要将日期格式化为不同的格式,我们可以使用SimpleDateFormat方法,并使用format方法为其提供自定义格式:
val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") //Input date formate
val formatter = SimpleDateFormat("HH:mm a") //Output date formate
val formattedDate = formatter.format(parser.parse("2018-12-14T09:55:00"))
您应该更改日期格式其他内容如何更改此..?以及在哪个文件中..?因为我已经发布了三个文件..挖掘一个谷歌,将一种日期格式转换为另一种日期格式
dateformat.format(date)
date显示未解析的引用。您是否将date(dateString.toLong()).toString()替换为date(dateString.toLong())?在这种情况下,SmsData类中会有一个普通的日期对象。否则,您可以在InboxActivity类中设置日期格式:replace:val sms=SmsData(getContactName(this,it.getString(nameID!!.toInt()).toString()),it.getString(messageID),simpleDataFormat(“hh:mm a”).format(Date(dateString.toLong()).toString())好的,我现在就试试这个,我已经尝试了第二种方法,但我的应用程序仍然在兑现,我必须将它放在收件箱或listadapterrefreshSmsInbox()中,我的logcat没有停止,这就是为什么我应用了try-catch方法我收到了Toast消息“Bad class:class.java.lang.String”给我看看你的日志我的日志不会停止这就是为什么我不能复制粘贴它或截图
val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") //Input date formate
val formatter = SimpleDateFormat("HH:mm a") //Output date formate
val formattedDate = formatter.format(parser.parse("2018-12-14T09:55:00"))