1.获取日期时间
/**
* 月、星期、时间
* day :01月01日 week: 星期四 time: 08:00
* @param time 时间 单位是毫秒
* @return
*/
public static String[] getDate(long time) {
Date date = new Date(time);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
SimpleDateFormat weekFormat = new SimpleDateFormat("EEEE");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
String day = dateFormat.format(date);
String week = weekFormat.format(date);
String tim = timeFormat.format(date);
String[] dateStr = new String[]{day, week, tim};
Log.e("MyLog", "DateUtil.getDate: 未设置时区: --->>> " + day + " " + week + " " + tim);
return dateStr;
}
打印结果
01-13 17:35:25.878 18213-18326/com.pa.paperless E/MyLog: DateUtil.getDate: 未设置时区: --->>> 01月14日 星期日 01:35
2.解决时区问题,比现在的时间多出8小时
/**
* @param time 单位 毫秒
* 时区设置:SimpleDateFormat对象.setTimeZone(TimeZone.getTimeZone("GTM"));
* @return String id = TimeZone.getDefault().getID();
*/
public static String[] getGTMDate(long time) {
Date tTime = new Date(time);
SimpleDateFormat day = new SimpleDateFormat("MM月dd日");
day.setTimeZone(TimeZone.getTimeZone("GTM"));
String dayt = day.format(tTime);
SimpleDateFormat week = new SimpleDateFormat("E");
week.setTimeZone(TimeZone.getTimeZone("GTM"));
String weekt = week.format(tTime);
SimpleDateFormat tim = new SimpleDateFormat("HH:mm");
tim.setTimeZone(TimeZone.getTimeZone("GTM"));
String timt = tim.format(tTime);
String[] date = {dayt, weekt, timt};
Log.e("MyLog", "DateUtil.getGTMDate: 设置时区 --->>> " + dayt + " " + weekt + " " + timt);
return date;
}
打印结果
01-13 17:35:25.878 18213-18326/com.pa.paperless E/MyLog: DateUtil.getGTMDate: 设置时区 --->>> 01月13日 周六 17:35