String类型与时间格式的转换 附带时间格式比较方法

String类型转换为 LocalDate LocalDateTime 格式的方法 

String date01 = "2018-01-01";
String dateTime01 = "2018-01-01 12:12:12";

LocalDate localDate = LocalDate.parse(date01, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDateTime localDateTime = LocalDateTime.parse(dateTime01, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(localDate);
System.out.println(localDateTime);

结果展示贴图

 LocalDate LocalDateTime 转String类型的方法

//提前声明 这里的 localDate = 2018-01-01 || localDateTime = 2018-01-01T12:12:12
String strLocalDate = String.valueOf(localDate);
String strLocalDateTime = String.valueOf(localDateTime);
System.out.println(strLocalDate);
System.out.println(strLocalDateTime);

结果贴图

 Date格式转换为String LocalDateTime格式

Date utilDate = new Date();
Instant instant = utilDate.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localData_UtilDateTime = LocalDateTime.ofInstant(instant,zoneId);
String localData_UtilDateTime_str = LocalDateTime.ofInstant(instant,zoneId).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(localData_UtilDateTime);
System.out.println(localData_UtilDateTime_str);

结果贴图

 

LocalDateTime格式转换为Date

// 声明LocalDateTime localData_UtilDateTime = 2022-08-10T16:00:35.619
ZoneId zoneId_LocalDateTime = ZoneId.systemDefault();
Instant instant_LocalDateTime = localData_UtilDateTime.atZone(zoneId_LocalDateTime).toInstant();
Date localDate_date = Date.from(instant_LocalDateTime);
System.out.println(localDate_date);

结果贴图

 LocalDateTime转换为LocalDate格式

LocalDateTime localDateTime = LocalDateTime.now();

LocalDate localDate = localDateTime.toLocalDate() 

LocalDate 与 LocalDateTime 两种时间格式比较大小

时间比较大小 举个栗子 2018年 比2020年

String date01 = "2018-01-01";
String dateTime01 = "2018-01-01 12:12:12";

LocalDate localDate = LocalDate.parse(date01, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDateTime localDateTime = LocalDateTime.parse(dateTime01, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//比较 localDate是否大于LocalDate.now()
boolean before = localDate.isBefore(LocalDate.now());
//比较 localDate是否小于LocalDate.now()
boolean after = localDate.isAfter(LocalDate.now());
//比较 localDate是否等于LocalDate.now()
boolean equal = localDate.isEqual(LocalDate.now());

System.out.println(before);
System.out.println(after);
System.out.println(equal);

结果贴图

 详细讲解

这三个方法适用于 LocalDate LocalDateTime

左.isBefore(右)

isBefore方法比较大小 比较左边是否大于右边 如果大于返回 true  反之false

左.isAfter(右)

isAfter方法比较大小 比较左边是否小于右边 如果小于返回 true 反之false

左.isEqual(右)

isEqual方法比较是否相同  比较左右两边是否相同 如果相同返回true 反之false

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将时间转换String类型,可以使用DateTimeFormatter类中的ofPattern方法来指定日期时间的格式,然后使用对应的parse方法将日期时间对象转换String类型。例如,如果要将LocalDate对象转换String类型,可以使用以下代码: ```java LocalDate localDate = LocalDate.now(); String strLocalDate = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); ``` 如果要将LocalDateTime对象转换String类型,可以使用以下代码: ```java LocalDateTime localDateTime = LocalDateTime.now(); String strLocalDateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); ``` 以上代码中,DateTimeFormatter.ofPattern方法中的参数"yyyy-MM-dd"指定了日期的格式,"yyyy-MM-dd HH:mm:ss"则指定了日期时间的格式。通过调用format方法将日期或日期时间对象转换为指定格式的字符串。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [时间转换工具类](https://download.csdn.net/download/qq_35834998/10162854)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [String类型时间格式转换 附带时间格式比较方法](https://blog.csdn.net/refreshed_God/article/details/126267535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值