// TODO: 2023/3/14 将毫秒值转换为时间
//Date date=new Date(19090008);//自定义的毫秒值
Date date=new Date(System.currentTimeMillis());//获取系统当前时间的毫秒值
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
String dateStr=simpleDateFormat.format(date);
Log.e("fxHou","毫秒值转为日期="+dateStr);
// TODO: 2023/3/14 日期转换为毫秒值
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
try {
Date date1=simpleDateFormat1.parse("2020-09-20 09:47:00");
Long ms=date1.getTime();
Log.e("fxHou","日期转为毫秒="+ms+"毫秒");
} catch (ParseException e) {
e.printStackTrace();
}
// TODO: 2023/3/14 从日期获取星期、年、月、日
Date date2=new Date(System.currentTimeMillis());//获取系统当前时间的毫秒值
Calendar calendar=Calendar.getInstance();
calendar.setTime(date2);
//星期
int weekDay = calendar.get(Calendar.DAY_OF_WEEK)-1;
//年
int year = calendar.get(Calendar.YEAR);
//月
int month = calendar.get(Calendar.MONTH)+1;
//日
int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
//日
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//日
int minute = calendar.get(Calendar.MINUTE);
//日
int second = calendar.get(Calendar.SECOND);
Log.e("fxHou","星期="+weekDay);
Log.e("fxHou","年="+year);
Log.e("fxHou","月="+month);
Log.e("fxHou","日="+monthDay);
Log.e("fxHou","时="+hour);
Log.e("fxHou","分="+minute);
Log.e("fxHou","秒="+second);
Java操作时间
猜你喜欢
转载自blog.csdn.net/qq_41008818/article/details/129517233
今日推荐
周排行