一、使用Calendar(日历)类
效率最低方法,在无效率要求时常使用
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH);
int day_Of_Month=calendar.get(Calendar.DAY_OF_MONTH);
int hour=calendar.get(Calendar.HOUR);
int hour_Of_Day=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
//Log.d("OK","year:"+year+" month:"+month+" day_Of_Month:"+day_Of_Month+" hour:"+hour+" hour_Of_Day:"+hour_Of_Day+" minute:"+minute+" second:"+second);
//显示格式为 year:2023 month:9 day_Of_Month:19 hour:3 hour_Of_Day:15 minute:16 second:17
二、使用Date(日期)类
由currentTimeMills()方法演化,效率仅次于currentTimeMills()方法
Date date=new Date();
String dateStr=date.toLocaleString();
//Log.d("OK",dateStr);
//显示格式为 Oct 19, 2023 15:09:22
三、使用currentTimeMills()方法
效率最高方法
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
long dateLong = System.currentTimeMillis();
String dateString = dateFormat.format( dateLong );
//Log.d("OK", dateString );
//显示格式为 2023-10-24 17:08:36 ( yyyy-MM-dd HH:mm:ss )