目录
LocalDateTime -- JDK8 后提供日期时间类(年月日时分秒)
DateTimeFormatter -- JDK8 后提供格式化日期类
ChronoUnit -- JDK8 提供日期时间间隔工具类(推荐)
- // 1、创建一个Date类的对象:代表系统此刻日期时间对象
- Date d = new Date();
- // 2、获取时间毫秒值
- long time = d.getTime();
-
- // 1、得到当前时间
- Date d1 = new Date();
- System.out.println(d1);
-
- // 2、当前时间往后走 1小时 121s
- long time2 = System.currentTimeMillis();
- time2 += (60 * 60 + 121) * 1000;
-
- // 3、把时间毫秒值转换成对应的日期对象。
- Date d2 = new Date(time2);
-
- Date d3 = new Date();
- d3.setTime(time2);
- // 1、日期对象
- Date d = new Date();
- // 2、格式化这个日期对象 (指定最终格式化的形式)
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
- // 3、开始格式化日期对象成为喜欢的字符串形式
- String rs = sdf.format(d);
- // 4、格式化时间毫秒值
- // 需求:请问121秒后的时间是多少
- long time1 = System.currentTimeMillis() + 121 * 1000;
- String rs2 = sdf.format(time1);
-
- String dateStr = "2021年08月06日 11:11:11";
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
- Date d2 = sdf.parse(dateStr);
- //往后走2天 14小时 49分 06秒
- long time = d2.getTime() + (2L*24*60*60 + 14*60*60 + 49*60 + 6) * 1000;
- //格式化这个时间毫秒值就是结果
- System.out.println(sdf.format(time));
- // 1、拿到系统此刻日历对象
- Calendar cal = Calendar.getInstance();
- System.out.println(cal); //可以看到所有Calendar的field
- // 2、获取日历的信息:public int get(int field):取日期中的某个字段信息。
- int year = cal.get(Calendar.YEAR);
- int mm = cal.get(Calendar.MONTH) + 1;
- int days = cal.get(Calendar.DAY_OF_YEAR) ;
- // 3、public void set(int field,int value):修改日历的某个字段信息。
- cal.set(Calendar.HOUR , 12);
- // 4.public void add(int field,int amount):为某个字段增加/减少指定的值
- // 请问64天后是什么时间
- cal.add(Calendar.DAY_OF_YEAR , 64);
- cal.add(Calendar.MINUTE , 59);
- // 5.public final Date getTime(): 拿到此刻日期对象。
- Date d = cal.getTime();
- // 6.public long getTimeInMillis(): 拿到此刻时间毫秒值
- long time = cal.getTimeInMillis();
- // 1、获取本地日期对象。
- LocalDate nowDate = LocalDate.now();
- System.out.println("今天的日期:" + nowDate);//今天的日期:
- int year = nowDate.getYear();
- System.out.println("year:" + year);
- int month = nowDate.getMonthValue();
- System.out.println("month:" + month);
- int day = nowDate.getDayOfMonth();
- System.out.println("day:" + day);
-
- //当年的第几天
- int dayOfYear = nowDate.getDayOfYear();
- //星期
- System.out.println(nowDate.getDayOfWeek());
- System.out.println(nowDate.getDayOfWeek().getValue());
- //月份
- System.out.println(nowDate.getMonth());//AUGUST
- System.out.println(nowDate.getMonth().getValue());//8
-
- System.out.println("------------------------");
- LocalDate bt = LocalDate.of(1991, 11, 11);
- System.out.println(LocalDate.of(1991, Month.NOVEMBER, 11));//相对上面只是把月换成了枚举
-
- LocalDate myDate = LocalDate.of(2018, 9, 5);
- System.out.println("今天是2018-09-06吗? " + nowDate.equals(myDate));//今天是2018-09-06吗? false
- System.out.println(myDate + "是否在" + nowDate + "之前? " + myDate.isBefore(nowDate));//2018-09-05是否在2018-09-06之前? true
- System.out.println(myDate + "是否在" + nowDate + "之后? " + myDate.isAfter(nowDate));//2018-09-05是否在2018-09-06之后? false
-
- // 判断今天是否是你的生日
- LocalDate birDate = LocalDate.of(1996, 8, 5);
- LocalDate nowDate1 = LocalDate.now();
-
- MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());
- MonthDay nowMd = MonthDay.from(nowDate1);
-
- System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));//今天是你的生日吗? false
- // 1、获取本地时间对象。
- LocalTime nowTime = LocalTime.now();
- System.out.println("今天的时间:" + nowTime);//今天的时间:
- int hour = nowTime.getHour();//时
- int minute = nowTime.getMinute();//分
- int second = nowTime.getSecond();//秒
- int nano = nowTime.getNano();//纳秒
-
- System.out.println(nowTime.minusHours(1));//一小时前
- System.out.println(nowTime.minusMinutes(1));//一分钟前
- System.out.println(nowTime.minusSeconds(1));//一秒前
- System.out.println(nowTime.minusNanos(1));//一纳秒前
-
- System.out.println(nowTime.plusHours(1));//一小时后
- System.out.println(nowTime.plusMinutes(1));//一分钟后
- System.out.println(nowTime.plusSeconds(1));//一秒后
- System.out.println(nowTime.plusNanos(1));//一纳秒后
-
- // 不可变对象,每次修改产生新对象!
- System.out.println(nowTime);
-
- System.out.println("-----");
- System.out.println(LocalTime.of(8, 20));//时分
- System.out.println(LocalTime.of(8, 20, 30));//时分秒
- System.out.println(LocalTime.of(8, 20, 30, 150));//时分秒纳秒
- LocalTime mTime = LocalTime.of(8, 20, 30, 150);
- // 日期 时间
- LocalDateTime nowDateTime = LocalDateTime.now();
- System.out.println("今天是:" + nowDateTime);//今天是:
- System.out.println(nowDateTime.getYear());//年
- System.out.println(nowDateTime.getMonthValue());//月
- System.out.println(nowDateTime.getDayOfMonth());//日
- System.out.println(nowDateTime.getHour());//时
- System.out.println(nowDateTime.getMinute());//分
- System.out.println(nowDateTime.getSecond());//秒
- System.out.println(nowDateTime.getNano());//纳秒
- //日:当年的第几天
- System.out.println("dayOfYear:" + nowDateTime.getDayOfYear());//dayOfYear:249
- //星期
- System.out.println(nowDateTime.getDayOfWeek());//THURSDAY
- System.out.println(nowDateTime.getDayOfWeek().getValue());//4
- //月份
- System.out.println(nowDateTime.getMonth());//SEPTEMBER
- System.out.println(nowDateTime.getMonth().getValue());//9
-
-
- LocalDate ld = nowDateTime.toLocalDate();
- System.out.println(ld);
-
- LocalTime lt = nowDateTime.toLocalTime();
- System.out.println(lt.getHour());
- System.out.println(lt.getMinute());
- System.out.println(lt.getSecond());
- // 1、得到一个Instant时间戳对象
- Instant instant = Instant.now();
- System.out.println(instant);
-
- // 2、系统此刻的时间戳怎么办?
- Instant instant1 = Instant.now();
- System.out.println(instant1.atZone(ZoneId.systemDefault()));
-
- // 3、如何去返回Date对象
- Date date = Date.from(instant);
- System.out.println(date);
-
- Instant i2 = date.toInstant();
- System.out.println(i2);
- // 本地此刻 日期时间 对象
- LocalDateTime ldt = LocalDateTime.now();
- System.out.println(ldt);
-
- // 解析/格式化器
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a");
- // 正向格式化
- System.out.println(dtf.format(ldt));
- // 逆向格式化
- System.out.println(ldt.format(dtf));
-
- // 解析字符串时间
- DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- // 解析当前字符串时间成为本地日期时间对象
- LocalDateTime ldt1 = LocalDateTime.parse("2019-11-11 11:11:11" , dtf1);
- System.out.println(ldt1);
- System.out.println(ldt1.getDayOfYear());
- // 当前本地 年月日
- LocalDate today = LocalDate.now();
- System.out.println(today);//
-
- // 生日的 年月日
- LocalDate birthDate = LocalDate.of(1998, 10, 13);
- System.out.println(birthDate);
-
- Period period = Period.between(birthDate, today);//第二个参数减第一个参数
-
- System.out.println(period.getYears());
- System.out.println(period.getMonths());
- System.out.println(period.getDays());
- // 本地日期时间对象。
- LocalDateTime today = LocalDateTime.now();
- System.out.println(today);
-
- // 出生的日期时间对象
- LocalDateTime birthDate = LocalDateTime.of(2021,8
- ,06,01,00,00);
-
- System.out.println(birthDate);
-
- Duration duration = Duration.between( today , birthDate);//第二个参数减第一个参数
-
- 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());//两个时间差的纳秒数
- // 本地日期时间对象:此刻的
- LocalDateTime today = LocalDateTime.now();
- System.out.println(today);
-
- // 生日时间
- LocalDateTime birthDate = LocalDateTime.of(1990,10,1,
- 10,50,59);
- System.out.println(birthDate);
-
- System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));
- System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));
- System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
- System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today));
- System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today));
- System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today));
- System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today));
- System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today));
- System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today));
- System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today));
- System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today));
- System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today));
- System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today));
- System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today));
- System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));