涉及到时间、日期时,我们通常会用到Java中的Date、Calendar、SimpleDateFormat这几个类。今天我们就来详细分析下这几个类,以及它们的一些常用用法。
一、概念:
1、Date:表示特定的瞬间,精准到毫秒。
构造方法:
Date() 分配Date对象并初始化此对象,以表示分配它的时间(精确到毫秒)
Date(long date) 分配Date对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即1970年1月1日00:00:00 GMT)以来的指定毫秒数。
需要注意的是Date的好多方法已被弃用,转而被Calendar替代。
2、Calendar:抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换提供了一些方法。
Calendar提供了一个类方法getInstance,以获得此类型的一个通用的对象。Calendar的getInstance方法返回一个Calendar对象,其日历字段已由当前日期和时间初始化。
3、SimpleDateFormat:是DateFormat的子类,它是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期->文本)、解析(文本->日期)和规范化。
二、常用用法:
场景1:给你若干个“2017-8-20 12:20:30”这种字符串形式表示的时间,让你按照从距离当前时间的最近时间到最远时间排序。
解决办法:
long times = new long[list.size()]; //将字符串形式表示的时间放进list集合中
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //根据给定的格式实例化SimpleDateFormat类
for (int i = 0; i < list.size(); i ++) {
String time = list.get(i);
try {
Date date = simpleDateFormat.parse(time); //将文本解析成日期
times[i] = date.getTime(); //返回自1970年1月1号 00:00:00以来的毫秒数
} catch (ParseException e) {
e.printStackTrace();
}
}
Arrays.sort(times); //排序,这里排完序后是从小到大,对应的时间是从远到近
for (int i = times.length - 1; i > -1; i ++) {
String time = simpleDateFormat.format(new Date(times[i])); //将日期格式化成文本
System.out.println(time);
}
场景2:将图片以当前时间以“HH-mm-ss”的形式作为文件名,以当前日期以“yyyy-MM-dd”的形式作为文件夹名称,保存图片。
解决办法:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
String folderPath = Environment.getExternalStorageDirectory().getPath() + "/" + simpleDateFormat.format(new Date()) + "/";
try {
File file = new File(folderPath);
if (!file.exists()) {
file.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH-mm-ss", Locale.CHINA);
String filePath = new StringBuilder().append(folderPath).append(simpleDateFormat2.format(new Date())).append(".png").toString;
//接下来将图片写入该路径即可
场景3:已知一个“yyyy-MM-dd_HH_mm_ss”格式的时间,现在要求播放从该时间前30秒~该时间后60秒的录像。
解决办法:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
try {
Date date = simpleDateFormat.parse(time);
long timeMills = date.getTime();
//根据前30秒,后60秒计算得到录像的开始时间和结束时间
Date beginDate = new Date(timeMills - 30 * 1000);
Date endDate = new Date(timeMills + 60 * 1000);
Calendar beginCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);
endCalendar.setTime(endDate);
beginYear = beginCalendar.get(Calendar.YEAR);
beginMonth = beginCalendar.get(Calendar.MONTH) + 1;
beginDay = beginCalendar.get(Calendar.DAY_OF_MONTH);
beginHour = beginCalendar.get(Calendar.HOUR_OF_DAY);
beginMinute = beginCalendar.get(Calendar.MINUTE);
beginSecond = beginCalendar.get(Calendar.SECOND);
endYear = endCalendar.get(Calendar.YEAR);
endMonth = endCalendar.get(Calendar.MONTH) + 1;
endDay = endCalendar.get(Calendar.DAY_OF_MONTH);
endHour = endCalendar.get(Calendar.HOUR_OF_DAY);
endMinute = endCalendar.get(Calendar.MINUTE);
endSecond = endCalendar.get(Calendar.SECOND);
} catch (ParseException e) {
e.printStackTrace();
}
场景4:获取字符串形式表示的日期
public static String getDateFormat(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}
场景5:获取从周一到目前为止的时间段
public static String[] getTheWeek() {
String[] strings = new String[2];
Calendar c = Calendar.getInstance();
//获取今天是这周的第几天,周日为1,周一为2,周六为7
int day = c.get(Calendar.DAY_OF_WEEK);
//获取当前时间
String rightTime = getDateFormat(c.getTime(), "yyyy-MM-dd HH:mm:ss");
//获取周一的calendar,如果是周日,则减6。如果不是,则减去DAY_OF_WEEK比周一Val大的部分,剩下的就是周一
c.add(Calendar.DAY_OF_MONTH, day == Calendar.SUNDAY ? -6 : -(day - Calendar.MONDAY));
//获取本周一的凌晨时间,获取周一的年月日,然后拼接00:00:00
String leftTime = getDateFormat(c.getTime(), "yyyy-MM-dd ") + "00:00:00";
strings[0] = leftTime;
strings[1] = rightTime;
return strings;
}
场景6:获取本月到目前的时间段
public static String[] getTheMonth() {
String[] strings = new String[2];
Calendar c = Calendar.getInstance();
//获取当前时间
String rightTime = getDateFormat(c.getTime(), "yyyy-MM-dd HH:mm:ss");
//calendar设为本月一号
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1);
//获取本月一号凌晨时间
String leftTime = getDateFormat(c.getTime(), "yyyy-MM-dd ") + "00:00:00";
strings[0] = leftTime;
strings[1] = rightTime;
return strings;
}
场景7:获取本季度到目前为止的时间段
public static String[] getTheSeason() {
String[] strings = new String[2];
Calendar c = Calendar.getInstance();
//获取当前时间
String rightTime = getDateFormat(c.getTime(), "yyyy-MM-dd HH:mm:ss");
//获取本季度一号凌晨时间
int month = c.get(Calendar.MONTH) + 1;
if (month <= 3) {
c.set(c.get(Calendar.YEAR), 0, 1);
} else if (month <= 6) {
c.set(c.get(Calendar.YEAR), 3, 1);
} else if (month <= 9) {
c.set(c.get(Calendar.YEAR), 6, 1);
} else {
c.set(c.get(Calendar.YEAR), 9, 1);
}
String leftTime = getDateFormat(c.getTime(), "yyyy-MM-dd " + "00:00:00");
strings[0] = leftTime;
strings[1] = rightTime;
return strings;
}
场景8:获取年度到目前为止的时间段
public static String[] getTheYear() {
String[] strings = new String[2];
Calendar c = Calendar.getInstance();
//获取当前时间
String rightTime = getDateFormat(c.getTime(), "yyyy-MM-dd HH:mm:ss");
//calendar设为本年度一月一号
c.set(c.get(Calendar.YEAR), Calendar.JANUARY, 1);
//获取今年的起始时间
String leftTime = getDateFormat(c.getTime(), "yyyy-MM-dd ") + "00:00:00";
strings[0] = leftTime;
strings[1] = rightTime;
return strings;
}
场景9:获取日期的星期表示
public static String getTheDayOfTheWeek(String date, String format) {
String theDayOfTheWeek = "";
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
calendar.setTime(sdf.parse(date));
int i = calendar.get(Calendar.DAY_OF_WEEK);
switch (i) {
case 1:
theDayOfTheWeek = "周日";
break;
case 2:
theDayOfTheWeek = "周一";
break;
case 3:
theDayOfTheWeek = "周二";
break;
case 4:
theDayOfTheWeek = "周三";
break;
case 5:
theDayOfTheWeek = "周四";
break;
case 6:
theDayOfTheWeek = "周五";
break;
case 7:
theDayOfTheWeek = "周六";
break;
}
} catch (ParseException e) {
e.printStackTrace();
}
return theDayOfTheWeek;
}
场景10:获取上周日的凌晨时间
public static String getLastSundayTime() {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
calendar.add(Calendar.DAY_OF_MONTH, day == Calendar.SUNDAY ? -7 : -(day - Calendar.SUNDAY));
return getDateFormat(calendar.getTime(), "yyyy-MM-dd") + " 00:00:00";
}
场景11:获取上上周日的凌晨时间
public static String getLastTwoWeekSundayTime() {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
calendar.add(Calendar.DAY_OF_WEEK, day == Calendar.SUNDAY ? -14 : -(day - Calendar.SUNDAY + 7));
return getDateFormat(calendar.getTime(), "yyyy-MM-dd") + " 00:00:00";
}
场景12:获取上周六的最后时间
public static String getLastSaturdayTime() {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
calendar.add(Calendar.DAY_OF_MONTH, day == Calendar.SUNDAY ? -8 : -(day - Calendar.SUNDAY + 1));
return getDateFormat(calendar.getTime(), "yyyy-MM-dd") + " 23:59:59";
}
场景13:获取本周六的最后时间(注:如果当前时间为周日,则取本周六的最后时间;如果当前时间未到周日,则取当前时间即可)
public static String getTheSaturdayTime() {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day == 1) {
calendar.add(Calendar.DAY_OF_MONTH, -1);
return getDateFormat(calendar.getTime(), "yyyy-MM-dd") + " 23:59:59";
} else {
return getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
}
}
场景14:获取本月第一天的凌晨时间
public static String getTheMonthFirstDayTime() {
Calendar c = Calendar.getInstance();
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1);
return getDateFormat(c.getTime(), "yyyy-MM-dd") + " 00:00:00";
}
场景15:获取上个月第一天的凌晨时间
public static String getLastMonthFirstDayTime() {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
c.set(Calendar.DATE, 1);
return getDateFormat(c.getTime(), "yyyy-MM-dd") + " 00:00:00";
}
场景16:获取上个月最后一天的最后时间
public static String getLastMonthEndDayTime() {
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 1);
c.add(Calendar.DATE, -1);
return getDateFormat(c.getTime(), "yyyy-MM-dd") + " 23:59:59";
}