更多文章请看本人博客https://chenjiabing666.github.io/java
版权全部,如需转载请注明来源git
Date
类表示特定的瞬间精确到毫秒,不过从API能够看出不少的方法已经废弃了,这个类已经在过多使用了,不过仍是须要了解一下的,为了后面的学习作铺垫github
new Date()
app
long getTime()
返回计算机上面的时间,返回的是毫秒学习
setTime(long s)
用给定的毫秒值s设置时间spa
Date date=new Date(); System.out.println(date.getTime());
这个类是一个简单的格式化日期的类,继承与
DateFormat
,相对于父类来讲使用简单code
new SimpleDateFormat()
使用默认的格式化模板建立对象orm
new SimpleDateFormat(String pattern)
使用指定的格式化模板建立对象对象
String format(Date date)
将给定的日期格式化指定的模板的样式,好比2017-01-29 23:22:11
继承
applyPattern(String pattern)
将给定的格式应用于此日期的格式,至关于直接使用new Date(String pattern)
Date parse(String d)
将给定的格式化的日期格式字符串转换成Date
对象,须要注意的是转化的时候定义的模板必定要和字符串的日期格式的模板同样,不然将会解析不正确的形式
使用默认的模板格式化日期
SimpleDateFormat dateFormat=new SimpleDateFormat(); //默认的格式 String formateString=dateFormat.format(date); //格式化当前的日期 System.out.println(formateString);
使用指定的模板格式化日期
String model="yyyy-MM-dd-FF HH:mm:ss"; //指定格式化的模板 SimpleDateFormat dateFormat2=new SimpleDateFormat(model); System.out.println(dateFormat2.format(date));
将格式化的日期转换成
Date
类型的,使用的parse(String s)
,须要注意的是,下面定义的模板必定要和给定的格式化后的日期格式同样,不然转换后Date类型的毫秒值可能不正确
String d = "2017-06-12 22:34:19"; //给出格式化后的日期 String pattern = "yyyy-MM-dd HH:mm:ss"; //按照上面的日期格式定义模板,这个必定要彻底和上面的同样,不然转换不正确 SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); try { Date date = dateFormat.parse(d); //解析成Date类型 System.out.println(dateFormat.format(date)); } catch (ParseException e) { System.err.println("解析错误"); }
综合使用:计算时间差
Scanner scanner=new Scanner(System.in); System.out.println("请输入年-月-日"); String startTime=scanner.next(); System.out.println("请输入结束时间(年-月-日)"); String endTime=scanner.next(); String moudle="yyyy-MM-dd"; //定义时间模板 //建立指定模板的解析 SimpleDateFormat dateFormat=new SimpleDateFormat(moudle); Date startDate=dateFormat.parse(startTime);//解析开始时间 Date endDate =dateFormat.parse(endTime);//解析结束时间 long time=startDate.getTime()-endDate.getTime(); //返回两个时间的差,毫秒 int day=(int)(time/1000/60/60/24); //转化为天数,1秒等于1000毫秒,一分钟等于60秒,一小时等于60分钟,一天等于24小时 System.out.println(day);
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操做日历字段(例如得到下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
Calendar.getInstance()
int get(int field)
返回当前对象的一些日期信息
Date getTime()
得到当前日期的Date
对象
add(int field,int amount)
根据日历的规则,为给定的日历字段添加或减去指定的时间量。例如,要从当前日历时间减去 5 天,能够经过调用如下方法作到这一点:add(Calendar.DAY_OF_MONTH, -5)
。
setTime(Date date)
使用给定的Date
对象,设置Calendar
时间
get
方法获取一些字段的值
Calendar calendar = Calendar.getInstance(); // 建立对象 System.out.println(calendar.get(Calendar.YEAR));// 获取年份 System.out.println(calendar.get(Calendar.MONTH) + 1);// 月,从0开始,便是输出5表示6月 System.out.println(calendar.get(Calendar.DATE));// 获取一个月中的第几天 System.out.println(calendar.get(Calendar.HOUR)); // 小时 System.out.println(calendar.get(Calendar.MINUTE)); // 分钟 System.out.println(calendar.get(Calendar.SECOND)); // 秒 System.out.println(calendar.get(Calendar.AM_PM)); // 得到是上午仍是下午AM=0,PM=1 System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 一个月中的第几天 System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // 一周中的第几天,星期日是第一天 System.out.println(calendar.get(Calendar.DAY_OF_YEAR));// 一年中的第几天 System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); // 一天中的第几小时 if (calendar.get(Calendar.AM_PM) == Calendar.AM) { System.out.println("如今是上午"); } if (calendar.get(Calendar.MONTH) + 1 == Calendar.JULY) { System.out.println("如今是6月"); }
Date getTime()
方法的使用
Calendar calendar=Calendar.getInstance(); Date date=calendar.getTime(); //得到Date对象 String pattern="yyyy-MM-dd HH:mm:ss"; SimpleDateFormat dateFormat=new SimpleDateFormat(pattern); System.out.println(dateFormat.format(date));
add(int field,int amount)
方法的使用
Calendar calendar=Calendar.getInstance(); calendar.add(Calendar.DATE, -2); System.out.println(calendar.get(Calendar.DATE));
综合实例:计算出当前的准确日期
Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(calendar.MONTH) + 1; // 从0开始算,所以加1 int date = calendar.get(Calendar.DATE); int week = calendar.get(Calendar.WEEK_OF_MONTH) + 1; // 从周日开始算,所以加1 int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int seconds = calendar.get(Calendar.SECOND); if (calendar.get(Calendar.AM_PM) == Calendar.AM) { System.out.println("如今是" + year + "年" + month + "月" + date + "号" + "星期" + week + "上午" + hour + "点" + minute + "分" + seconds + "秒"); } else { System.out.println("如今是" + year + "年" + month + "月" + date + "号" + "星期" + week + "下午" + hour + "点" + minute + "分" + seconds + "秒"); }