class Adapter(private val data: ArrayList<Date>) : RecyclerView.Adapter<ViewHolder>() {
private lateinit var binding: ListitemDateBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
binding =
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.listitem_date,
parent,
false
)
return ViewHolder(binding)
}
override fun getItemCount(): Int =
data.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
}
ViewHolder.kt
class ViewHolder (
private val binding: ListitemDateBinding
) : RecyclerView.ViewHolder(binding.root){
fun bind(date: Date){
val currentDateTime = LocalDateTime.now()
val format = SimpleDateFormat("M/d", Locale.US)
val cal = Calendar.getInstance(Locale.US)
val currentDate = Calendar.getInstance(Locale.US)
val currentDay = currentDate[Calendar.DAY_OF_MONTH]
val currentMonth = currentDate[Calendar.MONTH]
binding.textDate.text = currentDateTime.toString()
}
}
我尝试使用 new Date() 显示当前 UTC 时间而不是当地时间。使用 (new Date()).toUTCString() 确实提供了 UTC 字符串,但我需要 UTC 对象,因为我需要将值传递给...
我正在尝试使用新的Date()显示当前UTC时间而不是当地时间。
使用 (new Date()).toUTCString() 确实提供了 UTC 字符串,但我需要 UTC 对象,因为我需要将值传递给其他库,因为库在内部将其转换为日期对象。因此,每次我将字符串包装到新日期中并将其转换为 UTC 时间时,它都会再次显示本地时间 +GMT+200 。有什么方法可以获取 +GMT000 日期对象,而不管我处于哪个时区?
typealias Dates = java.util.Date
fun Dates.toEpochMillis(timeZoneId: ZoneId): Long {
val one = Instant.ofEpochMilli(time).toEpochMilli() // 1719565201000
val three = secsToMillis(this.toLocalDateTime(timeZoneId).utcEpochSecs) // 1719568801000
return one
}
fun Dates.toLocalDateTime(timeZoneId: ZoneId): LocalDateTime {
return Instant.ofEpochMilli(time).atZone(timeZoneId).toLocalDateTime()
}
fun secsToMillis(timeInSecs: Long): Long {
return timeInSecs * 1000
}