Java之Date、Calendar、SimpleDateFormat总结

涉及到时间、日期时,我们通常会用到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 = folderPath + simpleDateFormat2.format(new Date()) + ".png";
//接下来将图片写入该路径即可

场景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();
}

猜你喜欢

转载自blog.csdn.net/zdj_develop/article/details/77623493