Day15—常用API-3

📅 发布时间:2026/7/10 4:00:26 👁️ 浏览次数:
Day15—常用API-3
JDK7时间Date时间相关知识点Date时间类java.util.Date类 表示特定的瞬间精确到毫秒。两个构造函数public Date()从运行程序的此时此刻到时间原点经历的毫秒值,转换成Date对象分配Date对象并初始化此对象以表示分配它的时间精确到毫秒。public Date(long date)将指定参数的毫秒值date,转换成Date对象分配Date对象并初始化此对象以表示自从标准基准时间称为“历元epoch”即1970年1月1日00:00:00 GMT以来的指定毫秒数。常用方法 public long getTime()把日期对象转换成对应的时间毫秒值。public void setTime(long time)把方法参数给定的毫秒值设置给日期对象SimpleDateFormat格式化按照指定的格式把Date对象转换为String对象。解析按照指定的格式把String对象转换为Date对象。构造方法与常用方法常用的格式规则为标识字母区分大小写含义y年M月d日H时m分s秒CalendarCalendar代表了系统当前时间的日历对象可以单独修改、获取时间中的年、月、日细节Calendar是一个后向类不能直接创建对象两种方式可以获取GregorianCalendar对象直接创建GregorianCalendar对象通过Calendar的静态方法getInstance()方法获取GregorianCalendar对象Calendar常用方法细节点日历类中月份的范围0~11 日历类中星期的特点星期日十一周中的第一天JDK8时间相关类ZoneId 时区Instant 时间戳ZoneDateTime 带时区的时间DateTimeFormatter 用于时间的格式化和解析LocalDate年月日、LocalTime时分秒、LocalDateTime年月日时分秒LocalDateTime可以转换为LocalDate和LocalTimeDuration(秒纳秒)、Period 时间间隔(年月日)、ChronoUnit 时间间隔所有单位ChronoUnit// 当前时间 LocalDateTime today LocalDateTime.now(); System.out.println(today); // 生日时间 LocalDateTime birthDate LocalDateTime.of(2000, 1, 1,0, 0, 0); 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));包装类包装类基本数据类型对应的引用类型-对象基本类型对应的包装类位于java.lang包中byteByteshortShortintIntegerlongLongfloatFloatdoubleDoublecharCharacterbooleanBoolean如何获取包装类对象直接赋值Integer成员方法细节1:在类型转换的时候括号中的参数只能是数字不能是其他否则代码会报错2:8种包装类当中除了Character都有对应的parseXxx的方法进行类型转换练习自己实现parseInt方法的效果将字符串形式的数据转成整数。要求:字符串中只能是数字不能有其他字符最少一位最多10位日不能开头public class Test2 { public static void main(String[] args) { /* 自己实现parseInt方法的效果将字符串形式的数据转成整数。要求: 字符串中只能是数字不能有其他字符最少一位最多10位日不能开头 */ //1.定义一个字符串 String str 123; //2.校验字符串 //习惯:会先把异常数据进行过滤剩下来就是正常的数据。 if (!str.matches([1-9]\\d{0,9})) { //错误的数据 System.out.println(数据格式有误); } else { //正确的数据 System.out.println(数据格式正确); //3.定义一个变量表示最终的结果 int number 0; //4.遍历字符串得到里面的每一个字符 for (int i 0; i lt; str.length(); i) { int c str.charAt(i) - 0;//把每一位数字放到number当中 number number * 10 c; } System.out.println(number); System.out.println(number 1); } } }判断任意的一个年份是闰年还是平年要求:用JDK7和JDK8两种方式判断提示:二月有29天是闰年一年有366天是闰年public class Test5 { public static void main(String[] args) { /* 判断任意的一个年份是闰年还是平年要求:用JDK7和JDK8两种方式判断提示: 二月有29天是闰年一年有366天是闰年 */ //jdk7 //我们可以把时间设置为2000年3月1日 Calendar c Calendar.getInstance(); c.set(2000, 2, 1); //月份的范围:0~11 //再把日历往前减一天 c.add(Calendar.DAY_OF_MONTH, -1); //看当前的时间是28号还是29号? int day c.get(Calendar.DAY_OF_MONTH); System.out.println(day); //jdk8 //月份的范围:1~12 //设定时间为2000年的3月1日 LocalDate ld LocalDate.of(2001, 3, 1); //把时间往前减一天 LocalDate ld2 ld.minusDays(1); //获取这一天是一个月中的几号 int day2 ld2.getDayOfMonth(); System.out.println(day2); //true:闰年 //false:平年 System.out.println(ld.isLeapYear()); } }