java关于时间类

jdk7

Date 时间

|--------------------------------|------------|
| 方法 | 说明 |
| public Date() | 创建对象表示当前时间 |
| public Date(long date) | 创建对象表示指定时间 |
| public void setTime(long date) | 设置毫秒指 |
| public void getTime() | 获取毫秒值 |

SimpleDateFormat 日期格式化

|-----------------------------------------|-------------------|
| 方法 | 说明 |
| public SimpleDateFormat() | 默认格式 |
| public SimpleDateFormat(String pattern) | 指定格式 |
| public final String format(Date date) | 格式化(日期对象 -> 字符串) |
| public Date parse(String source) | 解析(字符串 -> 日期对象 ) |

Calender 日历

|---------------------------------------|------------------|
| 方法 | 说明 |
| public static Calendar getInstance() | 获取当前时间对象 |
| public final Date getTime() | 获取日期对象 |
| public final setTime(Date date) | 给日历设置日期对象 |
| public long getTimeInMillis() | 拿到时间毫秒值 |
| public int get(int field) | 取日历中的某个字段信息 |
| public void set(int field,int value) | 修改日历的某个字段信息 |
| public void add(int field,int amount) | 为某个字段增加 / 减少指定的值 |

jdk8

Date 时间

ZoneId 时区

|--------------------------------------------|------------------|
| 方法 | 说明 |
| static Set<String> getAvailableZoneIds() | 获取 Java 中支持的所有时区 |
| static ZoneId systemDefault() | 获取系统默认时区 |
| static ZoneId of(String zoneId) | 获取一个指定时区 |

Instant 时间戳

|-------------------------------------------|------------------------------|
| 方法 | 说明 |
| static Instant now()`` | 获取当前时间的 Instant 对象(标准时间) |
| static Instant ofXxx(long epochMilli) | 根据(秒 / 毫秒 / 纳秒)获取 Instant 对象 |
| ZonedDateTime atZone(ZoneId zone) | 指定时区 |
| boolean isXxx(Instant otherInstant) | 判断系列的方法 |
| Instant minusXxx(long millisToSubtract) | 减少时间系列的方法 |
| Instant plusXxx(long millisToSubtract) | 增加时间系列的方法 |

ZoneDateTime 带时区的时间

|-------------------------------------|--------------------------|
| 方法 | 说明 |
| static ZonedDateTimenow()`` | 获取当前时间的 ZonedDateTime 对象 |
| static ZonedDateTime ofXxx(...) | 获取指定时间的 ZonedDateTime 对象 |
| ZonedDateTime withXxx(时间) | 修改时间系列的方法 |
| ZonedDateTime minusXxx(时间) | 减少时间系列的方法 |
| ZonedDateTime plusXxx(时间) | 增加时间系列的方法 |

SimpleDateFormat 日期格式化

|----------------------------------------|-----------|
| 方法 | 说明 |
| static DateTimeFormatter ofPattern(格式) | 获取格式对象 |
| String format(时间对象) | 按照指定方式格式化 |

Calendat 日历

LocalDate、LocalTime、LocalDateTime

|--------------------------------|-------------------------------|
| 方法 | 说明 |
| static XXX now() | 获取当前时间的对象。 |
| static XXX of(...) | 获取指定时间的对象 |
| get开头的方法 | 获取日历中的年、月、日、时、分、秒等信息 |
| isBefore, isAfter | 比较两个 LocalDate |
| with开头的 | 修改时间系列的方法 |
| minus开头的 | 减少时间系列的方法 |
| plus开头的 | 增加时间系列的方法 |
| public LocalDate toLocalDate() | localTimeDate转换成一个LocalDate对象 |
| public LocalTime toLocalTime() | localTimeDate转换成一个LocalTime对象 |

时间工具类

|------------|------------------------------|
| 方法 | 说明 |
| Duration | 用于计算两个时间间隔(秒,纳秒) |
| Period | 用于计算两个日期间隔(年、月、日) |
| ChronoUnit | 用于计算两个日期间隔(年月日时分秒毫秒微秒纳秒世纪纪元) |

相关推荐
点心的游戏开发世界19 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
phltxy19 小时前
C语言操作符详解
java·c语言·算法
步行cgn20 小时前
@Configuration 详解:Spring 配置类的核心注解
java·后端·spring
sunshine22 girl20 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
事圆则缓21 小时前
Kotlin 协程完全指南
开发语言·kotlin
我爱写代码i21 小时前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
Java后端的Ai之路21 小时前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
嘿嘿-661 天前
Windows 一键使用 GPT-6 Astra:Codex CLI 配置教程
java·人工智能·windows·gpt·chatgpt·web
统计学小王子1 天前
数学建模国赛倒计时4天——《统计模型精讲(混合效应模型R语言实现)》
开发语言·数学建模·r语言
知无不研1 天前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while