当前位置: 首页 > news >正文

LocalDateTime获取时间的年、月、日、时、分、秒、纳秒

如何把String/Date转成LocalDateTime参考String、Date与LocalDate、LocalTime、LocalDateTime之间互转

String、Date、LocalDateTime、Calendar与时间戳之间互相转化参考String、Date、LocalDateTime、Calendar与时间戳之间互相转化

方法介绍

getYear() 获取日期的年
getMonth().name() 获取日期的月的英文
getMonth().getValue() 获取日期的月的英文的数字
getDayOfYear() 获取日期是年中的第几天
getDayOfMonth() 获取日期是月中的第几天
getDayOfWeek().name() 获取周几的英文
getDayOfWeek().getValue() 获取周几的数字
getHour() 获取日期的小时
getMinute() 获取日期的分钟
getSecond() 获取日期的秒
getNano() 获取日期中纳秒

具体使用

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;/*** 测试LocalDate* @author leishen*/
public class LocalDateTest {/*** LOCAL_DATE_TIME的时间格式*/private static final DateTimeFormatter LOCAL_DATE_TIME=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss ms ns");public static void main(String[] args) {//系统默认时区ZoneId zoneId = ZoneId.systemDefault();//Date-->ZonedDateTimeZonedDateTime zonedDateTime = new Date().toInstant().atZone(zoneId);LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();System.out.println("localDateTime.format(LOCAL_DATE_TIME) = " + localDateTime.format(LOCAL_DATE_TIME));//getYear() 获取日期的年System.out.println("localDateTime.getYear() = " + localDateTime.getYear());//getMonth().name() 获取日期的月的英文System.out.println("localDateTime.getMonth().name() = " + localDateTime.getMonth().name());//getMonth().getValue() 获取日期的月的英文的数字System.out.println("localDateTime.getMonth().getValue() = " + localDateTime.getMonth().getValue());//getDayOfYear() 获取日期是年中的第几天System.out.println("localDateTime.getDayOfYear() = " + localDateTime.getDayOfYear());//getDayOfMonth() 获取日期是月中的第几天System.out.println("localDateTime.getDayOfMonth() = " + localDateTime.getDayOfMonth());//getDayOfWeek().name() 获取周几的英文System.out.println("localDateTime.getDayOfWeek().name = " + localDateTime.getDayOfWeek().name());//getDayOfWeek().getValue() 获取周几的数字System.out.println("localDateTime.getDayOfWeek().getValue() = " + localDateTime.getDayOfWeek().getValue());//getHour() 获取日期的小时System.out.println("localDateTime.getHour() = " + localDateTime.getHour());//getMinute() 获取日期的分钟System.out.println("localDateTime.getMinute() = " + localDateTime.getMinute());//getSecond() 获取日期的秒System.out.println("localDateTime.getSecond() = " + localDateTime.getSecond());//getNano() 获取日期中纳秒System.out.println("localDateTime.getNano() = " + localDateTime.getNano());}
}
http://www.lryc.cn/news/2051.html

相关文章:

  • MoveIT Rviz和Gazebo联合仿真
  • ESP32S2(12K)-DS18B20数码管显示温度
  • linux栈溢出定位
  • CSS基础:选择器和声明样式
  • VS中安装gismo库
  • 元学习方法解决CDFSL以及两篇SOTA论文讲解
  • 大数据之------------数据中台
  • Python 中 字符串是什么?
  • OJ刷题Day1 · 一维数组的动态和 · 将数字变成 0 的操作次数 · 最富有的客户资产总量 · Fizz Buzz · 链表的中间结点 · 赎金信
  • 【数据结构】栈——必做题
  • LearnOpenGL 笔记 - 入门 04 你好,三角形
  • keepalived+mysql高可用
  • JAVA工具篇--1 Idea中 Gradle的使用
  • 弄懂自定义 Hooks 不难,改变开发认知有点不习惯
  • Java面向对象基础
  • 基于python下selenium库实现交互式图片保存操作(批量保存浏览器中的图片)
  • 一:Datart的下载、本地运行
  • Docker-compose
  • 经典文献阅读之--PLC-LiSLAM(面,线圆柱SLAM)
  • 计算组合数Cnk即从n个不同数中选出k个不同数共有多少种方法math.comb(n,k)
  • 工厂设计模式
  • IO多路转接 —— poll和epoll
  • 计算机网络整理-问答
  • JS 实现抛物线动画案例
  • CSGO搬砖项目,23年最适合小白的项目!
  • 谈谈会话管理
  • Linux查看JVM FULL GC频率
  • java世界String的那些事
  • 【图像配准】多图配准/不同特征提取算法/匹配器比较测试
  • 2023金三银四季跳槽季,啃完这软件测试面试题,跳槽不就稳稳的了