LocalDate主要的三个API类:

java.time.LocalDate;
java.time.LocalDateTime;
java.time.LocalTime;

LocatDate对象获取:

@Test
void contextLoads() {
// 获取方式
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
LocalDate localDate1 = LocalDate.of(2020, 5, 23);
System.out.println(localDate1);
// 2020-09-28
// 2020-05-23
int year = localDate.getYear(); // 取年
int monthValue = localDate.getMonthValue(); // 取月
Month month = localDate.getMonth(); // 取月的枚举对象
int monthValue1 = month.getValue(); // 具体值可以通过该枚举对象获取
// 除此之外Month还有一些获取其他信息的方法
int maxDaysLength = month.maxLength(); // 该月的最大天数
int minDaysLength = month.minLength(); // 该月的最小天数
int dayOfMonth = localDate.getDayOfMonth(); // 按当月取天数
int dayOfYear = localDate.getDayOfYear(); // 按本年取天数
DayOfWeek dayOfWeek = localDate.getDayOfWeek(); // 按本周取天数?
// 和月枚举对象一样,这里也是一个周枚举对象
int value = dayOfWeek.getValue();
System.out.println(dayOfWeek); // 打印为 MONDAY ....
}
private static void cryptTest() {
final String PASSWORD = "123456";
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encode = passwordEncoder.encode(PASSWORD);
System.out.println(encode);
boolean matches = passwordEncoder.matches(PASSWORD, encode);
System.out.println(matches);
}

LocalDateTime & LocalTime:

@Test
void contextLoads() {
LocalDateTime localDateTime1 = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 2, 2, 13, 24, 52);
System.out.println(localDateTime1); // 2020-09-28T16:01:48.248
System.out.println(localDateTime2); // 2020-02-02T13:24:52 LocalTime localTime1 = LocalTime.now();
LocalTime localTime2 = LocalTime.of(20, 2, 2);
System.out.println(localTime1); // 16:03:41.330
System.out.println(localTime2); // 20:02:02 int hour = localTime1.getHour();
int minute = localTime1.getMinute();
int second = localTime1.getSecond();
int nano = localTime1.getNano();
System.out.println("时 -> " + hour); // 时 -> 16
System.out.println("分 -> " + minute); // 分 -> 6
System.out.println("秒 -> " + second); // 秒 -> 26
System.out.println("Nano -> " + nano); // Nano -> 206000000
}

GET方法

SET方法

增加时间

减少时间

public class DateTest {
public static void main(String[] args) {
LocalDate now1 = LocalDate.now();
LocalTime now2 = LocalTime.now();
LocalDateTime now3 = LocalDateTime.now(); System.out.println("LocalDate.now() -> " + now1);
System.out.println("LocalTime.now() -> " + now2);
System.out.println("LocalDateTime.now() -> " + now3);
/*
LocalDate.now() -> 2020-04-19
LocalTime.now() -> 21:16:03.854
LocalDateTime.now() -> 2020-04-19T21:16:03.854
*/
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime.getDayOfWeek() -> " + localDateTime.getDayOfWeek() ); // 按周算天
System.out.println("localDateTime.getDayOfMonth() -> " + localDateTime.getDayOfMonth() ); // 按月算天
System.out.println("localDateTime.getDayOfYear() -> " + localDateTime.getDayOfYear() ); // 按年算天
/*
localDateTime.getDayOfWeek() -> SUNDAY
localDateTime.getDayOfMonth() -> 19
localDateTime.getDayOfYear() -> 110
*/
System.out.println( localDateTime.getSecond() );// 取秒
System.out.println( localDateTime.getMinute() );// 取分
System.out.println( localDateTime.getHour() );// 取时
System.out.println( localDateTime.getMonth() );// 取月 英文大写
System.out.println( localDateTime.getMonthValue() );// 取月 数值表示
System.out.println( localDateTime.getYear() );// 取年
// set × with √
}
}

