Java新特性-日期
概述
- Java 8 在
Java.time
包下提供了很多新的 API。- Local(本地) 简化了日期时间的处理,没有时区的问题。
- Zoned(时区) 通过制定的时区处理日期时间。
LocalDate
- LocalDate 只会获取
年
月
日
。
创建 LocalDate
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDateOne = LocalDate.now();
LocalDate localDateTwo = LocalDate.of(2020, 11, 28);
}
}
获取当前年月日
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
}
}
构造指定的年月日
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2020, 11, 28);
System.out.println(localDate);
}
}
获取年
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2021, 2, 20);
int yearOne = localDate.getYear();
int yearTwo = localDate.get(ChronoField.YEAR);
System.out.println(yearOne);
System.out.println(yearTwo);
}
}
获取月
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2021, 2, 20);
Month monthOne = localDate.getMonth();
int monthTwo = localDate.get(ChronoField.MONTH_OF_YEAR);
System.out.println(monthOne);
System.out.println(monthTwo);
}
}
获取日
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2021, 2, 20);
int dayOne = localDate.getDayOfMonth();
int dayTwo = localDate.get(ChronoField.DAY_OF_MONTH);
System.out.println(dayOne);
System.out.println(dayTwo);
}
}
获取星期几
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2021, 2, 20);
DayOfWeek dayOfWeekOne = localDate.getDayOfWeek();
int dayOfWeekTwo = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println(dayOfWeekOne);
System.out.println(dayOfWeekTwo);
}
}
LocalTime
- LocalTime 只会获取
时
分
秒
。
创建 LocalTime
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalTime localTimeOne = LocalTime.of(14, 14, 14);
LocalTime localTimeTwo = LocalTime.now();
}
}
获取小时
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
int hourOne = localTime.getHour();
int hourTwo = localTime.get(ChronoField.HOUR_OF_DAY);
System.out.println(hourOne);
System.out.println(hourTwo);
}
}
获取分
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
int minuteOne = localTime.getMinute();
int minuteTwo = localTime.get(ChronoField.MINUTE_OF_HOUR);
System.out.println(minuteOne);
System.out.println(minuteTwo);
}
}
获取秒
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
int secondOne = localTime.getMinute();
int secondTwo = localTime.get(ChronoField.SECOND_OF_MINUTE);
System.out.println(secondOne);
System.out.println(secondTwo);
}
}
LocalDateTime
- 获取年月日时分秒,相当于
LocalDate
+LocalTime
。
创建 LocalDateTime
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTimeOne = LocalDateTime.now();
LocalDateTime localDateTimeTwo = LocalDateTime.of(2019,
Month.SEPTEMBER, 10, 14, 46, 56);
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTimeThree = LocalDateTime.of(localDate, localTime);
LocalDateTime localDateTimeFour = localDate.atTime(localTime);
LocalDateTime localDateTimeFive = localTime.atDate(localDate);
}
}
获取 LocalDate
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTimeOne = LocalDateTime.now();
LocalDate localDate = localDateTimeOne.toLocalDate();
System.out.println(localDate);
}
}
获取 LocalTime
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTimeOne = LocalDateTime.now();
LocalTime localTime = localDateTimeOne.toLocalTime();
System.out.println(localTime);
}
}
Instant 获取秒数
创建 Instant 对象
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
Instant instant = Instant.now();
}
}
获取秒数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
Instant instant = Instant.now();
long currentSecond = instant.getEpochSecond();
System.out.println(currentSecond);
}
}
获取毫秒数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
Instant instant = Instant.now();
long currentMilli = instant.toEpochMilli();
System.out.println(currentMilli);
}
}
如果只是为了获取秒数或者毫秒数,可以使用如下的方式进行获取。
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
long l = System.currentTimeMillis();
System.out.println(l);
}
}
Duration
- Duration:表示一个时间段。
创建 Duration 对象
通过 Duration.between()
方法进行创建, 接收两个参数一个 from
, 一个 to
, 表示从 from 到 to 这段时间。
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0);
LocalDateTime to = LocalDateTime.of(2020, Month.SEPTEMBER, 12, 14, 28, 0);
Duration duration = Duration.between(from, to);
}
}
获取这段时间的总天数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0);
LocalDateTime to = LocalDateTime.of(2020, Month.SEPTEMBER, 12, 14, 28, 0);
Duration duration = Duration.between(from, to);
long days = duration.toDays();
System.out.println(days);
}
}
获取这段时间的小时数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0);
LocalDateTime to = LocalDateTime.of(2020, Month.SEPTEMBER, 12, 14, 28, 0);
Duration duration = Duration.between(from, to);
long hours = duration.toHours();
System.out.println(hours);
}
}
获取这段时间的分钟数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0);
LocalDateTime to = LocalDateTime.of(2020, Month.SEPTEMBER, 12, 14, 28, 0);
Duration duration = Duration.between(from, to);
long minutes = duration.toMinutes();
System.out.println(minutes);
}
}
获取这段时间的秒数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0);
LocalDateTime to = LocalDateTime.of(2020, Month.SEPTEMBER, 12, 14, 28, 0);
Duration duration = Duration.between(from, to);
long seconds = duration.getSeconds();
System.out.println(seconds);
}
}
获取这段时间的毫秒数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0);
LocalDateTime to = LocalDateTime.of(2020, Month.SEPTEMBER, 12, 14, 28, 0);
Duration duration = Duration.between(from, to);
long milliSeconds = duration.toMillis();
System.out.println(milliSeconds);
}
}
获取这段时间的纳秒数
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0);
LocalDateTime to = LocalDateTime.of(2020, Month.SEPTEMBER, 12, 14, 28, 0);
Duration duration = Duration.between(from, to);
long nanoSeconds = duration.toNanos();
System.out.println(nanoSeconds);
}
}
日期操作
增加一年
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
localDateTime = localDateTime.plusYears(1);
localDateTime = localDateTime.plus(1, ChronoUnit.YEARS);
System.out.println(localDateTime.toLocalDate());
System.out.println(localDateTime.toLocalTime());
}
}
减少一个月
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
localDateTime = localDateTime.minusMonths(1);
localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS);
System.out.println(localDateTime.toLocalDate());
System.out.println(localDateTime.toLocalTime());
}
}
通过 with 修改某些值
修改年
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
localDateTime = localDateTime.withYear(2021);
localDateTime = localDateTime.with(ChronoField.YEAR, 2020);
System.out.println(localDateTime.toLocalDate());
System.out.println(localDateTime.toLocalTime());
}
}
时间计算
获取该年的第一天
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
LocalDate oneDay = localDate.with(TemporalAdjusters.firstDayOfYear());
System.out.println(oneDay);
}
}
TemporalAdjusters 类
格式化时间
/**
* @author BNTang
*/
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
String strOne = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
String strTwo = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("s1:" + strOne);
System.out.println("s2:" + strTwo);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("获取当前时间:" + localDateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String s = localDateTime.format(formatter);
System.out.println("格式化当前时间:" + s);
}
}