复习:计算机中的时间
因为历史原因,比较混乱,演化至Java 8:
获取当前时间:
Date dNow = new Date(); //抽象类,没法new(),获得的是GregorianCalendar子类 Calendar cNow = Calendar.getInstance(); //当前日期和当前时间 LocalDate today = LocalDate.now(); LocalDateTime now = LocalDateTime.now();
获取年月日、星期几、小时分钟数等属性:
System.out.println(dNow.getYear()+1900); //从1900年开始的 System.out.println(dNow.getMonth()+1); //从0开始计算的 //System.out.println(dNow.getWeek()); 没有直接获取星期几的
System.out.println(cNow.get(Calendar.YEAR)); //年份正常 System.out.println(cNow.get(Calendar.MONTH)); //从0开始计算的 System.out.println(cNow.get(Calendar.DAY_OF_WEEK)); //从星期天开始算 System.out.println(cNow.get(Calendar.WEEK_OF_MONTH)); //第几个星期
System.out.println(today.getYear()); System.out.println(today.getMonth()); //返回的是枚举值 System.out.println(today.getMonth().getValue()); //从1开始 System.out.println(today.getMonthValue()); System.out.println(today.getDayOfWeek()); //返回枚举值 System.out.println(today.getDayOfWeek().getValue()); //星期一开始
设定时间:
new Date(2012, 3, 5); //deprecated //Calendar略显尴尬,需要先拿到一个当前时间 cNow.clear(); cNow.set(Calendar.DAY_OF_MONTH, 8); //直接of方法,指定年月日、小数分钟数等 LocalDateTime.of(2021, 8, 28, 12, 35);
SimpleDateFormat cn = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
DateTimeFormatter cn = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
Date now = Calendar.getInstance().getTime();
String strDate = "2021-08-13 21:43";转成Date:
Date dNow = cn.parse(strDate); //cn是SimpleDateFormat
Date再转成Calendar:
Calendar cNow = Calendar.getInstance(); cNow.setTime(dNow);
*LocalDateTime可以直接调用parse方法:
LocalDateTime.parse(strDate, cn); //不指定DateTimeFormatter使用当前地区的默认值
比较(compare):是之前(before),之后(after),还是同一时间
System.out.println(another.compareTo(now)); //返回:0/1/-1 //Date和Calendar System.out.println(now.after(another)); //返回:boolen System.out.println(now.before(another)); //*DateTime:方法名更规范 System.out.println(now.isBefore(another))
加减时间:add/plus、minus
//Calendar: //只有一个add()方法,参数指定增/减单位(field) //修改调用对象,返回void now.add(Calendar.DAY_OF_MONTH, 1); now.add(Calendar.MINUTE, -10); System.out.println(now.getTime()); //*DateTime:有plus,有minus,方法名直接表明增减单位 //可以连缀,返回加减后时间 System.out.println(now.minusDays(10).plusHours(2)); //调用对象本身不改变 System.out.println(now);
获取时间段(between):
System.out.println((now /* Date */ .getTime()-(new Date(110,0,1)).getTime()) / (365*24*60*60*1000L)); //注意这个L
Duration span = Duration.between(now /* LocalDateTime */, LocalDateTime.of(2019, 2, 18, 8, 0)); //能拿到日小时分钟等,拿不到年月等 System.out.println(span.toDays()); System.out.println(span.toHours()); Period span = Period.between(now /* LocalDate */, LocalDate.of(2019, 2, 18)); //能拿到年月日,拿不到小时分钟秒等 System.out.println(span.getYears()); System.out.println(span.getMonths());
Java 8 新引入的类,是能精确到纳秒级(1毫秒=1000000纳秒)的时间戳。
之前的Date和Calendar只有毫秒级的精度,他们都可以通过实例方法toInstant()获得其所对应的Instant对象,从而:
//没有Instant,就最多只能拿到毫秒 System.out.println(now.get(Calendar.MILLISECOND)); System.out.println(now.toInstant().getNano()); //*DateTime本来就可以拿到纳秒 System.out.println(LocalDateTime.now().getNano());
LocalDateTime time = //Instant本身没有时区历法概念,需要指定 LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());
多快好省!前端后端,线上线下,名师精讲
更多了解 加: