LocalDateTime类常用方法

public class DemoLocalDateTime {
    public static void main(String[] args) {
        // static LocalDateTime	MAX:支持的最大本地日期时间(不包括时区)
        LocalDateTime max = LocalDateTime.MAX;// +999999999-12-31T23:59:59.999999999
        // static LocalDateTime	MIN:支持的最小本地日期时间(不包括时区)
        LocalDateTime min = LocalDateTime.MIN;// -999999999-01-01T00:00
        // static LocalDateTime	now():获取本地当前的日期时间(不含时区)
        LocalDateTime now = LocalDateTime.now(); // 2019-10-17T15:44:03.640
        // static LocalDateTime	now(Clock clock):从指定的时钟获取当前日期时间:这里用了UTC(世界标准时间)
        LocalDateTime nowClock = LocalDateTime.now(Clock.systemUTC()); // 2019-10-17T07:57:42.404
        // static LocalDateTime	now(ZoneId zone):从指定时区中的系统时钟获取当前日期时间
        LocalDateTime nowZoneId = LocalDateTime.now(ZoneId.systemDefault()); // 2019-10-17T16:09:04.127
        // static LocalDateTime	of(int year, int month, int dayOfMonth, int hour, int minute)
        // 从年,月,日,小时和分钟获取LocalDateTime的实例,并将秒和毫秒设置为零。
        LocalDateTime dateTime = LocalDateTime.of(2019, 10, 12, 6, 30); // 2019-10-12T06:30
        // static LocalDateTime	of(int year, int month, int dayOfMonth, int hour, int minute, int second)
        // static LocalDateTime	of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
        // static LocalDateTime	of(int year, Month month, int dayOfMonth, int hour, int minute)
        LocalDateTime localDateTime = LocalDateTime.of(2019, Month.OCTOBER, 12, 6, 30); // 2019-10-12T06:30
        // static LocalDateTime	of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
        // static LocalDateTime	of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
        // static LocalDateTime	of(LocalDate date, LocalTime time):根据日期和时间获取LocalDateTime的实例
        LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.now()); // 2019-10-17T16:33:42.714
        // static LocalDateTime	ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)
        // 使用从1970-01-01T00:00:00Z的纪元开始的秒数获取LocalDateTime的实例,参数1:从纪元开始的秒数,参数2:毫秒数,参数3:时区
        LocalDateTime time = LocalDateTime.ofEpochSecond(55L, 99999, ZoneOffset.UTC); // 1970-01-01T00:00:55.000099999
        // static LocalDateTime	ofInstant(Instant instant, ZoneId zone)
        // 从Instant和区域ID获取LocalDateTime的实例。
        LocalDateTime ofInstant = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); // 2019-10-17T16:54:50.941
        // static LocalDateTime	parse(CharSequence text)
        // 将字符串转化成日期时间对象
        LocalDateTime parse = LocalDateTime.parse("2019-10-17T16:54:50.941"); // 2019-10-17T16:54:50.941
        // static LocalDateTime	parse(CharSequence text, DateTimeFormatter formatter)
        // 使用特定格式化程序将文本字符串转成LocalDateTime对象
        LocalDateTime parse1 = LocalDateTime.parse("2019-10-17T16:54:50.941+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); // 2019-10-17T16:54:50.941
        // LocalDateTime plus(long amountToAdd, TemporalUnit unit)
        // 在该日期时间基础上增加或减少一定时间,参数1:时间量,可以是负数,参数2:时间单位
        LocalDateTime plus = parse.plus(1L, ChronoUnit.HOURS); // 2019-10-17T17:54:50.941
        // LocalDateTime plus(TemporalAmount amountToAdd):在该时间基础上增加一定时间
        LocalDateTime plus1 = parse.plus(Period.ofDays(1)); // 2019-10-18T16:54:50.941
        // LocalDateTime    plusDays(long days):增加指定天数
        LocalDateTime plus2 = parse.plusDays(1L); // 2019-10-18T16:54:50.941
        // LocalDateTime	plusHours(long hours):增加指定小时数
        // LocalDateTime	plusMinutes(long minutes):增加指定分钟数
        // LocalDateTime	plusMonths(long months):增加指定月份数
        // LocalDateTime	plusNanos(long nanos):增加指定毫秒数
        // LocalDateTime	plusSeconds(long seconds):增加指定秒数
        // LocalDateTime	plusWeeks(long weeks):增加指定周数
        // LocalDateTime	plusYears(long years):增加指定年数

    }
}
发布了86 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43365369/article/details/102614741