localtime的用法_’s和s’的用法

localtime的用法_’s和s’的用法常规使用LocalDateTimenow=LocalDateTime.now();//2020-01-29T14:35:51.207intyear=now.getYear();//2020M

localtime的用法_'s和s'的用法"

常规使用

LocalDateTime now = LocalDateTime.now();   // 2020-01-29T14:35:51.207
int year = now.getYear();                  // 2020
Month month = now.getMonth();              // JANUARY
int monthValue = now.getMonthValue();      // 1
int dayOfYear = now.getDayOfYear();        // 29
int dayOfMonth = now.getDayOfMonth();      // 29
DayOfWeek dayOfWeek = now.getDayOfWeek();  // WEDNESDAY
int dayOfWeekValue = dayOfWeek.getValue(); // 3
int hour = now.getHour();                  // 14
int minute = now.getMinute();              // 35
int second = now.getSecond();              // 51

long seconds = Instant.now().getEpochSecond();    // 秒时间戳(10位)
long milliSeconds = Instant.now().toEpochMilli(); // 毫秒时间戳(13位)

LocalDateTime dateTimeFromSecond = LocalDateTime.ofInstant(
    Instant.ofEpochSecond(seconds), ZoneOffset.ofHours(8)
);  // 秒转datetime

LocalDateTime dateTimeFromMilliSecond = LocalDateTime.ofInstant(
    Instant.ofEpochMilli(milliSeconds), ZoneOffset.ofHours(8)
); // 毫秒转datetime

各种转换

String format = LocalDateTime.now().format(
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
);  // datetime转字符串

LocalDateTime dateTime = LocalDateTime.parse(
    format,
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
);  // 字符串转datetime

Date date = Date.from(dateTime.toInstant(ZoneOffset.ofHours(8)));                             // datetime转date
LocalDateTime fromDate = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime();  // date转datetime

自定义日期 / 日期加减

LocalDateTime dateTime = LocalDateTime.of(2020, 1, 29, 14, 35, 51); // 2020-01-29T14:35:51

LocalDateTime localDateTime1 = dateTime.minusSeconds(1);            // 2020-01-29T14:35:50
LocalDateTime localDateTime2 = dateTime.minusMinutes(1);            // 2020-01-29T14:34:51
LocalDateTime localDateTime3 = dateTime.minusHours(1);              // 2020-01-29T13:35:51
LocalDateTime localDateTime4 = dateTime.minusDays(1);               // 2020-01-28T14:35:51
LocalDateTime localDateTime5 = dateTime.minusWeeks(1);              // 2020-01-22T14:35:51
LocalDateTime localDateTime6 = dateTime.minusMonths(1);             // 2019-12-29T14:35:51
LocalDateTime localDateTime7 = dateTime.minusYears(1);              // 2019-01-29T14:35:51

增加的为对应的plus函数,如dateTime.plusSeconds(1)

比较日期

LocalDateTime now = LocalDateTime.now();     // 2020-01-29T14:35:51.207
LocalDateTime yesterday = now.minusDays(1);  // 2020-01-28T14:35:51.207
boolean after = now.isAfter(yesterday);      // true
boolean before = now.isBefore(yesterday);    // false
boolean equal = now.isEqual(yesterday);      // false

其他类

LocalDate日期

LocalDate dateNow = LocalDate.now();        // 2020-01-29
LocalDate date = LocalDate.of(2020, 1, 29); // 2020-01-29

LocalTime时间

LocalTime timeNow = LocalTime.now();       // 14:35:51.207
LocalTime time = LocalTime.of(23, 59, 59); // 23:59:59

LocalDateTime日期时间

LocalDateTime dateTimeNow = LocalDateTime.now();                    // 2020-01-29T14:35:51.207
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 29, 23, 59, 59); // 2020-01-29T23:59:59

Instant时刻

long second = Instant.now().getEpochSecond();          // 1580279751
long milliSecond = Instant.now().toEpochMilli();       // 1580279751000
Instant instant1 = Instant.ofEpochSecond(second);     // 2020-01-29T14:35:51Z
Instant instant2 = Instant.ofEpochMilli(milliSecond); // 2020-01-29T14:35:51.207Z

Duration时间差

Duration duration = Duration.between(
    LocalDateTime.now(), LocalDateTime.now().plusDays(1)
);  // PT24H
long seconds = duration.getSeconds();             // 86400
long seconds2 = duration.get(ChronoUnit.SECONDS); // 86400

Duration ofDays = Duration.ofDays(1);             // PT24H
Duration ofHours = Duration.ofHours(1);           // PT1H
Duration ofMinutes = Duration.ofMinutes(1);       // PT1M
Duration ofSeconds = Duration.ofSeconds(1);       // PT1S

Period时间段

Period between = Period.between(
    LocalDate.of(2020, 1, 22), LocalDate.of(2020, 2, 9)
); // P18D
int year = between.getYears();   // 0
int month = between.getMonths(); // 0
int day = between.getDays();     // 18

Period period = Period.of(1, 2, 3); // P1Y2M3D
long years = period.get(ChronoUnit.YEARS);            // 1
long months = period.get(ChronoUnit.MONTHS);          // 2
long days = period.get(ChronoUnit.DAYS);              // 3

Clock时钟

Clock clock1 = Clock.system(ZoneOffset.ofHours(8));  // SystemClock[+08:00]
Clock clock2 = Clock.systemUTC();                    // SystemClock[Z]

long millis1 = clock1.millis();      // 1582285924008
long millis2 = clock2.millis();      // 1582285924008
Instant instant1 = clock1.instant(); // 2020-02-21T11:54:58.427Z
Instant instant2 = clock2.instant(); // 2020-02-21T11:54:58.428Z

天猫超市优惠地址

今天的文章localtime的用法_’s和s’的用法分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/72065.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注