1. 首页
  2. > 海外移民投资 >

8月份日历(8月份是什么天)

JDK8新增日期(重要)

旧版日期时间 API 存在的问题


  1. 设计很差: 在Java.util和java.sql的包中都有日期类,java.util.Date同时包含日期和时间,而java.sql.Date仅包含日期。此外用于格式化和解析的类在java.text包中定义。
  2. 非线程安全:java.util.Date 是非线程安全的,所有的日期类都是可变的,这是Java日期类最大的问题之一。
  3. 时区处理麻烦:日期类并不提供国际化,没有时区支持,因此Java引入了java.util.Calendar和 java.util.TimeZone类,但他们同样存在上述所有的问题。

日期

获取当前日期


LocalDate nowDate = LocalDate.now(); System.out.println(nowDate); //2021-10-12

获取年月日


int year = nowDate.getYear(); int month = nowDate.getMonthValue(); int day= nowDate.getDayOfMonth(); System.out.println(year "---" month "---" day); //2021---10---12

获取当月、星期、年的第几天


int dayOfMonth = nowDate.getDayOfMonth(); DayOfWeek dayOfWeek = nowDate.getDayOfWeek(); int dayOfYear = nowDate.getDayOfYear(); System.out.println(dayOfMonth "--" dayOfYear "--" dayOfWeek);//12--285--TUESDAY

当显示英文时,用getValue会获取到数字


int value = nowDate.getMonth().getValue(); System.out.println(value); //10

自己设定时间


LocalDate of = LocalDate.of(2021, 11, 12); System.out.println(of); //2021-11-12

判断时间,返回true/false


System.out.println("今天是2021.11.12吗" nowDate.equals(of)); System.out.println("在之前么" of.isBefore(nowDate)); System.out.println("在之后么" of.isAfter(nowDate)); System.out.println("今年是闰年么" nowDate.isLeapYear());

判断今天是否是你的生日


LocalDate birDate = LocalDate.of(1998, 10, 12); LocalDate now = LocalDate.now(); MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth()); MonthDay from = MonthDay.from(now); Sy是什么stem.out.println("今天是你的生日吗?" birMd.equals(from));

时间

获取时间


LocalTime nowTime = LocalTime.now(); System.out.println(nowTime); //09:50:24.529 int hour = nowTime.getHour(); int minute = nowTime.getMinute(); int second = nowTime.getSecond(); int nano = nowTime.getNano(); System.out.println(hour "--" minute "--" second "--" nano);

时间运算


minus是减,plus是加 注意:nowTime是不可变对象,每次进行运算时,都会创建一个新对象


LocalTime localTime = nowTime.minusHours(1); LocalTime localTime1 = nowTime.plusMinutes(2);

时间和日期

LocalDateTime now = LocalDateTime.now();//前面的所有方法都能调用 int dayOfMonth = now.getDayOfMonth(); DayOfWeek dayOfWeek = now.getDayOfWeek(); int dayOfYear = now.getDayOfYear(); int year = now.getYear(); int hour = now.getHour();

时间戳

//得到一个时间戳对象,获取到的是美国的时间2021-10-12T02:20:28.403Z Instant now = Instant.now(); System.out.println(now);//得到系统此时的时间戳2021-10-12T10:20:28.445 08:00[Asia/Shanghai] Instant now1 = Instant.now(); System.out.println(now1.atZone(ZoneId.systemDefault())); //时间戳和日期对象转化,了解 Date date = Date.from(now); System.out.println(date); //Tue Oct 12 10:24:12 CST 2021 Instant instant = date.toInstant(); System.out.println(instant);//2021-10-12T02:24:12.397Z

日期格式化

  • 在JDK8中,引入了一个全新的日期与时间格式化器DateTimeFormatter
  • 正反都能调用format方法

