In development, some projects need to obtain the current time accurate to milliseconds. How to obtain it? Now we share two methods:
1. Use the SimpleDateFormat class to obtain
val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//设置日期格式精确到毫秒 SSS代表毫秒
val date= df.format(Date())// new Date()为获取当前系统时间
Log.e("MainActivity","当前时间:"+date)
2. Use the Calendar class to get
Calendar Cld = Calendar.getInstance();
int YY = Cld.get(Calendar.YEAR) ;
int MM = Cld.get(Calendar.MONTH)+1;
int DD = Cld.get(Calendar.DATE);
int HH = Cld.get(Calendar.HOUR_OF_DAY);
int mm = Cld.get(Calendar.MINUTE);
int SS = Cld.get(Calendar.SECOND);
int MI = Cld.get(Calendar.MILLISECOND);//毫秒