In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use the time class of Java 8". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the time class of Java 8".
Why do you add so many time classes to Jdk8
Non-thread safety
Java.util.Date is not thread-safe, and all date classes are mutable, which is one of the biggest problems with Java date classes.
Date date = new Date (); for (int I = 0; I
< 100; i++) { new Thread(new Runnable() { @Override public void run() { // 举例 int time = new Random().nextInt(100); date.setTime(time); System.out.println( Thread.currentThread().getId() + " = " + time); System.out.println( Thread.currentThread().getId() + " = " + date.getTime()); } }).start(); } SimpleDateFormat格式化工具也是一样,阿里巴巴规约中建议将SimpleDateFormat放到ThreadLocal中。 而java8中日期和时间基本都被设计final,final修饰的类,天然线程安全。 设计很差 java.util.Date同时包含日期和时间,而java.sql.Date仅包含日期,两个类又有相同的名字,令人匪夷所思。 java.util.Date代表时间线上的一个瞬间(包含了从Unix新纪元到现在的总毫秒数),但是如果调用了Date的toString(),返回值会提示它是带着时区的,这也会让开发者感到疑惑。 时区、日期计算处理麻烦 日期类并不提供国际化,没有时区支持,因此Java引入了java.util.Calendar和java.util.TimeZone类,但他们同样存在上述所有的问题,使用复杂,不直观 // 获取当前时间日历 +8时区 Calendar calendar = Calendar.getInstance(); // 毫秒数 calendar.setTimeInMillis(1601186434000L); // 时区转到 utc 时间 calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET); int zoneOffset = calendar.get(Calendar.ZONE_OFFSET); calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); // 时区转到对应的时区 calendar.setTimeZone(TimeZone.getTimeZone("GMT+5:00")); int dstOffset1 = calendar.get(java.util.Calendar.DST_OFFSET); int zoneOffset1= calendar.get(Calendar.ZONE_OFFSET); calendar.add(java.util.Calendar.MILLISECOND, (zoneOffset1 + dstOffset1)); // 时间计算 calendar.add(Calendar.HOUR,15); // 日期计算 calendar.add(Calendar.DAY_OF_MONTH, -1); // 时区计算 calendar.add(Calendar.ZONE_OFFSET, 3); // 周几 int week = calendar.get(Calendar.DAY_OF_WEEK); 基于上述的原因,java8重新提供一套时间类,下面来看一下相关类 java8 日期、时间常见类 ZoneId 地区 Asia/Shanghai、Europe/Paris ZoneOffset 偏移数据 +8:00 Instant 它代表的是时间戳 Duration 它表示秒或纳秒时间间隔 Period 表示一段时间的年、月、日,开使用between()方法获取两个日期之间的差作为Period 对象返回 LocalDate 不包含具体时间的日期,比如2014-01-14。它可以用来存储生日,周年纪念日,入职日期等。 LocalTime 它代表的是不含日期的时间 LocalDateTime 它包含了日期及时间,不过还是没有偏移信息或者说时区。 ZonedDateTime 这是一个包含时区的完整的日期时间,偏移量是以UTC/格林威治时间为基准的。 OffsetDateTime 类实际上包含了LocalDateTime与ZoneOffset DateTimeFormatter 日期的格式化与解析,与SimpleDateFormat不同,它是不可变且线程安全的 TemporalAdjusters 类中包含许多常用的静态方法,避免自己编写工具类 时间类关系图 常见类的操作示例 ZoneId zoneId = ZoneId.systemDefault(); System.out.println(zoneId);//Asia/Shanghai ZoneOffset zoneOffset = ZoneOffset.ofHours(8); System.out.println(zoneOffset);//+08:00 Instant instant = Instant.ofEpochSecond(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8))); System.out.println(instant.getEpochSecond());//1605596559 Duration duration = Duration.between(LocalDateTime.now(), LocalDateTime.now().plusHours(1)); System.out.println(duration.getSeconds());//3600 Period period = Period.between(LocalDate.now(),LocalDate.now().plusDays(1)); System.out.println(period.getDays());//1 LocalDate date = LocalDate.now(); System.out.println(date);//2020-11-17 LocalTime time = LocalTime.now(); System.out.println(time);//15:02:39.067 LocalDateTime now = LocalDateTime.now(); System.out.println(now);//2020-11-17T15:02:39.06 ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), zoneId); System.out.println(zonedDateTime);//2020-11-17T15:02:39.067+08:00[Asia/Shanghai] OffsetDateTime offsetDateTime = OffsetDateTime.of(LocalDateTime.now(), ZoneOffset.ofHours(8)); System.out.println(offsetDateTime);//2020-11-17T15:02:39.068+08:00 String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(offsetDateTime); System.out.println(format);//2020-11-17 15:02:39 TemporalAdjuster temporalAdjuster = TemporalAdjusters.firstDayOfMonth(); System.out.println(temporalAdjuster.adjustInto(LocalDate.now()));//2020-11-01 特别说明 ZoneId、ZoneOffset主要表示时区和偏移 Instant 表示时间戳 Duration、Period 表示时间差,前者表示时间差,后者表示日期差 LocalDate、LocalTime、LocalDateTime表示日期、时间、日期+时间 ZonedDateTime、OffsetDateTime含时区信息的时间 Java8的方便之处 提供了很多时间、日期计算的方法,非常直观Time utility classes such as TemporalAdjusters are also provided with built-in methods.
Thank you for your reading, the above is the content of "how to use the time class of Java 8". After the study of this article, I believe you have a deeper understanding of how to use the time class of Java 8, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.