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

LocalDateTime设置时间的年、月、日、时、分、秒、纳秒

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

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

方法介绍

withYear(int year) 指定日期的年
withMonth(int month) 指定日期的月 范围:1-12
withDayOfMonth(int dayOfMonth) 指定日期是月中的第几天 范围:1 - 28/31
withDayOfYear(int dayOfYear) 指定日期是年中的第几天 范围:1 - 365/366
withHour(int hour) 指定日期的小时 范围:0 - 23
withMinute(int minute) 指定日期的分钟 范围:0 - 59
withSecond(int second) 指定日期的秒 范围:0 - 59
withNano(int nanoOfSecond) 指定日期中纳秒

具体使用

import java.text.SimpleDateFormat;
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");/*** DATE_TIME的时间格式*/private static final SimpleDateFormat DATE_TIME=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ms");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));//withYear(int year) 指定日期的年localDateTime = localDateTime.withYear(2022);System.out.println("localDateTime.withYear(2022) = " +localDateTime.format(LOCAL_DATE_TIME));//withMonth(int month) 指定日期的月 范围:1-12localDateTime = localDateTime.withMonth(1);System.out.println("localDateTime.withMonth(1) = " + localDateTime.format(LOCAL_DATE_TIME));//withDayOfMonth(int dayOfMonth) 指定日期是月中的第几天 范围:1 - 28/31localDateTime = localDateTime.withDayOfMonth(1);System.out.println("localDateTime.withDayOfMonth(1) = " + localDateTime.format(LOCAL_DATE_TIME));//withDayOfYear(int dayOfYear) 指定日期是年中的第几天 范围:1 - 365/366localDateTime = localDateTime.withDayOfYear(60);System.out.println("localDateTime.withDayOfYear(60) = " + localDateTime.format(LOCAL_DATE_TIME));//withHour(int hour) 指定日期的小时 范围:0 - 23localDateTime = localDateTime.withHour(23);System.out.println("localDateTime.withHour(23) = " + localDateTime.format(LOCAL_DATE_TIME));//withMinute(int minute) 指定日期的分钟 范围:0 - 59localDateTime = localDateTime.withMinute(59);System.out.println("localDateTime.withMinute(59) = " + localDateTime.format(LOCAL_DATE_TIME));//withSecond(int second) 指定日期的秒 范围:0 - 59localDateTime =localDateTime.withSecond(59);System.out.println("localDateTime.withSecond(59) = " + localDateTime.format(LOCAL_DATE_TIME));//withNano(int nanoOfSecond) 指定日期中纳秒localDateTime = localDateTime.withNano(0);System.out.println("localDateTime.withNano(0) = " + localDateTime.format(LOCAL_DATE_TIME));Date date = Date.from(localDateTime.atZone(zoneId).toInstant());System.out.println("DATE_TIME.format(date) = " + DATE_TIME.format(date));}
}
http://www.lryc.cn/news/1718.html

相关文章:

  • C语言rand和srand用法详解
  • 安装作业
  • 电压放大器在非共线混频方法检测混凝土中的应用
  • 二十四、Gtk4-GtkExpression
  • Oracle Dataguard(主库为 Oracle rac 集群)配置教程(02)—— Oracle RAC 主库的相关操作
  • midjournery AI绘画使用指南
  • iptables防火墙SNAT和DNAT
  • python | 第二章考试题和练习题
  • Mysql 存储引擎设计:xa协议相关接口功能及实现
  • 字符串常量池
  • 让技术更有温度,腾讯Light 点亮公益之光
  • 电子采购一体化解决方案
  • SAP COPA 获利能力分析深度解析
  • Java学习记录day6
  • ubuntu 使用 adb 工具卸载鸿蒙系统预装软件
  • Jmeter in Linux - 在Linux系统使用Jmeter的坑
  • 什么是特权访问管理(PAM)
  • LeetCode题目笔记——1.两数之和
  • CSDN版的详细MarkDown的使用教程
  • Nextcloud通过不被信任的域名访问解决方法 Nextcloud 您正在访问来自不信任域名的服务器
  • Set集合的特点,HashSet去重的几个重要问题
  • 云计算|OpenStack|社区版OpenStack安装部署文档(十一--- 如何获取镜像---Rocky版)
  • UmiJS学习
  • Leetcode:322. 零钱兑换(C++)
  • C经典小游戏之扫雷
  • 第十节 使用设备树插件实现RGB 灯驱动
  • 【LeetCode】公交路线 [H](宽度优先遍历)
  • 报表生成器 FastReport .Net 用户指南 2023(十):Band的属性
  • DAMA数据管理知识体系指南之文档和内容管理
  • C++入门:数据结构