如何在 Java 中获取当前日期和时间

x33g5p2x  于2021-10-16 转载在 Java  
字(1.1k)|赞(0)|评价(0)|浏览(497)

在本文中,您将找到几个使用 Java 获取特定时区中的当前日期、当前时间、当前日期和时间、当前日期和时间的示例。

###在Java中获取当前日期

import java.time.LocalDate;

public class CurrentDateTimeExample {
    public static void main(String[] args) {
        // Current Date
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current Date: " + currentDate);
    }
}

在 Java 中获取当前时间

import java.time.LocalTime;

public class CurrentDateTimeExample {
    public static void main(String[] args) {
        // Current Time
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Time: " + currentTime);
    }
}

在 Java 中获取当前日期和时间

import java.time.LocalDateTime;

public class CurrentDateTimeExample {
    public static void main(String[] args) {
        // Current Date and Time
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date & time: " + currentDateTime);
    }
}

获取 Java 中特定时区的当前日期和时间

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class CurrentDateTimeExample {
    public static void main(String[] args) {
        // Current Date and Time in a given Timezone
        ZonedDateTime currentNewYorkDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println(currentNewYorkDateTime);
    }
}

相关文章

微信公众号

最新文章

更多