Java学习笔记13
1.Date类
1.1 概述
java.util.Date类表示特定的瞬间,精确到毫秒。
1.2 构造方法
Date类有多个构造方法,部分已经过时。
| 方法 | 作用 |
|---|---|
| public Date() | 从此刻到计算机时间原点(1970年1月1日00:00:00 GMT)经历的毫秒值,转换成Date对象。 |
| public Date(long date) | 将指定参数的毫秒值date转换成Date对象,以表示从计算机时间原点以来的指定毫秒数。 |
1.3 常用方法
| 方法 | 作用 |
|---|---|
| public long getTime() | 把日期对象转换成对应的时间毫秒值 |
| public void setTime(long time) | 把参数time指定的毫秒值设置给时间对象 |
2.SimpleDateFormat类
2.1 概述
java.text.SimpleDateFormat类是日期/时间格式化类,可以使用该类完成日期和字符串之间的转换。
- 格式化:按照指定的格式,把Date对象转换为String对象
- 解析:按照指定的格式,把String对象转换为Date对象
2.2 构造方法
| 方法 | 作用 |
|---|---|
| public SimpleDateFormat(String pattern) | 用给定的模式和默认语言环境的日期格式符号构造SimpleDateFormat |
常用的规则:
| 标识字母(区分大小写) | 含义 |
|---|---|
| y | 年 |
| M | 月 |
| d | 日 |
| H | 时 |
| m | 分 |
| s | 秒 |
提示:区分大小写
2.3 常用方法
| 方法 | 作用 |
|---|---|
| public String format(Date date) | 将Date对象格式化为字符串 |
| public Date parse(String source) | 将字符串解析为Date对象 |
2.4 详细规则
| 字母 | 日期或时间元素 |
|---|---|
| G | Era 标志符 |
| y | 年 |
| M | 年中的月份 |
| w | 年中的周数 |
| W | 月份中的周数 |
| D | 年中的天数 |
| d | 月份中的天数 |
| F | 月份中的星期 |
| E | 星期中的天数 |
| a | Am/pm 标记 |
| H | 一天中的小时数(0-23) |
| k | 一天中的小时数(1-24) |
| K | am/pm 中的小时数(0-11) |
| h | am/pm 中的小时数(1-12) |
| m | 小时中的分钟数 |
| s | 分钟中的秒数 |
| S | 毫秒数 |
| z | 时区 |
| Z | 时区 |
3.Calendar类
3.1 概述
java.util.Calendar类表示一个“日历类”,可以进行日期运算。该类为抽象类,不能创建对象,但是可以使用其子类java.util.GregorianCalendar。
有两种方法可以获取GregorianCalendar对象:
- 直接创建
GregorianCalendar对象。 - 通过
Calendar类的静态方法getInstance()获取GregorianCalendar对象。
3.2 常用方法
| 方法 | 作用 |
|---|---|
| public static Calendar getInstance() | 获取一个GregorianCalendar对象 |
| public int get(int field) | 获取某个字段的值 |
| public void set(int field, int value) | 设置某个字段的值 |
| public void add(int field, int amount) | 将某个字段增加/减少指定的值 |
Calendar字段对应表:
| 字段 | 含义 |
|---|---|
| Calendar.YEAR | 年 |
| Calendar.MONTH | 月(0-11) |
| Calendar.DAY_OF_MONTH | 日 |
| Calendar.HOUR | 时 |
| Calendar.MINUTE | 分 |
| Calendar.SECOND | 秒 |
| Calendar.DAY_OF_WEEK | 星期(1-7,从星期天开始) |
4.JDK8时间相关类
| 类名 | 作用 |
|---|---|
| ZoneId | 时间 |
| Instant | 时间戳 |
| ZoneDateTime | 带时区的时间 |
| DateTimeFormatter | 用于时间的格式化和解析 |
| LocalDate | 年、月、日 |
| LocalTime | 时、分、秒 |
| LocalDateTime | 年、月、日、时、分、秒 |
| Duration | 时间间隔(秒,纳秒) |
| Period | 时间间隔(年,月,日) |
| ChronoUnit | 时间间隔(所有单位) |
4.1 ZoneId类
常用方法:
| 方法 | 作用 |
|---|---|
| static Set<String> getAvailableZoneIds() | 获取Java中支持的所有时区 |
| static ZoneId systemDefault() | 获取系统默认时区 |
| static ZoneId of(String zoneId) | 获取指定时区 |
4.2 Instant类
常用方法:
| 方法 | 作用 |
|---|---|
| static Instant now() | 获取当前时间的Instant对象(世界标准时) |
| static Instant ofXxx(long epochMilli) | 根据秒/毫秒/纳秒获取Instant对象 |
| ZonedDateTime atZone(ZoneId zone) | 指定时区(需要用对象调用) |
| boolean isBefore/isAfter(Instant otherInstant) | 判断时间的先后 |
| Instant minusXxx(long millisToSubstract) | 减少时间 |
| Instant plusXxx(long millisToSubstract) | 增加时间 |
注意:Instant类对象内容不能改变,每次运算都会生成新的对象。
4.3 ZoneDateTime类
常用方法:
| 方法 | 作用 |
|---|---|
| static ZoneDateTime now() | 获取当前时间的ZoneDateTime对象(带时区) |
| static ZoneDateTime ofXxx() | 获取指定时间的ZoneDateTime对象(带时区) |
| ZoneDateTime withXxx(时间) | 修改时间 |
| ZoneDateTime minusXxx(时间) | 减少时间 |
| ZoneDateTime plusXxx(时间) | 增加时间 |
注意:ZoneDateTime类对象内容不能改变,每次运算都会生成新的对象。
4.4 DateTimeFormatter类
常用方法:
| 方法 | 作用 |
|---|---|
| static DateTimeFormatter ofPattern(String pattern) | 按照格式获取DateTimeFormatter对象 |
| String format(TemporalAccessor temporal) | 按照指定格式格式化时间对象 |
4.5 LocalDate类(年、月、日)
常用方法:
| 方法 | 作用 |
|---|---|
| public static LocalDate now() | 获取当前时间的日历对象 |
| public static LocalDate of(时间参数) | 获取指定时间的日历对象 |
| public int getXxx() | 获取日历对象中某个属性值 |
| public boolean isBefore/After(ChronoLocalDate other) | 判断日历对象的时间先后 |
| public LocalDate withXxx(int xxx) | 修改日历对象的某个属性值 |
| public LocalDate minusXxx(long xxx) | 减少时间 |
| public LocalDate plusXxx(long xxx) | 增加时间 |
4.6 LocalTime类(时、分、秒)
常用方法:
| 方法 | 作用 |
|---|---|
| public static LocalTime now() | 获取当前时间的日历对象 |
| public static LocalTime of(时间参数) | 获取指定时间的日历对象 |
| public int getXxx() | 获取日历对象中某个属性值 |
| public boolean isBefore/After(LocalTime other) | 判断日历对象的时间先后 |
| public LocalTime withXxx(int xxx) | 修改日历对象的某个属性值 |
| public LocalTime minusXxx(long xxx) | 减少时间 |
| public LocalTime plusXxx(long xxx) | 增加时间 |
4.7 LocalDateTime类(年、月、日、时、分、秒)
常用方法:
| 方法 | 作用 |
|---|---|
| public static LocalDateTime now() | 获取当前时间的日历对象 |
| public int getXxx() | 获取日历对象中某个属性值 |
| public LocalDate toLocalDate() | 转换为LocalDate对象 |
| public LocalTime toLocalTime() | 转换为LocalTime对象 |
4.8 Duration类(秒、纳秒)
常用方法:
| 方法 | 作用 |
|---|---|
| public static Duration between(Temporal startInclusive, Temporal endExclusive) | 获取两个时间的间隔 |
| public long toXxx() | 获取两个时间相差的对应单位的数值 |
4.9 Period类(年、月、日)
常用方法:
| 方法 | 作用 |
|---|---|
| public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) | 获取两个时间的间隔 |
| public long toXxx() | 获取两个时间相差的对应单位的数值 |
4.10 ChronoUnit类(所有单位)
常用方法:
| 方法 | 作用 |
|---|---|
| public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) | 获取两个时间的间隔 |
5.包装类
5.1 概述
Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:
| 基本类型 | 对应的包装类(位于java.lang包中) |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
5.2 Integer类
概述
包装一个对象中的原始类型int的值。
方法
| 方法 | 作用 |
|---|---|
| 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类型的整数 |
5.3 装箱与拆箱
- 装箱:从基本数据类型转换为对应的包装类对象。
- 拆箱:从包装类对象转换为对应的基本数据类型。
提示:从JDK5开始,基本数据类型与包装类的装箱、拆箱可以自动完成。
5.4 基本数据类型与字符串之间的转换
5.4.1 基本数据类型转换为字符串
转换方法:
- 方法一:直接在数字后加一个空字符串。
- 方法二:通过String类中的静态方法
valueOf()。
5.4.2 字符串转换为基本数据类型
除了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基本类型 |
5.5 底层原理
建议:获取Integer对象的时候不要自己new,而是采取直接赋值或者静态方法valueOf的方式
因为在实际开发中,-128~127之间的数据,用的比较多。如果每次使用都是new对象,那么太浪费内存了。
所以,提前把这个范围之内的每一个数据都创建好对象,如果要用到了不会创建新的,而是返回已经创建好的对象。

浙公网安备 33010602011771号