第一章 正则表达式
1.1 正则表达式的概念
1.2 正则表达式-字符类
- 语法示例:
abc:代表a或者b,或者c字符中的一个。
\^abc:代表除a,b,c以外的任何字符。
a-z:代表a-z的所有小写字符中的一个。
A-Z:代表A-Z的所有大写字符中的一个。
0-9:代表0-9之间的某一个数字字符。
a-zA-Z0-9:代表a-z或者A-Z或者0-9之间的任意一个字符。
a-dm-p:a 到 d 或 m 到 p之间的任意一个字符。
1.3 正则表达式-逻辑运算符
语法示例:
&&:并且
| :或者
\ :转义字符
1.4 正则表达式-预定义字符
语法示例:
"." : 匹配任何字符。
"\d":任何数字0-9的简写;
"\D":任何非数字\^0-9的简写;
"\s": 空白字符: \\t\\n\\x0B\\f\\r 的简写
"\S": 非空白字符:\^\\s 的简写
"\w":单词字符:a-zA-Z_0-9的简写
"\W":非单词字符:\^\\w
1.5 正则表达式-数量词
语法示例:
X? : 0次或1次
X* : 0次到多次
X+ : 1次或多次
X{n} : 恰好n次
X{n,} : 至少n次
X{n,m}: n到m次(n和m都是包含的)
第二章 JDK8时间相关类
| JDK8时间类类名 | 作用 |
|---|---|
| ZoneId | 时区 |
| Instant | 时间戳 |
| ZoneDateTime | 带时区的时间 |
| DateTimeFormatter | 用于时间的格式化和解析 |
| LocalDate | 年、月、日 |
| LocalTime | 时、分、秒 |
| LocalDateTime | 年、月、日、时、分、秒 |
| Duration | 时间间隔(秒,纳,秒) |
| Period | 时间间隔(年,月,日) |
| ChronoUnit | 时间间隔(所有单位) |
2.1 ZoneId 时区
/* static Set<string> getAvailableZoneIds() 获取Java中支持的所有时区 static ZoneId systemDefault() 获取系统默认时区 static Zoneld of(string zoneld) 获取一个指定时区 */ //1.获取所有的时区名称 Set<String> zoneIds = ZoneId.getAvailableZoneIds(); System.out.println(zoneIds.size());//600 System.out.println(zoneIds);// Asia/Shanghai //2.获取当前系统的默认时区 ZoneId zoneId = ZoneId.systemDefault(); System.out.println(zoneId);//Asia/Shanghai //3.获取指定的时区 ZoneId zoneId1 = ZoneId.of("Asia/Pontianak"); System.out.println(zoneId1);//Asia/Pontianak
2.2 Instant 时间戳
/* static Instant now() 获取当前时间的Instant对象(标准时间) static Instant ofXxxx(long epochMilli) 根据(秒/毫秒/纳秒)获取Instant对象 ZonedDateTime atZone(ZoneIdzone) 指定时区 boolean isxxx(Instant otherInstant) 判断系列的方法 Instant minusXxx(long millisToSubtract) 减少时间系列的方法 Instant plusXxx(long millisToSubtract) 增加时间系列的方法 */ //1.获取当前时间的Instant对象(标准时间) Instant now = Instant.now(); System.out.println(now); //2.根据(秒/毫秒/纳秒)获取Instant对象 Instant instant1 = Instant.ofEpochMilli(0L); System.out.println(instant1);//1970-01-01T00:00:00z Instant instant2 = Instant.ofEpochSecond(1L); System.out.println(instant2);//1970-01-01T00:00:01Z Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L); System.out.println(instant3);//1970-01-01T00:00:027 //3. 指定时区 ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai")); System.out.println(time); //4.isXxx 判断 Instant instant4=Instant.ofEpochMilli(0L); Instant instant5 =Instant.ofEpochMilli(1000L); //5.用于时间的判断 //isBefore:判断调用者代表的时间是否在参数表示时间的前面 boolean result1=instant4.isBefore(instant5); System.out.println(result1);//true //isAfter:判断调用者代表的时间是否在参数表示时间的后面 boolean result2 = instant4.isAfter(instant5); System.out.println(result2);//false //6.Instant minusXxx(long millisToSubtract) 减少时间系列的方法 Instant instant6 =Instant.ofEpochMilli(3000L); System.out.println(instant6);//1970-01-01T00:00:03Z Instant instant7 =instant6.minusSeconds(1); System.out.println(instant7);//1970-01-01T00:00:02Z
2.3 ZoneDateTime 带时区的时间
/* static ZonedDateTime now() 获取当前时间的ZonedDateTime对象 static ZonedDateTime ofXxxx(。。。) 获取指定时间的ZonedDateTime对象 ZonedDateTime withXxx(时间) 修改时间系列的方法 ZonedDateTime minusXxx(时间) 减少时间系列的方法 ZonedDateTime plusXxx(时间) 增加时间系列的方法 */ //1.获取当前时间对象(带时区) ZonedDateTime now = ZonedDateTime.now(); System.out.println(now); //2.获取指定的时间对象(带时区)1/年月日时分秒纳秒方式指定 ZonedDateTime time1 = ZonedDateTime.of(2023, 10, 1, 11, 12, 12, 0, ZoneId.of("Asia/Shanghai")); System.out.println(time1); //通过Instant + 时区的方式指定获取时间对象 Instant instant = Instant.ofEpochMilli(0L); ZoneId zoneId = ZoneId.of("Asia/Shanghai"); ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId); System.out.println(time2); //3.withXxx 修改时间系列的方法 ZonedDateTime time3 = time2.withYear(2000); System.out.println(time3); //4. 减少时间 ZonedDateTime time4 = time3.minusYears(1); System.out.println(time4); //5.增加时间 ZonedDateTime time5 = time4.plusYears(1); System.out.println(time5);
2.4DateTimeFormatter 用于时间的格式化和解析
/* static DateTimeFormatter ofPattern(格式) 获取格式对象 String format(时间对象) 按照指定方式格式化 */ //获取时间对象 ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai")); // 解析/格式化器 DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm;ss EE a"); // 格式化 System.out.println(dtf1.format(time));
2.5LocalDate 年、月、日
//1.获取当前时间的日历对象(包含 年月日) LocalDate nowDate = LocalDate.now(); //System.out.println("今天的日期:" + nowDate); //2.获取指定的时间的日历对象 LocalDate ldDate = LocalDate.of(2023, 1, 1); System.out.println("指定日期:" + ldDate); System.out.println("============================="); //3.get系列方法获取日历中的每一个属性值//获取年 int year = ldDate.getYear(); System.out.println("year: " + year); //获取月//方式一: Month m = ldDate.getMonth(); System.out.println(m); System.out.println(m.getValue()); //方式二: int month = ldDate.getMonthValue(); System.out.println("month: " + month); //获取日 int day = ldDate.getDayOfMonth(); System.out.println("day:" + day); //获取一年的第几天 int dayofYear = ldDate.getDayOfYear(); System.out.println("dayOfYear:" + dayofYear); //获取星期 DayOfWeek dayOfWeek = ldDate.getDayOfWeek(); System.out.println(dayOfWeek); System.out.println(dayOfWeek.getValue()); //is开头的方法表示判断 System.out.println(ldDate.isBefore(ldDate)); System.out.println(ldDate.isAfter(ldDate)); //with开头的方法表示修改,只能修改年月日 LocalDate withLocalDate = ldDate.withYear(2000); System.out.println(withLocalDate); //minus开头的方法表示减少,只能减少年月日 LocalDate minusLocalDate = ldDate.minusYears(1); System.out.println(minusLocalDate); //plus开头的方法表示增加,只能增加年月日 LocalDate plusLocalDate = ldDate.plusDays(1); System.out.println(plusLocalDate); //------------- // 判断今天是否是你的生日 LocalDate birDate = LocalDate.of(2000, 1, 1); LocalDate nowDate1 = LocalDate.now(); MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth()); MonthDay nowMd = MonthDay.from(nowDate1); System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));//今天是你的生日吗?
2.6 LocalTime 时、分、秒
// 获取本地时间的日历对象。(包含 时分秒) LocalTime nowTime = LocalTime.now(); System.out.println("今天的时间:" + nowTime); int hour = nowTime.getHour();//时 System.out.println("hour: " + hour); int minute = nowTime.getMinute();//分 System.out.println("minute: " + minute); int second = nowTime.getSecond();//秒 System.out.println("second:" + second); int nano = nowTime.getNano();//纳秒 System.out.println("nano:" + nano); 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); //is系列的方法 System.out.println(nowTime.isBefore(mTime)); System.out.println(nowTime.isAfter(mTime)); //with系列的方法,只能修改时、分、秒 System.out.println(nowTime.withHour(10)); //plus系列的方法,只能修改时、分、秒 System.out.println(nowTime.plusHours(10));
2.7 LocalDateTime 年、月、日、时、分、秒
// 当前时间的的日历对象(包含年月日时分秒) 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()); //星期 System.out.println(nowDateTime.getDayOfWeek()); System.out.println(nowDateTime.getDayOfWeek().getValue()); //月份 System.out.println(nowDateTime.getMonth()); System.out.println(nowDateTime.getMonth().getValue()); 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());
2.8 Duration 时间间隔(秒,纳,秒)
// 本地日期时间对象。 LocalDateTime today = LocalDateTime.now(); System.out.println(today); // 出生的日期时间对象 LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0); System.out.println(birthDate); Duration duration = Duration.between(birthDate, today);//第二个参数减第一个参数 System.out.println("相差的时间间隔对象:" + duration); System.out.println("============================================"); 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());//两个时间差的纳秒数
2.9 Period 时间间隔(年,月,日)
// 当前本地 年月日 LocalDate today = LocalDate.now(); System.out.println(today); // 生日的 年月日 LocalDate birthDate = LocalDate.of(2000, 1, 1); System.out.println(birthDate); Period period = Period.between(birthDate, today);//第二个参数减第一个参数 System.out.println("相差的时间间隔对象:" + period); System.out.println(period.getYears()); System.out.println(period.getMonths()); System.out.println(period.getDays()); System.out.println(period.toTotalMonths());
2.10 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));
第三章 包装类
3.1 概述
| 基本类型 | 对应的包装类(位于java.lang包中) |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
3.2 Integer类
-
Integer类概述
包装一个对象中的原始类型 int 的值
-
Integer类构造方法及静态方法
| 方法名 | 说明 |
|---|---|
| public Integer(int value) | 根据 int 值创建 Integer 对象(过时) |
| public Integer(String s) | 根据 String 值创建 Integer 对象(过时) |
| public static Integer valueOf(int i) | 返回表示指定的 int 值的 Integer 实例 |
| public static Integer valueOf(String s) | 返回保存指定String值的 Integer 对象 |
| static string tobinarystring(int i) | 得到二进制 |
| static string tooctalstring(int i) | 得到八进制 |
| static string toHexstring(int i) | 得到十六进制 |
| static int parseInt(string s) | 将字符串类型的整数转成int类型的整数 |
3.3 装箱与拆箱
基本类型与对应的包装类对象之间,来回转换的过程称为"装箱"与"拆箱":
-
装箱:从基本类型转换为对应的包装类对象。
-
拆箱:从包装类对象转换为对应的基本类型。
用Integer与 int为例:(看懂代码即可)
基本数值---->包装对象
Integer i = new Integer(4);//使用构造函数函数 Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法
包装对象---->基本数值
int num = i.intValue();
3.4 自动装箱与自动拆箱
由于我们经常要做基本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。例如:
Integer i = 4;//自动装箱。相当于Integer i = Integer.valueOf(4); i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5; //加法运算完成后,再次装箱,把基本数值转成对象。
3.5 基本类型与字符串之间的转换
基本类型转换为String
-
转换方式
-
方式一:直接在数字后加一个空字符串
-
方式二:通过String类静态方法valueOf()
-
示例代码
public class IntegerDemo { public static void main(String[] args) { //int --- String int number = 100; //方式1 String s1 = number + ""; System.out.println(s1); //方式2 //public static String valueOf(int i) String s2 = String.valueOf(number); System.out.println(s2); System.out.println("--------"); } }
String转换成基本类型
除了Character类之外,其他所有包装类都具有parseXxx静态方法可以将字符串参数转换为对应的基本类型:
public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。
public static short parseShort(String s):将字符串参数转换为对应的short基本类型。
public static int parseInt(String s):将字符串参数转换为对应的int基本类型。
public static long parseLong(String s):将字符串参数转换为对应的long基本类型。
public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。
public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。
public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean基本类型。
代码使用(仅以Integer类的静态方法parseXxx为例)如:
-
转换方式
-
方式一:先将字符串数字转成Integer,再调用valueOf()方法
-
方式二:通过Integer静态方法parseInt()进行转换
-
3.6 底层原理
建议:获取Integer对象的时候不要自己new,而是采取直接赋值或者静态方法valueOf的方式
因为在实际开发中,-128~127之间的数据,用的比较多。如果每次使用都是new对象,那么太浪费内存了。
所以,提前把这个范围之内的每一个数据都创建好对象,如果要用到了不会创建新的,而是返回已经创建好的对象。