JDK7时间类: Date 时间------Date时间类是一个JDK写好的javabean类,用来描述时间,精确到毫秒。 利用空参对象创建的对象,默认表示系统当前的时间。 利用有参构造创建的对象,默认表示系统当前的时间。 如何创建日期对象:Date date= new Date; 如何修改时间对象的毫秒值:setTime(毫秒值); 如何获取时间对象的毫秒值:getTime();
SimpleDateFormat 格式化时间 构造方法里的成员方法 1》格式化:把时间变成我们喜欢的格式 2》解析:把字符串表示的时间变成Date对象(字符串本身不能加减,变成对象之后,通过get方法获取毫秒值计算)
Calendar 日历:代表系统当前时间得日历对象,可以单独修改,获取时间中的年,月, 日 细节:Calendar是一个抽象类,不能直接创建对象 public static Calendar getInstance()获取当前时间的日历对象(Calendar是一个抽象类,不能直接new,而是通过一个静态方法获取到子类对象) 底层原理:会根据系统得不同时区来获取不同的日历对象 会把时间中的纪元,年,月,日,时,分,秒等等都放到一个数组中。 月份:范围0-11,如果获取出来的是0,实际是一月份 星期:在老外的眼中,星期日是一周中的第一天 例如:Calendar c=Calendar.getInstance(); public int get (int field)取日期中的某个字段信息 public void set (int field ,int value)修改日历的某个字段信息 public void add (int field ,int amount)为某个字段增加/减少指定的值 JDK8时间类:
//static Set<String> getAvailableZoneIds(); 获取java中所支持的时区 //static ZoneId SystemDefault(); 获取系统默认时区 //static ZoneId of(String zoneId); 获取一个指定时区
//static Instant now() 获取当前时间的Instant对象 //static Instant of Xxxx(long ) 根据(秒,毫秒,纳秒)获取Instant对象 //ZoneDateTime atZone(ZoneId zone) 指定时区 //boolean isXxx(Instant otherInstant) 判断系列的方法 //Instant minusXxx(long) 减少时间系列的方法 //Instant plusXxx(long) 增加时间系列的方法