java中LocalDateTime,Instant,ZoneID,ZoneDateTime,DateTimeFormatter类常用的API

本文详细介绍了Java8中LocalDate,LocalTime,LocalDateTime,Instant,ZoneId,ZonedDateTime,Period和Duration类的用法,包括获取当前时间、日期间隔计算、时区处理以及日期/时间的格式化和解析。
摘要由CSDN通过智能技术生成

LoaclDate LocalTime LocalDateTime类

LocalDate :代表本地日期(年,月,日,星期)

LocalTime:代表本地时间(时,分,秒,纳秒)API

LocalDateTime:代表本地日期,时间

以上三个类获取对象的方法是:public static xxx.now();

import java.time.LocalDate;

public class test {
    public static void main(String[] args) {
        //0:获取本地日期对象(不可变的)
        LocalDate ld=LocalDate.now();//年 月 日
        System.out.println(ld);
        //1:获取日期对象中的信息(get)
        int year =ld.getYear();//年
        int month=ld.getMonthValue();//月份
        int day=ld.getDayOfMonth();//一个月的第几天
        int dayofYear=ld.getDayOfYear();//一年的第几天
        int dayofWeek=ld.getDayOfWeek().getValue();//星期几
        System.out.println(dayofWeek);//星期3

        //2:直接修改某个信息(with)
        LocalDate d2=ld.withYear(2080);//会返回一个新的LocalDate对象
        LocalDate d3=ld.withDayOfMonth(4);
        LocalDate d4=ld.withMonth(5);


        //3:把某个信息加多少(plus)
        LocalDate d5=ld.plusWeeks(3);

        //4:把某个信息减多少(minus)
        LocalDate d6=ld.minusMonths(4);

        //5:获取指定日期的LocalDate对象
        LocalDate d7=LocalDate.of(2050,1,1);
        LocalDate d8=LocalDate.of(2050,1,1);
        System.out.println(d7);//2050-01-01

        //6:判断两个日期对象,是否相等,在前还是在后:equals isBefore isAfter
        System.out.println(d7.equals(d8));//true
        System.out.println(d7.isAfter(ld));//true
        System.out.println(d7.isBefore(ld));//false

    }
}
//7:可以直接把LocalDateTime对象变成LocalDate对象和LocalTime
LocalDateTime ldt=LocalDateTime.now();
System.out.println(ldt);
LocalDate d9=ldt.toLocalDate();
LocalTime t1=ldt.toLocalTime();

//也可以把LocalDate和LocalTime对象变成一个LocalDateTime对象
LocalDateTime ldt2=LocalDateTime.of(d9,t1);

总结:这三个类获取对象的方法有

public static xxx now();

public static xxx of();获取指定时间的对象

 ZoneId时区类:
public static ZoneId systemDefault()

Gets the system default time-zone.

获取系统默认时区 

public static ZoneId of(String zoneId) 

 Obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use.

获取一个指定时区

public static Set<String> getAvailableZoneIds()

Gets the set of available zone IDs. 

获取java支持的所有时区

 ZoneDateTime(LocalDateTime+ZoneId)带时区时间的常见方法:

public static ZonedDateTime now()

Obtains the current date-time from the system clock in the default time-zone. 

获取当前时区的ZoneDateTime对象

public static ZonedDateTime now(ZoneId zone)

Obtains the current date-time from the system clock in the specified time-zone.

获取指定时区的ZoneDateTime对象

 

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class test {
    public static void main(String[] args) {
        //1:获取系统默认时区
        ZoneId zoneid=ZoneId.systemDefault();
        System.out.println(zoneid.getId());//Asia/Shanghai
        System.out.println(zoneid);//Asia/Shanghai
        //2:获取所有时区
        System.out.println(ZoneId.getAvailableZoneIds());

        //获取指定时区的ZoneId对象
        ZoneId zoneid2=ZoneId.of("America/New_York");
        System.out.println(zoneid2);//America/New_York

        //获取当前时区的ZoneDateTime对象
        ZonedDateTime z1=ZonedDateTime.now();
        System.out.println(z1);//2024-04-04T20:13:17.006835900+08:00[Asia/Shanghai]
        //获取指定时区的ZoneDateTime对象
        ZonedDateTime z2=ZonedDateTime.now(zoneid2);
        System.out.println(z2);//2024-04-04T08:14:52.356801600-04:00[America/New_York]

        //获取世界标准时间
        ZonedDateTime z3=ZonedDateTime.now(Clock.systemUTC());
        System.out.println(z3);//2024-04-04T12:17:47.416688700Z
    }
}

 Instant类:时间线上的某个时刻/时间戳

通过获取Instant的对象可以拿到此刻的时间,该时间有两个部分组成:从1970-1-1 00:00:00开始到现在的总秒数和不到一秒的纳秒数

public static Instant now()

Obtains the current instant from the system clock.

获取当前时间的Instant对象

public long getEpochSecond()

Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.

public int getNano()

Gets the number of nanoseconds, later along the time-line, from the start of the second. 

获取不满1s的纳秒

public class test {
    public static void main(String[] args) {
        //获取当前时间的Instant对象
        Instant i1=Instant.now();//不可变对象
        System.out.println(i1);//2024-04-04T12:29:43.772489700Z

        //获取总秒数
        long second= i1.getEpochSecond();
        System.out.println(second);//1712233841

        //获取不满1s的纳秒数
        int nano= i1.getNano();
        System.out.println(nano);//930559300
    }
}

 DateTimeFormatter类

public static DateTimeFormatter ofPattern(String pattern)

 Creates a formatter using the specified pattern.

获取格式化器对象

public String format(TemporalAccessor temporal)

Formats a date-time object using this formatter. 

格式化时间

public class test {
    public static void main(String[] args) {
        DateTimeFormatter d=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

        //对时间格式化
        LocalDateTime ldt=LocalDateTime.now();
        System.out.println(ldt);//2024-04-04T20:42:16.266254400
        String s= d.format(ldt);
        System.out.println(s);//2024年04月04日 20:52:39
        //第二种格式化的方法,LocalDateTime自己调用format方法
        String rs= ldt.format(d);
        System.out.println(rs);//2024年04月04日 20:52:39


        //解析时间,使用LccalDateTime的parse方法
        String dateStr="2024年12月12日 20:42:16";
        LocalDateTime ldt2=LocalDateTime.parse(dateStr,d);
        System.out.println(ldt2);//2024-12-12T20:42:16
    }
}

 Period类:计算日期间隔(年,月,日)

用于两个LocalDate对象

public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)

Obtains a Period consisting of the number of years, months, and days between two dates.

传入两个日期对象,得到period对象

public class test {
    public static void main(String[] args) {
        //创建一个Period对象
        LocalDate d1=LocalDate.of(2028,3,1);
        LocalDate d2=LocalDate.of(2050,2,5);
        Period p=Period.between(d1,d2);

        //获取相差信息
        int year=p.getYears();
        int month=p.getMonths();
        int day=p.getDays();
        System.out.println(year);//21
        System.out.println(month);//11
        System.out.println(day);//4
    }
}

 Duration

可以用于计算两个时间对象相差的天数,小时数,分数,秒数,纳秒数;支持LocalTime,LocalDateTime,Instant

public static Duration between(Temporal startInclusive, Temporal endExclusive)

 传入两个时间对象,得到Duration对象

public class test {
    public static void main(String[] args) {
        LocalDateTime l1=LocalDateTime.of(2025,11,11,12,12,10);
        LocalDateTime l2=LocalDateTime.of(2024,11,23,10,10,10);
        //1:得到Duration对象
        Duration d=Duration.between(l1,l2);
        
        //2:获得差值
        System.out.println(d.toDays());//间隔多少天
        System.out.println(d.toHours());//间隔多少小时
        System.out.println(d.toMinutes());//间隔多少分
        System.out.println(d.toSeconds());//间隔多少秒
        System.out.println(d.toMillis());//间隔多少毫秒
        System.out.println(d.toNanos());//间隔多少纳秒
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落落落sss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值