日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android获取毫秒,Android 日期转为为毫秒,毫秒转化为日期,获取当期日期年、月、日...

發布時間:2025/3/20 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android获取毫秒,Android 日期转为为毫秒,毫秒转化为日期,获取当期日期年、月、日... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//將時間毫秒值轉化為年月日

val date = Date(System.currentTimeMillis())

val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())

val dateStr = simpleDateFormat.format(date)

OLogsProUtil.e("測試:" + dateStr)

//將日期轉化為毫秒值

val simpleDateFormat1 = SimpleDateFormat("yyyy-MM-dd HH:mm")

val date1 = simpleDateFormat1.parse("2020-09-20 09:47")

val timeLong = date1.time

OLogsProUtil.e("測試:" + timeLong)

//將日期轉化為星期、獲取年月日

val date2 = Date(System.currentTimeMillis())

val calendar = Calendar.getInstance()

calendar.time = date2

//星期

val weekDay = calendar.get(Calendar.DAY_OF_WEEK)-1

//年

val year = calendar.get(Calendar.YEAR)

//月

val month = calendar.get(Calendar.MONTH)+1

//日

val monthDay = calendar.get(Calendar.DAY_OF_MONTH)

OLogsProUtil.e("測試:星期" + weekDay+"."+year+"-"+month+"-"+monthDay)

將日期轉化為毫秒主要使用的是SimpleDateFormat.parse()。使用時需要注意的是:

使用SimpleDateFormat.parse()的時候,經常會有ParseException發生,原因是輸入的字符串格式跟SimpleDateFormat定義的格式不一致。

這時候,可以先通過SimpleDateFormat.format把參數轉成符合格式的字符串,然后再調用SimpleDateFormat.parse()

比如:

錯誤情況:

一、

val simpleDateFormat3 = SimpleDateFormat("yyyy-MM-dd HH:mm")

val date3 = Date(System.currentTimeMillis())

val parse = simpleDateFormat3.parse(date3.toString())

二、

val simpleDateFormat1 = SimpleDateFormat("yyyy-MM-dd HH:mm")

val date1 = simpleDateFormat1.parse("2020-09-20")

val timeLong = date1.time

這兩種情況

“val parse = simpleDateFormat3.parse(date3.toString())”

“val date1 = simpleDateFormat1.parse("2020-09-20")”

這兩行都會報ParseException,因為與SimpleDateFormat的格式時間不統一。

正確情況:

先通過SimpleDateFormat.format把參數轉成符合格式的字符串,然后再調用SimpleDateFormat.parse()

//將日期轉化為毫秒值

val simpleDateFormat3 = SimpleDateFormat("yyyy-MM-dd HH:mm")

try {

val date3 = Date(System.currentTimeMillis())

val format_date = simpleDateFormat3.format(date3)

val parse_date = simpleDateFormat3.parse(format_date)

val time_date = parse_date.time

OLogsProUtil.e("測試:正確毫秒是" + time_date)

} catch (e: ParseException) {

OLogsProUtil.e("error")

}

本文地址:https://blog.csdn.net/NewActivity/article/details/108846204

總結

以上是生活随笔為你收集整理的android获取毫秒,Android 日期转为为毫秒,毫秒转化为日期,获取当期日期年、月、日...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。