在本文中,您将找到几个使用 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);
}
}
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);
}
}
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);
}
}
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);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.callicoder.com/how-to-get-current-date-time-java/
内容来源于网络,如有侵权,请联系作者删除!