首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
第一章:String 类
1.1 概述
1.2 字面量的定义方式
1.3 String 的特点
1.4 String 的内存示意图
1.5 构造 String 对象
1.6 字符串拼接问题
1.7 字符串对象的比较
1.8 空字符串的比较
1.9 字符串的常用方法
1.10 常见正则表达式
第二章:StringBuilder 类
2.1 概述
2.2 常用方法
第三章:系统相关类
3.1 System 类
3.2 Runtime 类
第四章:数学相关的类
4.1 Math 类
4.2 大数运算类
第五章:数组的相关操作
5.1 数组的算法升华
5.2 数组工具类
第六章:日期时间API
6.1.1 概述
6.1.2 本地日期时间
6.2.3 指定时区日期时间 ZonedDateTime
6.2.4 持续日期/时间 Period 和 Duration
6.2.5 日期时间格式化 DateTimeFormat
第七章:字符编码的发展
7.1 ASCII 码
7.2 OEM 字符集的诞生
7.3 多字节字符集(MBCS)和中文字符集
7.4 ANSI 标准、国家标准以及 ISO 标准
7.5 Unicode 的出现
当前位置:
首页>>
技术小册>>
Java语言基础9-常用API和常见算法
小册名称:Java语言基础9-常用API和常见算法
- LocalDate 表示本地日期,LocalTime 表示本地时间,而 LocalDateTIme 表示本地日期时间。 - Instant 表示瞬间(本地日期时间),精确到纳秒。 LocalDateTIme 和 Instant 的区别: - Instant 是带时区的(以UTC为准),用来替换以前的 Date 。 - LocalDateTIme 是不带时区(以本地时区为准)的,用来替换以前的 Calendar。 - 以 LocalDateTime 为例: - 获取对象: - ① 获取当前日期时间: ``` public static LocalDateTime now() {} public static LocalDateTime now(ZoneId zone) {} ``` ② 根据指定日期时间创建对象: ``` public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) {} ``` - 获取日期时间中的每个值: - 获取年份: ``` public int getYear() {} ``` 获取月份(1~12): ``` public int getMonthValue() {} ``` 获取月份的第几天(1~31): ``` public int getDayOfMonth() {} ``` 获取一年中的第几天(1~366): ``` public int getDayOfYear() {} ``` 获取星期: ``` public DayOfWeek getDayOfWeek() {} ``` 获取小时: ``` public int getHour() {} ``` 获取分钟: ``` public int getMinute() {} ``` 获取秒: ``` public int getMinute() {} ``` 获取纳秒: ``` public int getSecond() {} ``` 转换为 LocalDate : ``` public LocalDate toLocalDate() {} ``` 转换为 LocalTime : ``` public LocalTime toLocalTime() ``` 修改年: ``` public LocalDateTime withYear(int year) {} ``` 修改月: ``` public LocalDateTime withMonth(int month) {} ``` 修改日期(一个月中的第几天): ``` public LocalDateTime withDayOfMonth(int dayOfMonth) {} ``` 修改日期(一年中的第几天): ``` public LocalDateTime withDayOfYear(int dayOfYear) {} ``` 修改小时: ``` public LocalDateTime withHour(int hour) {} ``` 修改分钟: ``` public LocalDateTime withMinute(int minute) {} ``` 修改秒: ``` public LocalDateTime withSecond(int second) {} ``` 增加年或减少年: ``` public LocalDateTime plusYears(long years) {} ``` 增加或减少月: ``` public LocalDateTime plusMonths(long months) {} ``` 增加或减少日: ``` public LocalDateTime plusDays(long days) {} ``` 增加或减少小时: ``` public LocalDateTime plusHours(long hours) {} ``` 增加或减少分钟: ``` public LocalDateTime plusMinutes(long minutes) {} ``` 增加或减少秒: ``` public LocalDateTime plusSeconds(long seconds) {} ``` 增加或减少周: ``` public LocalDateTime plusWeeks(long weeks) {} ``` 减少或添加年: ``` public LocalDateTime minusYears(long years) {} ``` 减少或添加月: ``` public LocalDateTime minusMonths(long months) {} ``` 减少或添加日: ``` public LocalDateTime minusDays(long days) {} ``` 减少或添加小时: ``` public LocalDateTime minusHours(long hours) {} ``` 减少或添加分钟: ``` public LocalDateTime minusMinutes(long minutes) {} ``` 减少或添加秒: ``` public LocalDateTime minusSeconds(long seconds) {} ``` 减少或添加周: ``` public LocalDateTime minusWeeks(long weeks) {} ``` 是否在另一个时间之前: ``` public boolean isBefore(ChronoLocalDateTime<?> other) {} ``` 两个时间是否相等: ``` public boolean isEqual(ChronoLocalDateTime<?> other) {} ``` 是否在另一个时间之后: ``` public boolean isAfter(ChronoLocalDateTime<?> other) {} ``` 示例: ```bash package com.github.date.jdk8.demo1; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDate now = LocalDate.now(); System.out.println("now = " + now); // now = 2021-09-26 LocalTime now1 = LocalTime.now(); System.out.println("now1 = " + now1); // now1 = 20:02:32.659 LocalDateTime now2 = LocalDateTime.now(); System.out.println("now2 = " + now2); // now2 = 2021-09-26T20:02:32.660 } } ``` 示例: ``` package com.github.date.jdk8.demo9; import java.time.Instant; import java.time.LocalDateTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { Instant now = Instant.now(); System.out.println("now = " + now); // now = 2021-09-27T01:54:39.351Z LocalDateTime now1 = LocalDateTime.now(); System.out.println("now1 = " + now1); // now1 = 2021-09-27T09:54:39.401 } } ``` 示例: ``` package com.github.date.jdk8.demo3; import java.time.LocalDateTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDateTime of = LocalDateTime.of(2021, 11, 11, 11, 11, 11); System.out.println("of = " + of); // of = 2021-11-11T11:11:11 } } ``` 示例: ``` package com.github.date.jdk8.demo4; import java.time.DayOfWeek; import java.time.LocalDateTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); int year = now.getYear(); System.out.println("获取年份:" + year); int monthValue = now.getMonthValue(); System.out.println("获取月份:" + monthValue); int dayOfMonth = now.getDayOfMonth(); System.out.println("获取天数:" + dayOfMonth); int dayOfYear = now.getDayOfYear(); System.out.println("获取一年中的第" + dayOfYear + "天"); DayOfWeek dayOfWeek = now.getDayOfWeek(); System.out.println("获取星期" + dayOfWeek.getValue()); int hour = now.getHour(); System.out.println("获取小时:" + hour); int minute = now.getMinute(); System.out.println("获取分钟:" + minute); int second = now.getSecond(); System.out.println("获取秒:" + second); int nano = now.getNano(); System.out.println("获取纳秒:" + nano); } } ``` 示例: ``` package com.github.date.jdk8.demo5; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); // 转换为LocalDate LocalDate localDate = now.toLocalDate(); System.out.println("localDate = " + localDate); // 转换为LocalTime LocalTime localTime = now.toLocalTime(); System.out.println("localTime = " + localTime); } } ``` 示例: ``` package com.github.date.jdk8.demo9; import java.time.LocalDateTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("now = " + now); // now = 2021-09-27T08:41:04.197 LocalDateTime localDateTime = now.withYear(2022); System.out.println("修改年 = " + localDateTime); // 修改年 = 2022-09-27T08:41:04.197 LocalDateTime localDateTime1 = now.withMonth(10); System.out.println("修改月 = " + localDateTime1); // 修改月 = 2021-10-27T08:41:04.197 LocalDateTime localDateTime2 = now.withDayOfMonth(28); System.out.println("修改日期(一个月中的第几天) = " + localDateTime2); // 修改日期(一个月中的第几天) = 2021-09-28T08:41:04.197 LocalDateTime localDateTime3 = now.withDayOfYear(15); System.out.println("修改日期(一年中的第几天) = " + localDateTime3); // 修改日期(一年中的第几天) = 2021-01-15T08:41:04.197 LocalDateTime localDateTime4 = now.withHour(10); System.out.println("修改小时 = " + localDateTime4); // 修改小时 = 2021-09-27T10:41:04.197 LocalDateTime localDateTime5 = now.withMinute(15); System.out.println("修改分钟 = " + localDateTime5); // 修改分钟 = 2021-09-27T08:15:04.197 LocalDateTime localDateTime6 = now.withSecond(10); System.out.println("修改秒 = " + localDateTime6); // 修改秒 = 2021-09-27T08:41:10.197 } } ``` 示例: ``` package com.github.date.jdk8.demo7; import java.time.LocalDateTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("now = " + now); // now = 2021-09-27T09:30:21.527 LocalDateTime localDateTime = now.plusYears(1); System.out.println("localDateTime = " + localDateTime); // localDateTime = 2022-09-27T09:30:21.527 LocalDateTime localDateTime1 = now.plusYears(-1); System.out.println("localDateTime1 = " + localDateTime1); // localDateTime1 = 2020-09-27T09:30:21.527 LocalDateTime localDateTime2 = now.plusMonths(1); System.out.println("localDateTime2 = " + localDateTime2); // localDateTime2 = 2021-10-27T09:30:21.527 LocalDateTime localDateTime3 = now.plusDays(1); System.out.println("localDateTime3 = " + localDateTime3); // localDateTime3 = 2021-09-28T09:30:21.527 LocalDateTime localDateTime4 = now.plusHours(1); System.out.println("localDateTime4 = " + localDateTime4); // localDateTime4 = 2021-09-27T10:30:21.527 LocalDateTime localDateTime5 = now.plusMinutes(1); System.out.println("localDateTime5 = " + localDateTime5); // localDateTime5 = 2021-09-27T09:31:21.527 LocalDateTime localDateTime6 = now.plusSeconds(1); System.out.println("localDateTime6 = " + localDateTime6); // localDateTime6 = 2021-09-27T09:30:22.527 LocalDateTime localDateTime7 = now.plusWeeks(1); System.out.println("localDateTime7 = " + localDateTime7); // localDateTime7 = 2021-10-04T09:30:21.527 } } ``` 示例: ``` package com.github.date.jdk8.demo8; import java.time.LocalDateTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("now = " + now); // now = 2021-09-27T09:39:01.406 LocalDateTime localDateTime = now.minusYears(1); System.out.println("localDateTime = " + localDateTime); // localDateTime = 2020-09-27T09:39:01.406 LocalDateTime localDateTime1 = now.minusMonths(1); System.out.println("localDateTime1 = " + localDateTime1); // localDateTime1 = 2021-08-27T09:39:01.406 LocalDateTime localDateTime2 = now.minusDays(1); System.out.println("localDateTime2 = " + localDateTime2); // localDateTime2 = 2021-09-26T09:39:01.406 LocalDateTime localDateTime3 = now.minusHours(1); System.out.println("localDateTime3 = " + localDateTime3); // localDateTime3 = 2021-09-27T08:39:01.406 LocalDateTime localDateTime4 = now.minusMinutes(1); System.out.println("localDateTime4 = " + localDateTime4); // localDateTime4 = 2021-09-27T09:38:01.406 LocalDateTime localDateTime5 = now.minusSeconds(1); System.out.println("localDateTime5 = " + localDateTime5); // localDateTime5 = 2021-09-27T09:39:00.406 LocalDateTime localDateTime6 = now.minusWeeks(1); System.out.println("localDateTime6 = " + localDateTime6); // localDateTime6 = 2021-09-20T09:39:01.406 } } ``` 示例: ``` package com.github.date.jdk8.demo10; import java.time.LocalDateTime; /** * @author maxiaoke.com * @version 1.0 * */ public class Test { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); LocalDateTime localDateTime = LocalDateTime.now().plusHours(1); System.out.println(now.isBefore(localDateTime)); // true System.out.println(now.isAfter(localDateTime)); // false System.out.println(now.isEqual(localDateTime)); // false } } ```
上一篇:
6.1.1 概述
下一篇:
6.2.3 指定时区日期时间 ZonedDateTime
该分类下的相关小册推荐:
Java必知必会-Maven初级
Mybatis合辑1-Mybatis基础入门
Java语言基础13-类的加载和反射
Java语言基础14-枚举和注解
Java语言基础12-网络编程
Java必知必会-Maven高级
Java语言基础7-Java中的异常
Mybatis合辑3-Mybatis动态SQL
Java语言基础11-Java中的泛型
Java语言基础16-JDK8 新特性
手把手带你学习SpringBoot-零基础到实战
Java语言基础15-单元测试和日志技术