DatetimeFormatter字符串转日期
在Java中,我们经常需要将字符串形式的日期时间转换为LocalDateTime
、LocalDate
、LocalTime
等日期时间对象,或者将日期时间对象转换为字符串。为了完成这些操作,我们可以使用DateTimeFormatter
类。
1. 导入必要的类
首先,确保你已经导入了必要的类:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
2. 定义日期时间字符串和格式
假设我们有一个日期时间字符串,例如:”2023-10-23T15:30:45″。
String dateTimeStr = "2023-10-23T15:30:45";
我们希望将这个字符串转换为LocalDateTime
对象。为此,我们需要定义一个与字符串格式相匹配的DateTimeFormatter
。
3. 创建DateTimeFormatter
使用DateTimeFormatter
的静态方法ofPattern
来定义日期时间的格式。在这个例子中,我们使用ISO 8601格式,它是”yyyy-MM-dd’T’HH:mm:ss”。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
4. 字符串转日期时间
使用LocalDateTime
的静态方法parse
和之前创建的DateTimeFormatter
来将字符串转换为LocalDateTime
对象。
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);
现在,dateTime
变量包含了从字符串转换而来的日期时间信息。
5. 日期时间转字符串
如果你有一个LocalDateTime
对象,并希望将其转换为字符串,你可以使用相同的DateTimeFormatter
。
LocalDateTime someDateTime = LocalDateTime.now(); // 假设这是你的日期时间对象
String strDateTime = someDateTime.format(formatter);
现在,strDateTime
变量包含了从LocalDateTime
对象转换而来的字符串。
完整示例:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; public class DateTimeFormatterExample {
public static void main(String[] args) {
// 字符串转日期时间
String dateTimeStr = "2023-10-23T15:30:45";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);
System.out.println("Parsed DateTime: " + dateTime); // 日期时间转字符串
LocalDateTime someDateTime = LocalDateTime.now();
String strDateTime = someDateTime.format(formatter);
System.out.println("Formatted DateTime: " + strDateTime);
}
}
输出:
Parsed DateTime: 2023-10-23T15:30:45
Formatted DateTime: 2024-02-18T09:06:17
这只是一个简单的DatetimeFormatter字符串转日期教程,可以帮助你了解如何使用DatetimeFormatter实现日期对象和日期格式字符串之间的相互转换,从而了解DatetimeFormatter对象的用法,希望对你有帮助,学习愉快哦!
DatetimeFormatter字符串转日期的更多相关文章
- js字符串转为日期格式
1. <script type="text/javascript"> //字符串转日期格式,strDate要转为日期格式的字符串 function getDate(st ...
- Swift根据日期字符串返回日期是星期几
最近在做的一个IOS项目中需要根据日期得出日期代表的是星期几,日期以字符串的形式获得,于是该方法可以简单描述如下: /* * 根据日期格式字符串返回日期代表星期几 * 参数:dateTime,字符串类 ...
- Freemarker 内置函数 数字、字符串、日期格式化用法介绍
在用FreeMarker过程中,感觉FreeMarker的字符串,日期,集合等处理能力还是很强大的,上网搜了一些资料,整理如下,以便能帮助大家更熟练的应用Freemarker完成项目开发. 一.Seq ...
- android JAVA字符串转日期或日期转字符串(转)
用法: SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ); 这一行最重要,它确立了转换的 ...
- java 和 mysql 获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)
获取周的第一天,最后一天 System.out.println(getStartEndDate("2016-05-01", 1)); 获取星期的第一天和最后一天 System.ou ...
- MySQL字符串转日期类型
MySQL字符串转日期类型 select str_to_date('2014-08-20 00:00:00', '%Y-%m-%d %H:%i:%s'); >2014-08-20 00:00:0 ...
- c#字符串转换为日期,支持任意字符串
文章关键字: c#字符串转换为日期 c#日期转换字符串 字符串转换日期 字符串转换为date 整数转换为字符串 浮点数转换为字符串 字符串转换为时间 将字符串转换为时间 字符转 ...
- JAVA字符串转日期或日期转字
文章中,用的API是SimpleDateFormat,它是属于java.text.SimpleDateFormat,所以请记得import进来! 用法: SimpleDateFormat sdf ...
- SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学、字符串、日期时间)函数[转]
SQL server 创建 修改表格 及表格基本增删改查 及 高级查询 及 (数学.字符串.日期时间)函数 --创建表格 create table aa ( UserName varchar(50 ...
- mysql str_to_date字符串转换为日期
mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期. 示例:分隔符一致,年月日要一致 select str_to_date('2008-4-2 15:3:28','% ...
随机推荐
- django 设置外键的时候,related_name的值大写还是小写,规则怎样
django 设置外键的时候,related_name的值大写还是小写,规则怎样 在Django中,related_name参数用于定义反向关系的名称,即通过外键字段反向查询关联模型的对象.relat ...
- [oeasy]python0024_unix时间戳_epoch_localtime_asctime_PosixTime_unix纪年法
输出时间回忆上次内容 通过搜索 我们学会 import 导入 time 了 完整写法为 asc_time = time.asctime( time.localtime( time.time())) 内 ...
- 玄机-第二章日志分析-mysql应急响应
目录 前言 简介 应急开始 准备工作 日志分析 步骤 1 步骤 2 步骤 3 步骤 4 总结 补充mysql中的/var/log/mysql/erro.log 记录上传文件信息的原因 前言 这里应急需 ...
- 人脸识别项目打包成exe的过程遇到的问题
我最近重新拾起了计算机视觉,借助Python的opencv还有face_recognition库写了个简单的图像识别demo,额外定制了一些内容,原本想打包成exe然后发给朋友,不过在这当中遇到了许多 ...
- mysql索引失效的情况七字口诀:“模型数空运最快”
mysql索引失效的情况 七字口诀:"模型数空运最快" 模:使用like进行模糊查询的时候,以百分号%开头的,索引就会失效. 型:代表数据类型,数据类型错误了,索引也会失效. 数: ...
- 基于 TrueLicense 项目证书生成
一.创建公钥私钥 1.首先要用 KeyTool 工具来生成私匙库:(-alias别名 -validity 3650 表示10年有效,这个时间不是License的过期时间) keytool -genke ...
- 【MySQL】LEFT JOIN 踩坑
一.问题发现: 主查询功能发现两条一样的记录,但是审批状态不一样,一个已通过,一个待审核 主表付款表: CREATE TABLE `pur_or_payment` ( `id` int(11) NOT ...
- 使用 Alba 对 AspnetCore项目进行测试
前言 在AspnetCore生态系统中,我们测试项目一般使用Microsoft.AspNetCore.TestHost的TestServer 到.NET6后提供的Microsoft.AspNetCor ...
- 论文写作:写回复意见时的套话 (reviews and comments)
Typos, Grammar, and Citation Errors: We acknowledge the need for a thorough proofreading to rectify ...
- Windows系统下硬盘S.M.A.R.T硬盘检测工具——硬盘检测工具(CrystalDiskInfo)
CrystalDiskInfo是由日本人hiyohiyo个人开发的小工具,能全面详细直观的检查硬盘的健康状态及各种参数. 绿色版软件. 官方下载地址: https://crystalmark.info ...