instant 瞬时时间 ,精度达到纳秒级

public class DateTest {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println(instant);
// 2020-04-19T13:47:58.712Z 本初子午线的标准时间 // 我们是东八时区,添加8小时的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime); // 从实例获取毫秒数 时间戳
long epochMilli = instant.toEpochMilli();
System.out.println(epochMilli); // 通过时间戳注入产生instant实例
Instant epochMilli1 = Instant.ofEpochMilli(epochMilli);
System.out.println(epochMilli1);
}
}

DateTimeFormatter

public class DateTest {
public static void main(String[] args) { // 预定义的标准格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; // 事件对象
LocalDateTime now = LocalDateTime.now(); // 转换格式 日期 -> 字符串格式
String format = dateTimeFormatter.format(now); // 格式
System.out.println( now );
System.out.println( format ); // 日期 转 字符串格式
TemporalAccessor parse = dateTimeFormatter.parse("2020-03-19T22:09:46.345");
System.out.println(parse); DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
// ofLocalizedDateTime();
// ofLocalizedDate(); 按Date就支持FULL全格式
// ofPattern("自定义格式"); "yyyy-MM-dd hh:mm:ss" // FormatStyle.FULL 2020年4月19日 星期日
// FormatStyle.LONG 2020年4月19日 下午10时15分01秒
// FormatStyle.MEDIUM 2020-4-19 22:14:28
// FormatStyle.SHORT 20-4-19 下午10:16 String format1 = dateTimeFormatter1.format(now);
System.out.println(now);
System.out.println(format1); TemporalAccessor parse1 = dateTimeFormatter1.parse(format1);
System.out.println(parse1);
}
}

案例,制作日历:

@Test
void contextLoads() {
localDateCalendar();
} private static void localDateCalendar() {
LocalDate now = LocalDate.now();
int year = now.getYear();
int month = now.getMonth().getValue();
int dayOfMonth = now.getDayOfMonth(); // 设置这个月的第一天
now = now.minusDays(dayOfMonth - 1);
// 找到这一天为周几
int value = now.getDayOfWeek().getValue(); // 开始渲染控制台输出样式
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for (int i = 1; i < value; i++) System.out.print(" ");
while (now.getMonthValue() == month) {
System.out.printf("%3d", now.getDayOfMonth());
if (now.getDayOfMonth() == dayOfMonth) System.out.print("*");
else System.out.print(" ");
now = now.plusDays(1);
if (now.getDayOfWeek().getValue() == 1) System.out.println();
}
if (now.getDayOfWeek().getValue() != 1) System.out.println();
}

打印结果:

Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28* 29 30

其他API

ZoneId

ZoneDateTime

Clock

Duration

Period

TemporalAdjuster

TemporalAdjusters

