Java之Date、Calendar、SimpleDateFormat总结
来源:互联网 发布:java实现发短信功能 编辑:程序博客网 时间:2024/06/07 09:01
涉及到时间、日期时,我们通常会用到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();}
- Java之Date、Calendar、SimpleDateFormat总结
- java calendar SimpleDateFormat Date 类
- Java - Calendar & Date & DateUtils & SimpleDateFormat
- Java 时间--Date,SimpleDateFormat,Calendar
- java-Date、SimpleDateFormat、Calendar类
- 关于java日期类Date,DateFormat,SimpleDateFormat,Calendar总结
- Java中的Date、Calendar、DateFormat、SimpleDateFormat
- java:日期相关Date,Calendar,SimpleDateFormat
- java中Random,Date,Calendar,DateFormat,SimpleDateFormat
- java中Calendar,simpleDateFormat,Date 操作
- Java| 时间日期处理(SimpleDateFormat,Calendar,Date)
- 【java 基础 之 日期格式化大总结SimpleDateFormat,Calendar】
- SimpleDateFormat、Calendar、Date
- Calendar Date SimpleDateFormat
- Calendar Date SimpleDateFormat (2)
- Date、 Calendar、SimpleDateFormat类
- Date、Calendar and SimpleDateFormat
- java日期处理类总结:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat类
- CSU-ACM2017暑假集训比赛8
- 关于requireJS的适用场合以及与seaJS的比较
- 开发工具手册
- Android知识大全(持续更新)
- 续:加载文件定期到服务器目录下(非原创,作者联系本人删除)
- Java之Date、Calendar、SimpleDateFormat总结
- nginx使用阿里云的免费ssl证书
- 对一致性Hash算法,Java代码实现的深入研究
- 继承与非继承元素
- Shader学习——CPU与GPU之间的通信
- 知乎者也
- cookie 和session 的区别
- asp.net Webapi登录azureAD并调用azure graph api
- 吉比特笔试之走格子游戏