//日期时间格式化 LocalDateTime ldt = LocalDateTime.now(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a"); //正向格式化 String ldt1 = dtf.format(ldt); System.out.println(ldt1); //2021-10-12 10:29:55 星期二 上午 //逆向格式化 System.out.println(ldt.format(dtf)); //2021-10-12 10:31:42 星期二 上午 //解析字符时间 DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String dateStr = "2019-11-11 11:11:11"; //解析当前字符串时间成为本地时间对象(z) LocalDateTime parse = LocalDateTime.parse(date天Str, dtf1); System.out.println(parse); //2019-11-11T11:11:11 System.out.println(parse.getYear()); //可以获取到时间了,转化成功

获取时间间隔

Period


  • 在Jdk8中,我们使用ja8月份va.time.Period计算日期间隔
  • 主要是用Period类方法getYears()、getMonths()、和getDays()来计算,只能精确到年月日
  • 用于LocalDate之间的比较

//计算时间间隔 LocalDate now = LocalDate.now(); // 2021-10-12 LocalDate bir = LocalDate.of(1998, 10, 4);8月份 //日历1998-10-4 Period period = Period.between(bir, now); //第二个参数减去第一个参数 System.out.println(period);//相隔 23年 0月 8天 System.out.println(period.getYears()); System.out.println(period.getMonths()); System.out.println(period.getDays());

Duration


  • 在Jdk8中,我们可以使用java.time.Duration来计算时间间隔
  • 它提供了使用基于时间的值测量时间量的方法
  • 用于LocalDateTime之间的比较。也可以用于Instant之间的比较

LocalDateTime today = LocalDateTime.now(); LocalDateTime bir = LocalDateTime.of(2021, 3, 14, 20, 1, 0); Duration duration = D天uration.between(bir, today); //后一个参数减去前一个参数 System.out.println(duration.toDays()); //相差天数 System.out.println(duration.toHours()); //相差小时 System.out.println(duration.toMinutes()); //相差分钟 System.out.println(duration.toMillis()); //相差毫秒数 System.out.println(duration.toNanos()); //相差纳秒数

ChronoUnit能获取所有的时间间隔,用法如下


long year = ChronoUnit.YEARS.between(bir, today); long day = ChronoUnit.DAYS.between(bir, today); System.out.println(year "-" day);

设置日期时间的时区

@Test public void test10() { // 1.获取所有的时区ID // ZoneId.getAvailableZoneIds().forEa是什么ch(System.out::println); // 不带时间,获取计算机的当前时间 LocalDateTime now = LocalDateTime.now(); //中国使用的东八区的时区.比标准时间早8个小时 System.out.println("now = " now); //now = 2021-11-03T15:20:59.741 // 2.操作带时区的类 // now(Clock.systemUTC()): 创建世界标准时间 ZonedDateTime bz = ZonedDateTime.now(Clock.systemUTC()); System.out.println("bz = " bz); //bz = 2021-11-03T07:20:59.742Z // now(): 使用计算机的默认的时区,创建日期时间 ZonedDateTime now1 = ZonedDateTime.now(); System.out.println("now1 = " now1); //now1 = 2021-11-03T15:20:59.742 08:00[Asia/Shanghai] // 使用指定的时区创建日期时间 ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Vancouver")); System.out.println("now2 = " now2); 日历//now2 = 2021-11-03T00:20:59.743-07:00[America/Vancouver] // 修改时区 // withZoneSameInstant: 即更改时区,也更改时间 ZonedDateTime withZoneSameInstant = now2.withZoneSameInstant(ZoneId.of("Asia/Shanghai")); System.out.println("withZoneSameInstant = " withZoneSameInstant); //2021-11-03T15:20:59.743 08:00[Asia/Shanghai] // withZoneSameLocal: 只更改时区,不更改时间 ZonedDateTime withZoneSameLocal = now2.withZoneSameLocal(ZoneId.of("Asia/Shanghai")); System.out.println("withZoneSameLocal = " withZoneSameLocal); //2021-11-03T00:20:59.743 08:00[Asia/Shanghai] }




版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123456@qq.com 举报,一经查实,本站将立刻删除。

联系我们

工作日:9:30-18:30,节假日休息