1.System.currentTimeMillis()
这个方法是获取系统的精确时间,获取的时间是一个时间戳的格式,以毫秒数构成的长串数字。可以使用SimpleDateFormat来设置显示的格式。
System.out.println(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateForma.format(System.currentTimeMillis()));

因为这个方法返回的是一个时间戳,个人觉得可以用来测试程序代码的执行时间。但是发现有人说这个方法的测试时间并不是非常的精确,因为currentTimeMills()方法内部调用的是native的方法,获取的时间精度会依赖于操作系统的实现机制。所以建议在需要非常精确的时间的话,慎用这个方法。
2.java.util.Date
使用Date来获取时间只需要调用默认构造器来获取对象,对象里面存储的是一个带星期,月份,时区时间格式。同样可以使用SimpleDateFormat来设置时间的显示格式。
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);
System.out.println(dateFormat.format(date));

3.java.util.Calendar
这种方法是通过获取Calendar的方法getInstance()来获取对象,因为Calendar是一个抽象类,不能直接创建对象。对象里面存储的是关于日期的各种信息,通过方法getTime()来获取对象里面的time字段的值,同样通过SimpleDateFormat来改变时间显示格式。
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
String pattern = "yyyy年MM月dd日 HH时mm分ss秒";
SimpleDateFormat calendarFormat = new SimpleDateFormat(pattern);
System.out.println(calendarFormat.format(calendar.getTime()));

4.Date/Time API
在Java8中引入了全新的日期/时间处理API,用来替代原有的老旧的Date和Calendar,这个新的API以下几种用法。
4.1 LocalDate
这个LcoalDate类,调用它的now()方法,可以获取系统当前的日期,如2019-04-10。不同于new Date(),可以获取日期和时间,而LocalDate类只能获取日期。
LocalDate dateNow = LocalDate.now();
System.out.println(dateNow);
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println(dateNow.format(dateFormat));
System.out.println(dateFormat.format(dateNow));
//获取当前时间的年份
System.out.println(dateNow.getYear());
//获取今天是这个月的第几天
System.out.println(dateNow.getDayOfMonth());
//获取今天是今年的第几天
System.out.println(dateNow.getDayOfYear());

从输出的结果可以看出LocalDate获取的值本身就是一个带有格式时间,已经很方便于我们理解,但是如果不喜欢这个格式,也可以通过DateTimeFormatter来设置自己喜欢的格式。
*在新的Data/Time API 中不能用老旧的SimpleDateFormat来设置时间格式,不然会出现java.lang.IllegalArgumentException异常。在我的代码中显示可以用LocalDate的对象调用format(),也可以使用DateTimeFormatter的对象调用format(),两者显示的结果是一样,具体的细微差别我并没有深入的研究。
4.2 LocalTime
LocalTime类的作用和LocalDate恰巧相反,一个获取日期,一个获取时间。
LocalTime timeNow = LocalTime.now();
System.out.println(timeNow);
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("ss:mm:HH");
System.out.println(timeNow.format(dateFormat));
System.out.println(dateFormat.format(timeNow));
//获取小时
System.out.println(timeNow.getHour());
//获取分钟
System.out.println(timeNow.getMinute());
//获取秒数
System.out.println(timeNow.getSecond());

LocalTime和LocalDate大同小异,用法上大致都是相同的,只是LocalTime获取了一个毫秒数。
4.3 LocalDateTime
相信看这个类的名字,我们也可以猜到,有了日期,有了时间,肯定是少不了日期加时间的。今天它来了,别问落地价。。。
LocalDateTime类相当于LocalDate和LocalTime的结合。
LocalDateTime dateTimeNow = LocalDateTime.now();
System.out.println(dateTimeNow);
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(dateTimeNow.format(dateTimeFormat));
System.out.println(dateTimeFormat.format(dateTimeNow));

在LocalDateTime中基本上都包含了LocalTime和LocalDate对象的方法。
总结:我现在一共就了解了这几种获取系统时间的方法,如果以后有新的发现会继续添加。Java8 新引入的API非常强大,还有很多对于时间处理的方法。因为这里只需要获取系统时间,就没有细致的去分析,有需要的小伙伴可以自己去查询文档。


被折叠的 条评论
为什么被折叠?



