Java日期相关_DateTimeFormatter

package com.pyk.teat;

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.LocalDateTime;

public class Test05 {
	public static void main(String[] args) {
		//格式化类:DateTimeFormatter
		
		//方式一:预定义的标准格式:如ISO_LOCAL_TIME;ISO_LOCAL_DATE;ISO_LOCAL_DATE_TIME
		DateTimeFormatter df1=DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		//df1就可以帮我们完成LocalDateTime与String之间的互相转换
		//LocalDateTime-->String
		//创建LocalDateTime实例
		LocalDateTime now=LocalDateTime.now();
		String str=df1.format(now);
		System.out.println(str);
		
		//String-->LocalDateTime
		TemporalAccessor parse=df1.parse("2022-05-01T18:34:46.5974595");
		
		
		//方式二:本地化相关的格式。如ofLocalDateTime()
		//参数:FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT
		//FormatStyle.LONG:2022年5月1日 下午06时52分12秒
		//FormatStyle.MEDIUM:2022-5-1 18:52:12
		//FormatStyle.SHORT:22-5-1 下午6:53
		DateTimeFormatter df2=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
		//LocalDateTime-->String
		//创建LocalDateTime实例
		LocalDateTime now1=LocalDateTime.now();
		String str2=df2.format(now1);
		System.out.println(str2);
		
		//String-->LocalDateTime
		TemporalAccessor parse1=df2.parse("22-5-1 下午6:55");
		System.out.println(parse1);
		
		//方式三:自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss")重点
		DateTimeFormatter df3=DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
		//LocalDateTime-->String
		//创建LocalDateTime实例
		LocalDateTime now2=LocalDateTime.now();
		String format=df3.format(now2);
		System.out.println(format);
		
		//String-->LocalDateTime
		TemporalAccessor parse2=df3.parse("2022-05-01 06:22:01");
		System.out.println(parse2);
	}
}

<think>嗯,用户问的是如何使用JavaDateTimeFormatter进行日期加减操作。首先,我需要确认DateTimeFormatter的功能。根据引用[1]和[2],DateTimeFormatter主要用于格式化和解析日期时间对象,比如LocalDateTime和ZonedDateTime。不过,日期加减操作实际上是由日期时间类本身的方法处理的,比如LocalDateTime的plus和minus方法,而不是由DateTimeFormatter直接执行的。 用户可能混淆了格式化和日期操作的概念。所以,我应该先解释清楚DateTimeFormatter的作用,然后转向如何正确进行日期加减。需要提供示例代码,展示如何先解析字符串为日期对象,进行加减操作,然后再格式化为字符串。 另外,要注意用户可能需要的实际应用场景,比如调整当前日期或处理特定格式的日期字符串。要确保示例覆盖这些情况,并指出常见的异常处理,比如DateTimeParseException。同时,要区分不同日期时间类的使用,如LocalDate、LocalDateTime和ZonedDateTime,因为它们的加减方法类似,但处理时区时需要特别注意。 最后,生成的相关问题要围绕用户可能进一步想了解的内容,比如时区处理、自定义格式或异常处理,这样有助于他们深入学习。</think>在Java中,`DateTimeFormatter` 仅负责日期时间的**格式化和解析**,而日期加减操作需要通过日期时间类(如`LocalDateTime`、`ZonedDateTime`)的 `plus()` 和 `minus()` 方法实现[^1][^2]。以下是具体操作示例: --- ### 一、日期加减操作步骤 1. **解析字符串为日期对象**:用`DateTimeFormatter`将字符串转为日期对象 2. **执行加减操作**:使用日期时间类的方法进行时间偏移 3. **格式化输出结果**:将修改后的日期对象转为字符串 ```java import java.time.*; import java.time.format.*; public class DateTimeExample { public static void main(String[] args) { // 步骤1:创建格式化器(定义格式) DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 步骤2:解析字符串为LocalDateTime LocalDateTime dateTime = LocalDateTime.parse("2023-10-01 15:30:00", formatter); // 步骤3:执行加减操作 LocalDateTime future = dateTime.plusDays(7).plusHours(3); // 加7天3小时 LocalDateTime past = dateTime.minusMonths(2).minusMinutes(15); // 减2月15分钟 // 步骤4:格式化输出 System.out.println("原始时间:" + formatter.format(dateTime)); // 2023-10-01 15:30:00 System.out.println("加法结果:" + formatter.format(future)); // 2023-10-08 18:30:00 System.out.println("减法结果:" + formatter.format(past)); // 2023-08-01 15:15:00 } } ``` --- ### 二、不同日期时间类的加减方法 | 日期时间类 | 加减方法示例 | 说明 | |--------------------|---------------------------------------|-------------------------------| | `LocalDate` | `plusDays(3)` / `minusWeeks(1)` | 仅处理日期(无时间) | | `LocalDateTime` | `plusHours(5)` / `minusMinutes(30)` | 处理日期+时间(无时区) | | `ZonedDateTime` | `plus(Duration.ofHours(2))` | 处理带时区的日期时间 | --- ### 三、特殊场景处理 1. **当前时间加减**: ```java ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime modified = now.plusYears(1).minusMonths(6); ``` 2. **使用时间单位枚举**: ```java LocalDateTime dt = LocalDateTime.now(); dt = dt.plus(2, ChronoUnit.HOURS); // 明确指定时间单位 ``` 3. **跨时区调整**: ```java ZonedDateTime nyTime = ZonedDateTime.now(ZoneId.of("America/New_York")); ZonedDateTime tokyoTime = nyTime.plusHours(14).withZoneSameInstant(ZoneId.of("Asia/Tokyo")); ``` --- ### 四、常见错误解决 若出现 `DateTimeParseException`,需检查: 1. 字符串格式是否与`DateTimeFormatter`定义的格式完全匹配 2. 是否包含非法值(如2月30日) 3. 是否遗漏时区信息(对`ZonedDateTime`解析时) ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值