千家信息网

Java中常用时间的相关方法有哪些

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章主要介绍"Java中常用时间的相关方法有哪些",在日常操作中,相信很多人在Java中常用时间的相关方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Jav
千家信息网最后更新 2026年11月04日Java中常用时间的相关方法有哪些

这篇文章主要介绍"Java中常用时间的相关方法有哪些",在日常操作中,相信很多人在Java中常用时间的相关方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Java中常用时间的相关方法有哪些"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、获取当前时间的方式

public static void main(String[] args) {    //Date    Date now = new Date();    System.out.println(now);    //java8的时间    LocalDateTime localDateTime = LocalDateTime.now();    System.out.println(localDateTime);    Calendar calendar = Calendar.getInstance();    Date time = calendar.getTime();    System.out.println(time);    System.out.println("年" + calendar.get(Calendar.YEAR));    System.out.println("月" + (calendar.get(Calendar.MONTH) + 1));    //joda time    DateTime dateTime = DateTime.now();    System.out.println(dateTime);}

获取当前时间可以使用Date LocalDatetime Calendar Datetime

二、获取当月第n天

public static void main(String[] args) {    //建议使用Calendar  可以设置年月日时分秒    Calendar calendar = Calendar.getInstance();    ////当月16    calendar.set(Calendar.DAY_OF_MONTH, 16);    System.out.println(calendar.getTime());    //当月16    DateTime now = DateTime.now();    DateTime dateTime = now.withDayOfMonth(16);    System.out.println(dateTime);    //当月14    LocalDateTime localDateTime = LocalDateTime.now();    System.out.println(localDateTime.withDayOfMonth(14));    //1月11    System.out.println(localDateTime.withMonth(1).withDayOfMonth(11));}

三、格式化为字符串

```//使用SimpleDateFormatSimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(format.format(new Date()));//使用CalendarCalendar calendar = Calendar.getInstance();System.out.println(String.format("%s年%s月%s日%s时%s分%s秒", calendar.get(Calendar.YEAR),        calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),        calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));LocalDateTime now = LocalDateTime.now();String str = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println(str);```

四、加减时间(单位可以是秒,小时等)

public static void main(String[] args) {    Date now = new Date();    //加一小时    long time = now.getTime() + (60 * 60 * 1000);    System.out.println(new Date(time));    /*            cn.hutool        hutool-all        5.7.14         */    //引入Hutool 加一小时    System.out.println(DateUtil.offset(now, DateField.HOUR, 1));    //减一小时    System.out.println(DateUtil.offset(now, DateField.HOUR, -1));    LocalDateTime localDateTime = LocalDateTime.now();    System.out.println("加一小时" + localDateTime.plusHours(1));    System.out.println("减一小时" + localDateTime.minusHours(1));    DateTime dateTime = DateTime.now();    System.out.println(dateTime.plusHours(1));    System.out.println(dateTime.minusHours(1));}

LocalDateTime和DateTime都自带增加和减少时间的方法

五、通过出生日期获取年龄

public static void main(String[] args) {    //时间1990-12-05    DateTime birthDay = DateTime.now().withYear(1990).withMonthOfYear(10).withDayOfMonth(23);    System.out.println(birthDay);    //获取相差得年 会进行月份和日期比较 如    Years years = Years.yearsBetween(birthDay, new DateTime());    System.out.println(years);    System.out.println(years.getYears());}

还可以使用年份相减,再比较月,日的方法得到生日

六、判断两个时间段是否覆盖

public static void main(String[] args) {    DateTime now = DateTime.now();    DateTime start1 = now;    DateTime end1 = now.plusMinutes(1);    DateTime start2 = now.plusSeconds(50);    DateTime end2 = now.plusMinutes(2);    Interval interval1 = new Interval(start1, end1);    Interval interval2 = new Interval(start2, end2);    System.out.println(interval1.overlaps(interval2));    System.out.println(start1.getMillis() < end2.getMillis() && start2.getMillis() < end1.getMillis());}

七、求两个时间间隔

public static void main(String[] args) {    DateTime now = DateTime.now();    //开始时间    Date startTime = now.toDate();    //结束时间    Date endTime = now.plusHours(1).toDate();    //1小时    System.out.println("开始时间与结束时间的时间间隔:" + DateUtil.between(startTime, endTime, DateUnit.SECOND));    long time = (endTime.getTime() - startTime.getTime()) / 1000;    System.out.println(time);}

八、UTC时间与北京时间转换

public static void main(String[] args) throws ParseException {    Date now = new Date();    Date utcDate = bj2UTC(now);    //utc时间     System.out.println(utcDate);    //北京时间    System.out.println(utc2BJ(utcDate));    DateTime dateTime = DateTime.now().withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);    System.out.println(dateTime);    System.out.println(bj2UTC(dateTime.toDate()));}public static Date bj2UTC(Date date) {    if (date == null) {        return null;    }    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("-8"));    return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant());}public static Date utc2BJ(Date date) {    if (date == null) {        return null;    }    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("+8"));    return Date.from(localDateTime.atOffset(ZoneOffset.UTC).toInstant());}

北京时间=UTC+8

到此,关于"Java中常用时间的相关方法有哪些"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

时间 方法 小时 中常 学习 北京 加一 两个 日期 更多 帮助 实用 接下来 单位 字符 字符串 年份 年月 年月日 年龄 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网金融科技 科技互联网企业 科技赋能下的互联网金融 科技互联网技术 杭州互联网科技公司 深圳齐家互联网科技 上海电发互联网科技有限公司 农村开互联网科技讲堂 栖莱科技互联网股份有限公司 青岛科技大学互联网大赛研究生 深圳市优智互联网科技有限公司 健康食品互联网创新科技园项目 深圳市罗太互联网科技有限公司 四川慕妮娅互联网科技有限公司 爱医互联网络科技 互联网时代的科技公司 富国互联网科技002692 众联互联网络科技 银川新华互联网科技学校招聘 小牛互联网科技有限公司地址 江苏根荣互联网科技 河南真二互联网科技有限公司 中国互联网科技发展 狮子座互联网科技有限公司 丰台科技园 互联网公司 互联网科技公司招聘 洛阳共同富裕互联网科技有限公司 深圳无忧互联网科技 图游互联网科技 山东亿川互联网科技有限责任公司
0