【Java】【常用类】LocalDateTime 当前日期时间类 相关的更多相关文章

  1. Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用

    不可变类且线程安全 LocalDate .java.time.LocalTime 和LocaldateTime  新的Date和Time类 DateTimeFormatter ==https://ww ...

  2. 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北 ...

  3. Java基础——常用类之日期时间类

    如果有机会,请尝试Java8中全新的时间日期API!(参见Java8新特性随笔) 如果还是使用Java7及之前的版本,那么你可以尝试一些工具类(参考使用工具类相关的Hutool-DateUtil) 如 ...

  4. java 常用的验证方法帮助类

    import java.text.ParseException; import java.util.Collection; import java.util.Map; /** * 常用的验证方法帮助类 ...

  5. 【Java SE进阶】Day01 Object类、日期时间类、System类、StringBuilder类、包装类

    一.Object类 1.概述:Java语言的根类/超类,默认继承自Object类 2.常用方法 toString():返回对象的字符串表示--对象类型@内存地址值 可以对其重写@Override eq ...

  6. Object类、日期时间类、system类及StringBuilder字符串容器

    一.Object类常用API 1.1 概述 java.lang.Object类是Java语言中的根类,即所有类的父类.Object类中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类就是 ...

  7. java_Object类、日期时间类、System类、包装类

    Object类 java.lang.Object 类是所有类的父类.它描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类就是Object. 如果一个类没有特别指定父类, 那么默认则继承自O ...

  8. java常用加密和解密工具类EncryptUtil.java

    package cn.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; im ...

  9. 用CTime类得到当前日期 时间

    (1)定义一个CTime类的对象CTime time: (2)得到当前时间time = CTime::GetCurrentTime(); (3)Get Year(),GetMonth(),GetDay ...

  10. java 常用类库:操作系统System类,运行时环境Runtime

    System类: System 类代表Java程序的运行平台,程序不能创建System类的对象, System类提供了一些类变量和类方法,允许直接通过 System 类来调用这些类变量和类方法. Sy ...

随机推荐

  1. xv6 内存管理

    前文讲述了 xv6 的启动过程,本文接着讲述 xv6 内存管理的部分,直接来看. 公众号:Rand_cs 启动部分完善 前文只是介绍了启动的过程,但是各类函数之间的调用,地址的变换,内存布局的变化并没 ...

  2. docker-20.10.24搭建ferry工单系统

    安装文档 https://www.fdevops.com/docs/ferry-tutorial-document/introduction 必须要有docker环境,19.03以上的 本文档需用户自 ...

  3. 利用夜莺开源版对H3C无线设备监控

    编者荐语:真正搞监控的人肯定知道 SNMP 水有多深,有时我甚至腹黑猜测,这些厂商是故意的吧,,,指标不标准,格式各异,只能靠一款灵活的采集器了,本文是夜莺社区用户写的文章,转给大家参考. autho ...

  4. Java中PDF的转换(图片)与展示

    解决的问题 有些时候我们需要在项目中展示PDF,但是直接在浏览器中加入PDF展示的插件,存在兼容性问题,某些浏览器显示效果不理想,所以我们可以将PDF转为图片,然后已图片的方式展示,效果很好. 那么怎 ...

  5. javascript 生成器和迭代器

    前置知识 生成器函数会返回一种称为Generator的迭代器 迭代器是一个对象,定义一个序列,并在终止时返回一个返回值 Symbol.iterator为每一个对象定义了默认的迭代器,可以被for..o ...

  6. 『手撕Vue-CLI』自动安装依赖

    开篇 经过『手撕Vue-CLI』拷贝模板,实现了自动下载并复制指定模板到目标目录.然而,虽然项目已复制,但其依赖并未自动安装,可能需要用户手动操作,这并不够智能. 正如前文所述,我们已经了解了业务需求 ...

  7. 支付宝spi接口设计验签和返回结果加签注意点,支付宝使用JSONObject对象

    支付宝spi接口设计验签和返回结果加签注意点,支付宝使用JSONObject对象 SPI 三方服务接入指南https://opendocs.alipay.com/isv/spiforisv 服务端实现 ...

  8. Kotlin 数据类型详解:数字、字符、布尔值与类型转换指南

    Kotlin 数据类型 在 Kotlin 中,变量的类型由其值决定: 示例 val myNum = 5 // Int val myDoubleNum = 5.99 // Double val myLe ...

  9. EMQX配置ssl/tls双向认证+SpringBoot项目整合MQTT_真实业务实践

    一.使用docker搭建Emqx 1.拉取emqx镜像 docker pull emqx/emqx:5.7 2.运行 docker run -d --name emqx emqx/emqx:5.7 3 ...

  10. Linux Redis 服务设置开机自启动

    @ 目录 前言 一.准备工作 二.操作步骤 2.1 修改redis.conf文件 2.2 创建启动脚本 2.3 设置redis 脚本权限 2.4 设置开机启动 2.5 验证 总结 前言 请各大网友尊重 ...