Java基础之LocalDateTime

本文深入讲解Java中日期和时间的概念及操作,包括日期与时间的基本理解、本地时间的获取、时区的处理、日期时间的格式转换、加减运算、比较、特殊日期的获取以及与时间戳的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间基本概念

日期和时间

日期

  • 2000-11-12
  • 2018-12-23

时间

  • 20:23:21

 

日期是指某一天,他是离散的。

而时间有两种,一种是带日期的,比如:2018/11/12 15:02:23,这种能确定唯一的时刻。 另一种是不带日期的,比如12:23:21,他只能确定某个时间点。

 

本地时间

我们通常说的时间,是指北京时间,也就是本地时间。在同一时刻,不通地区的人,看到的时间是不一样的。

 

全球一共分为24个时区,英国是标准时区,而中国在东八区。

 

时区

全靠本地时间不能确定一个准确的时刻,所以我们还需要时区。

一种是靠GMT和UTC加时区偏移表示。例如GMT+08:00或者UTC+08:00

另外一种是以 洲/市 表示,例如:Asia/Shanghai

 

夏令时

所谓夏令时,就是夏天开始的时候,把时间往后拨一个小时;夏天结束的时候,把时间往前拨一个小时。

 

Java date

本地时间获取

  • date函数

Date date = new Date();

 

System.out.println(date.getYear());

System.out.println(date.getMonth());

System.out.println(date.getDate());

 

System.out.println(date.toString());

System.out.println(date.toGMTString());

 

  • Calendar函数

Calendar calendar=Calendar.getInstance();

 

Int year=calendar.get(Calendar.YEAR);

Int month=calendar.get(Calendar.MONTH)+1;

In tday=calendar.get(Calendar.DAY_OF_MONTH);

Int hour=calendar.get(Calendar.HOUR_OF_DAY);

Int mm=calendar.get(Calendar.MINUTE);

Int ss=calendar.get(Calendar.SECOND);

Int ms=calendar.get(Calendar.MILLISECOND);

 

System.out.println(year+"-"+month+"-"+day+""+hour+":"+mm+":"+ss+"."+ms);

  • Jdk8以前的时区获取

        for (String availableID : TimeZone.getAvailableIDs()) {

            System.out.print(availableID + " ");

        }

        System.out.println("---------------");

        TimeZone timeZone = TimeZone.getDefault(); //当前市区

        TimeZone tzGMT9 = TimeZone.getTimeZone("GMT+09:00"); //GMT+9:00时区

        TimeZone tzNy = TimeZone.getTimeZone("America/New_York");

        System.out.println(timeZone);

        System.out.println(tzGMT9);

        System.out.println(tzNy);

  • Jdk8获取当前事件

LocalDate d = LocalDate.now(); //当前日期

LocalTime t = LocalTime.now(); //当前时间

LocalDateTime dt = LocalDateTime.now(); //本地时间

 

System.out.println(d);

System.out.println(t);

System.out.println(dt);

 

时间转换

LocalDateTime dt = LocalDateTime.now();

LocalDate d = dt.toLocalDate();

LocalTime t = dt.toLocalTime();

System.out.println(d);

System.out.println(t);

System.out.println(dt);

 

获取指定日期

LocalDate localDate = LocalDate.of(2019,9, 12);

LocalTime localTime = LocalTime.of(12, 5, 1);

LocalDateTime localDateTime = LocalDateTime.of(localDate,localTime);

 

System.out.println(localDate);

System.out.println(localTime);

System.out.println(localDateTime);

 

日期格式转换

LocalDate localDate = LocalDate.parse("2110-11-23");

LocalDateTime localDateTime = LocalDateTime.parse("2012-10-25T10:09:31"); //ISO 8601标准格式

System.out.println(localDate);

System.out.println(localDateTime);

 

 注意ISO 8601规定的日期和时间分隔符是T。标准格式如下:

 

日期:yyyy-MM-dd

时间:HH:mm:ss

带毫秒的时间:HH:mm:ss.SSS

日期和时间:yyyy-MM-dd'T'HH:mm:ss

带毫秒的日期和时间:yyyy-MM-dd'T'HH:mm:ss.SSS

 

 

日期格式调整

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

System.out.println(dtf.format(LocalDateTime.now()));

 

// 用自定义格式解析:

LocalDateTime dt2 = LocalDateTime.parse("2019/11/30 15:16:17", dtf);

System.out.println(dt2);

 

加减日期

LocalDateTime dt = LocalDateTime.of(2019, 10, 31, 20, 30, 59);

System.out.println(dt);

// 加5天减3小时:

LocalDateTime dt2 = dt.plusDays(5).minusHours(3);

System.out.println(dt2); // 2019-10-31T17:30:59

 //减1月

LocalDateTime dt3 = dt2.minusMonths(1);

System.out.println(dt3); // 2019-09-30T17:30:59

 

 

日期比较

LocalDateTime now = LocalDateTime.now();

LocalDateTime target = LocalDateTime.of(2019, 11, 19, 8, 15, 0);

System.out.println(now.isBefore(target));

System.out.println(LocalDate.now().isBefore(LocalDate.of(2019, 11, 19)));

System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));

 

特殊日期获取

 // 本月第一天0:00时刻:

LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();

System.out.println(firstDay);

 

// 本月最后1天:

LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());

System.out.println(lastDay);

 

// 下月第1天:

LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());

System.out.println(nextMonthFirstDay);

 

// 本月第1个工作日:

LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));

System.out.println(firstWeekday);

 

//本月第一后周日

LocalDate firstSunday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));

System.out.println(firstSunday);

 

日期与时间戳的转换

LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("GMT+05:00"));

ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("GMT+08:00"));

System.out.println(localDateTime);

System.out.println(zonedDateTime);

 

当前时刻获取

ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区

ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间

System.out.println(zbj);

System.out.println(zny);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值