package com.xiaoshan;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DecimalStyle;
import java.time.format.FormatStyle;
import java.time.format.ResolverStyle;
import java.util.Locale;
public class DateTimeFormatterExample {
public static void main(String[] args) {
// 使用指定的模式字符串创建对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.now();
String formattedDateTime = dateTime.format(formatter);
System.out.println("格式化后的日期时间: " + formattedDateTime);
// 创建一个本地化日期时间格式的对象
DateTimeFormatter localizedFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
String localizedFormattedDateTime = dateTime.format(localizedFormatter);
System.out.println("本地化格式化后的日期时间: " + localizedFormattedDateTime);
// 创建一个本地化日期时间格式的对象,并指定日期部分和时间部分的显示样式
localizedFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
String localizedCustomFormattedDateTime = dateTime.format(localizedFormatter);
System.out.println("指定日期时间样式的本地化格式化后的日期时间: " + localizedCustomFormattedDateTime);
// 预定义的日期、时间和日期时间格式
DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_LOCAL_TIME;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String formattedDate = dateTime.format(dateFormatter);
String formattedTime = dateTime.format(timeFormatter);
String formattedDateTime2 = dateTime.format(dateTimeFormatter);
System.out.println("预定义的日期格式化结果: " + formattedDate);
System.out.println("预定义的时间格式化结果: " + formattedTime);
System.out.println("预定义的日期时间格式化结果: " + formattedDateTime2);
// 返回一个新的对象,其中的地区设置为指定的地区
DateTimeFormatter withLocaleFormatter = dateTimeFormatter.withLocale(Locale.CHINA);
String formattedWithLocaleDateTime = dateTime.format(withLocaleFormatter);
System.out.println("指定地区的格式化后的日期时间: " + formattedWithLocaleDateTime);
// 返回一个新的对象,其中的格式设置为指定的十进制样式
DateTimeFormatter withDecimalStyleFormatter = withLocaleFormatter.withDecimalStyle(DateTimeFormatter.ISO_TIME.getDecimalStyle());
String formattedWithDecimalStyleDateTime = dateTime.format(withDecimalStyleFormatter);
System.out.println("指定十进制样式的格式化后的日期时间: " + formattedWithDecimalStyleDateTime);
// 返回一个新的对象,其中的解析方式设置为指定的解析方式
DateTimeFormatter withResolverStyleFormatter = withDecimalStyleFormatter.withResolverStyle(DateTimeFormatter.ISO_DATE.getResolverStyle());
String date = "2022-06-15T10:30:00.484";
LocalDateTime parsedDateTime = LocalDateTime.parse(date, withResolverStyleFormatter);
System.out.println("严格解析后的日期时间: " + parsedDateTime);
// 将字符串解析为TemporalAccessor对象(例如LocalDate、LocalTime或LocalDateTime等)
String dateString = "2022-06-15";
LocalDate parsedDate = LocalDate.parse(dateString, dateFormatter);
System.out.println("解析后的日期: " + parsedDate);
// 将指定的TemporalAccessor对象格式化为字符串
LocalDateTime localDateTime = LocalDateTime.of(2022, 6, 15, 10, 30);
String formattedCustomDateTime = localDateTime.format(formatter);
System.out.println("自定义日期时间格式化结果: " + formattedCustomDateTime);
// 返回该DateTimeFormatter对象使用的十进制样式
DecimalStyle decimalStyle = withResolverStyleFormatter.getDecimalStyle();
System.out.println("该DateTimeFormatter对象使用的十进制样式: " + decimalStyle);
// 返回该DateTimeFormatter对象的解析方式
ResolverStyle resolverStyle = withResolverStyleFormatter.getResolverStyle();
System.out.println("该DateTimeFormatter对象的解析方式: " + resolverStyle);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本源码资源提供了Java中的日期时间处理类相关内容,包括日期、时间和时区的操作方法和示例。它涵盖了Java编程中常用的日期时间处理需求以及如何使用日期时间类来解决这些问题。 适用人群 本源码资源适用于具备一定Java编程基础的开发人员和学习者。特别适用于需要在Java项目中处理日期时间相关逻辑的人群。 使用场景 1、日期时间计算:本源码资源提供了丰富的示例代码,帮助用户学习如何在Java中进行日期时间的计算,包括日期加减、格式化、解析等操作。 2、时区处理:了解如何使用Java日期时间类处理多时区的情况,包括转换、调整时差、比较等操作。 3、计划任务与调度:学习如何使用日期时间类来实现计划任务和调度功能,例如定时执行任务、定期触发事件等。 4、日历应用开发:本源码资源将介绍如何使用日期时间类在日历应用中处理日程安排、重复事件、时区切换等功能。 本源码资源旨在帮助用户掌握以下几个方面: 日期时间基础知识:了解日期、时间和时区的概念,理解Java中的日期时间类的层次结构和主要方法。 日期时间操作:学习如何使用日期时间类进行常见的日期和时间操作,如加减日期、比较两个日期、格式化日期等
资源推荐
资源详情
资源评论
收起资源包目录
src.zip (7个子文件)
src
com
xiaoshan
CalendarExample.java 4KB
LocalDateTimeExample.java 5KB
SimpleDateFormatExample.java 3KB
DateExample.java 4KB
DateTimeExample.java 3KB
DateTimeFormatterExample.java 5KB
FormattingExample.java 2KB
共 7 条
- 1
资源评论
小山code
- 粉丝: 1w+
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240430_144340_com.ss.android.ugc.live.jpg
- 回到山沟沟.mp3
- 基于matlab实现自适应波束形成RLS及LMS算法仿真源程序1.rar
- 基于matlab实现自己编写的基于卡尔曼滤波的利用加速度传感器的计步器,测试数据是传感器放在腰部和手臂 .rar
- 基于matlab实现阵列信号处理,波束形成.rar
- 111111111111111111
- 基于matlab实现计步器编程;对当前的计步器装置的数值算法模拟 .rar
- Mdb学习查看PW;access;mdb;pw;password;patch
- 基于matlab实现关于语音信号声源定位DOA估计所用的一些传统算法.rar
- 基于ultralytics-yolov8, 将其检测/分类/分割/姿态等任务移植到rk3588上
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功