欢迎访问宙启技术站
智能推送

Java日期函数和时间函数的详细解释

发布时间:2023-06-17 12:45:10

Java语言中提供了许多用于操作日期和时间的函数,这些函数可用于获取当前日期时间、日期的加减、日期格式化、比较等操作。下面是对Java日期函数和时间函数的详细解释。

一、日期函数

1. 获取当前日期

Java中提供了许多获取当前日期的函数,例如:

(1)Date()函数:返回当前日期时间

(2)Calendar.getInstance()函数:返回一个Calendar对象,表示当前时间

(3)LocalDate.now()函数:返回当前日期

(4)LocalDateTime.now()函数:返回当前日期时间

示例代码:

Date date = new Date();

System.out.println(date);

Calendar calendar = Calendar.getInstance();

System.out.println(calendar.getTime());

LocalDate localDate = LocalDate.now();

System.out.println(localDate);

LocalDateTime localDateTime = LocalDateTime.now();

System.out.println(localDateTime);

2. 日期加减

Java中提供了add函数用于对日期进行加减操作,例如:

(1)Calendar.add(int field, int amount)函数:在指定字段上加上或减去指定的时间量

(2)LocalDate.plusDays(int days)函数:增加指定天数

(3)LocalDateTime.plusMinutes(int minutes)函数:增加指定分钟数

示例代码:

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.DAY_OF_MONTH, 10);

System.out.println(calendar.getTime());

LocalDate localDate = LocalDate.now();

localDate = localDate.plusDays(7);

System.out.println(localDate);

LocalDateTime localDateTime = LocalDateTime.now();

localDateTime = localDateTime.plusMinutes(30);

System.out.println(localDateTime);

3. 日期格式化

Java中提供了SimpleDateFormat类来对日期进行格式化,例如:

(1)SimpleDateFormat(String pattern)函数:创建一个时间格式对象,并指定格式

(2)format(Date date)函数:将Date对象格式化成字符串

示例代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = new Date();

System.out.println(sdf.format(date));

4. 日期比较

Java中提供了许多比较日期的函数,例如:

(1)Date.before(Date when)函数:如果当天在指定日期之前,则返回true

(2)Calendar.after(Object when)函数:如果当天在指定日期之后,则返回true

(3)LocalDate.isBefore(ChronoLocalDate otherDate)函数:如果当天在指定日期之前,则返回true

(4)LocalDateTime.isAfter(ChronoLocalDateTime<?> otherDateTime)函数:如果当天在指定日期之后,则返回true

示例代码:

Date date1 = new Date();

Date date2 = new Date(date1.getTime() - 1000000);

System.out.println(date1.before(date2));

Calendar calendar1 = Calendar.getInstance();

calendar1.add(Calendar.DAY_OF_MONTH, 10);

Calendar calendar2 = Calendar.getInstance();

calendar2.add(Calendar.DAY_OF_MONTH, 5);

System.out.println(calendar1.after(calendar2));

LocalDate localDate1 = LocalDate.now();

LocalDate localDate2 = LocalDate.now().plusDays(7);

System.out.println(localDate1.isBefore(localDate2));

LocalDateTime localDateTime1 = LocalDateTime.now();

LocalDateTime localDateTime2 = LocalDateTime.now().plusMinutes(30);

System.out.println(localDateTime1.isAfter(localDateTime2));

二、时间函数

1. 获取当前时间

Java中提供了许多获取当前时间的函数,例如:

(1)System.currentTimeMillis()函数:返回当前时间的毫秒数

(2)Instant.now()函数:获取当前时间

(3)LocalTime.now()函数:获取当前时间

示例代码:

long currTime = System.currentTimeMillis();

System.out.println("当前时间的毫秒数:" + currTime);

Instant instant = Instant.now();

System.out.println("当前时间:" + instant);

LocalTime localTime = LocalTime.now();

System.out.println("当前时间:" + localTime);

2. 时间加减

Java中提供了plus和minus函数用于对时间进行加减操作,例如:

(1)LocalTime.plusMinutes(int minutes)函数:增加指定分钟数

(2)LocalTime.minusHours(long hours)函数:减去指定小时数

示例代码:

LocalTime localTime = LocalTime.now();

localTime = localTime.plusMinutes(30);

System.out.println(localTime);

localTime = localTime.minusHours(2);

System.out.println(localTime);

3. 时间格式化

Java中提供了DateTimeFormatter类来对时间进行格式化,例如:

(1)DateTimeFormatter.ofPattern(String pattern)函数:创建一个时间格式对象,并指定格式

(2)format(TemporalAccessor temporal)函数:将时间对象格式化成字符串

示例代码:

LocalTime localTime = LocalTime.now();

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");

System.out.println(dateTimeFormatter.format(localTime));

4. 时间比较

Java中提供了许多比较时间的函数,例如:

(1)LocalTime.isBefore(LocalTime other)函数:如果当前时间在指定时间之前,则返回true

(2)LocalTime.isAfter(LocalTime other)函数:如果当前时间在指定时间之后,则返回true

示例代码:

LocalTime localTime1 = LocalTime.now();

LocalTime localTime2 = LocalTime.now().plusMinutes(30);

System.out.println(localTime1.isBefore(localTime2));

localTime1 = localTime1.plusHours(1);

System.out.println(localTime1.isAfter(localTime2));

综上所述,Java提供的日期函数和时间函数是使用Java进行时间和日期操作必备的工具函数,可以方便地获取当前日期时间、进行日期的加减、日期格式化、比较等操作。