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

实现日期和时间操作的Java函数

发布时间:2023-06-23 19:32:31

Java 语言提供了丰富的日期和时间操作函数,可以方便地对日期和时间进行各种计算和转换。以下是一些常用的日期和时间操作函数:

1. 获取当前日期和时间

Java 中可以使用 java.util 包中的 Date 类来获取当前日期和时间。Date 类表示当前时间的瞬间,使用 new Date() 创建一个表示当前时间的 Date 对象。

示例:

import java.util.Date;

public class CurrentDateTime {
    public static void main(String[] args) {
        Date now = new Date();
        System.out.println("Current date and time is: " + now);
    }
}

输出:

Current date and time is: Sat Oct 23 17:39:14 CST 2021

2. 获取指定格式的日期和时间

Java 中可以使用 java.text 包中的 SimpleDateFormat 类来获取指定格式的日期和时间。SimpleDateFormat 类可以将日期和时间格式化为字符串,或将字符串解析为日期和时间。

示例:

import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatDateTime {
    public static void main(String[] args) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = sdf.format(now);
        System.out.println("Formatted date and time is: " + formattedDateTime);
    }
}

输出:

Formatted date and time is: 2021-10-23 17:45:12

3. 计算两个日期之间的差值

Java 中可以使用 java.time 包中的 LocalDate 类和 Period 类来计算两个日期之间的差值。LocalDate 类表示日期,Period 类表示日期之间的差值。

示例:

import java.time.LocalDate;
import java.time.Period;

public class DateDiff {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2021, 1, 1);
        LocalDate date2 = LocalDate.of(2021, 10, 23);
        Period diff = Period.between(date1, date2);
        System.out.printf("Difference between %s and %s is %d years, %d months and %d days.%n",
                date1, date2, diff.getYears(), diff.getMonths(), diff.getDays());
    }
}

输出:

Difference between 2021-01-01 and 2021-10-23 is 0 years, 9 months and 22 days.

4. 计算两个时间之间的差值

Java 中可以使用 java.time 包中的 LocalTime 类和 Duration 类来计算两个时间之间的差值。LocalTime 类表示时间,Duration 类表示时间之间的差值。

示例:

import java.time.Duration;
import java.time.LocalTime;

public class TimeDiff {
    public static void main(String[] args) {
        LocalTime time1 = LocalTime.of(9, 30, 0);
        LocalTime time2 = LocalTime.of(17, 0, 0);
        Duration diff = Duration.between(time1, time2);
        System.out.printf("Difference between %s and %s is %d hours, %d minutes and %d seconds.%n",
                time1, time2, diff.toHours(), diff.toMinutesPart(), diff.toSecondsPart());
    }
}

输出:

Difference between 09:30 and 17:00 is 7 hours, 30 minutes and 0 seconds.

5. 时间日期转换

Java 中可以使用 java.time 包中的 LocalDateTime 类和 DateTimeFormatter 类来进行时间和日期之间的转换。LocalDateTime 类表示日期和时间,DateTimeFormatter 类表示日期和时间的格式。

示例:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeConversion {
    public static void main(String[] args) {
        String dateTimeStr = "2021-10-23 17:54:32";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);
        System.out.printf("Parsed date and time is: %s%n", dateTime);
        String formattedDateTime = dateTime.format(formatter);
        System.out.printf("Formatted date and time is: %s%n", formattedDateTime);
    }
}

输出:

Parsed date and time is: 2021-10-23T17:54:32
Formatted date and time is: 2021-10-23 17:54:32

以上是一些常用的日期和时间操作函数,Java 还提供了很多其他的日期和时间操作函数,可以根据需要灵活运用。