Android 如何在Kotlin中转换时间戳
我正在尝试转换来自数据json url的TimesTampAndroid 如何在Kotlin中转换时间戳,android,kotlin,Android,Kotlin,我正在尝试转换来自数据json url的TimesTamp TimeFlight.text = list[position].TimeFlight.getDateTime(toString()) 我正在我的应用程序中使用列表视图 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { val view : View = LayoutInflater.from(context
TimeFlight.text = list[position].TimeFlight.getDateTime(toString())
我正在我的应用程序中使用列表视图
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view : View = LayoutInflater.from(context).inflate(R.layout.row_layout,parent,false)
val TimeFlight = view.findViewById(R.id.time_id) as AppCompatTextView
val LogoAriline = view.findViewById(R.id.logo_image) as ImageView
status.text= list[position].Stauts
TimeFlight.text = list[position].TimeFlight.getDateTime(toString())
Picasso.get().load(Uri.parse("https://www.xxxxxxxxx.com/static/images/data/operators/"+status.text.toString()+"_logo0.png"))
.into(LogoAriline)
return view as View
}
private fun getDateTime(s: String): String? {
try {
val sdf = SimpleDateFormat("MM/dd/yyyy")
val netDate = Date(Long.parseLong(s))
return sdf.format(netDate)
} catch (e: Exception) {
return e.toString()
}
}
json的数据类
data class FlightShdu (val Airline : String ,val TimeFlight : String)
我使用了该代码,但格式未知
假设
timelight
是字符串化的历元时间戳(以毫秒为单位),则应将其传递给getDateTime()
函数:
TimeFlight.text = getDateTime(list[position].TimeFlight)
(如果它们不是毫秒而是秒,那么只需将它们乘以1000,然后将它们传递给日期
构造函数)
另一方面,根据具体的用例,在每个getDateTime()
调用中可能不需要创建新的SimpleDateFormat
对象,您可以将其设置为实例变量
另外,我建议您查看(并遵循)Java和Kotlin应用程序的时间。这里的问题是,从1970年1月1日起,Date构造函数花费的时间与毫秒数相同,而您得到的数字是秒数 我的建议是以下代码(您可以更改格式):
你预计会发生什么,确切地说,会发生什么?若有任何错误,请发布其完整且准确的堆栈跟踪。@jbnize请不要检查我的updateIs
TimeLight
一个时间戳字符串,如“1541480400”
?@earthw0rmjim yestimelight
是来自json数据的时间戳字符串url@pabloescobar如果你需要时间,你可能需要类似于SimpleDateFormat(“HH:mm”)
的东西,而不是SimpleDateFormat(“mm/dd/yyyyy”)
。我真的很感激。你好,艾哈迈德,我能把上面的代码改到明天的下一个日期吗??我的意思是我通过上面的代码只得到明天的日期,所以我从你的问题中了解到的是,你希望函数返回你函数正确的第二天的日期?
const val DayInMilliSec = 86400000
private fun getDateTime(s: String): String? {
return try {
val sdf = SimpleDateFormat("MM/dd/yyyy")
val netDate = Date(s.toLong() * 1000 ).addDays(1)
sdf.format(netDate)
} catch (e: Exception) {
e.toString()
}
}
fun Date.addDays(numberOfDaysToAdd: Int): Date{
return Date(this.time + numberOfDaysToAdd * DayInMilliSec)
}