更新时间:2022-10-11 10:52:01 来源:极悦 浏览2182次
以下是一些代码片段,用于在 Java 中显示当前日期时间。
对于java.time.LocalDate, 使用LocalDate.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); // 2021/03/22
对于java.time.localTime, 使用LocalTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
System.out.println(dtf.format(localTime)); // 16:37:15
对于java.time.LocalDateTime, 使用LocalDateTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); // 2021/03/22 16:37:15
对于java.time.ZonedDateTime, 使用ZonedDateTime.now()
// get current date-time, with system default time zone
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
ZonedDateTime now = ZonedDateTime.now();
System.out.println(dtf.format(now)); // 2021/03/22 16:37:15
System.out.println(now.getOffset()); // +08:00
// get current date-time, with a specified time zone
ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println(dtf.format(japanDateTime)); // 2021/03/22 17:37:15
System.out.println(japanDateTime.getOffset()); // +09:00
对于java.time.Instant, 使用Instant.now()
Instant now = Instant.now();
// convert Instant to ZonedDateTime
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDateTime.format(zonedDateTime));
对于java.util.Date, 使用new Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15
对于java.util.Calendar, 使用Calendar.getInstance()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); // 2021/03/22 16:37:15
对于java.time.LocalDate, 用于LocalDate.now()获取没有时区的当前日期,并使用DateTimeFormatter.
package com.mkyong.app;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); // 2021/03/22
}
}
输出
2021/03/22
对于java.time.LocalTime, 用于LocalDate.now()获取没有时区的当前时间,并使用DateTimeFormatter.
package com.mkyong.app;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalTimeExample {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
System.out.println(dtf.format(localTime)); // 16:37:15
}
}
输出
16:37:15
对于java.time.LocalDateTime, 用于LocalDateTime.now()获取没有时区的当前日期时间,并使用DateTimeFormatter.
package com.mkyong.app;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); // 2021/03/22 16:37:15
}
}
输出
2021/03/22 16:37:15
对于java.time.ZonedDateTime, 用于ZonedDateTime.now()获取系统默认时区或指定时区的当前日期时间。
package com.mkyong.app;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExample {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
// Get default time zone
System.out.println(ZoneOffset.systemDefault()); // Asia/Kuala_Lumpur
System.out.println(OffsetDateTime.now().getOffset()); // +08:00
// get current date time, with +08:00
ZonedDateTime now = ZonedDateTime.now();
System.out.println(dtf.format(now)); // 2021/03/22 16:37:15
System.out.println(now.getOffset()); // +08:00
// get get current date time, with +09:00
ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println(dtf.format(japanDateTime)); // 2021/03/22 17:37:15
System.out.println(japanDateTime.getOffset()); // +09:00
}
}
输出
Asia/Kuala_Lumpur
+08:00
2021/03/22 16:37:15
+08:00
2021/03/22 17:37:15
+09:00
对于java.time.Instant, 用于Instant.now()获取自Unix 纪元时间(UTC 时间 1970 年 1 月 1 日午夜)以来经过的秒数,然后转换为其他java.time.*日期时间类,如LocalDate,LocalDateTime和ZonedDateTime.
package com.mkyong.app;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class InstantExample {
private static final DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("uuuu/MM/dd");
private static final DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("HH:mm:ss");
private static final DateTimeFormatter dtfDateTime = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
public static void main(String[] args) {
// seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC)
Instant now = Instant.now();
// convert Instant to LocalDate
LocalDate localDate = LocalDate.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDate.format(localDate));
// convert Instant to localTime
LocalTime localTime = LocalTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfTime.format(localTime));
// convert Instant to LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDateTime.format(localDateTime));
// convert Instant to ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDateTime.format(zonedDateTime));
}
}
输出
2021/03/22
16:37:15
2021/03/22 16:37:15
2021/03/22 16:37:15
对于 legacy java.util.Date,使用new Date()ornew Date(System.currentTimeMillis()获取当前日期时间,并使用SimpleDateFormat.
package com.mkyong.app;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateExample {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15
// new Date() actually calls this new Date(long date)
Date date2 = new Date(System.currentTimeMillis());
System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15
}
}
输出
2021/03/22 16:37:15
2021/03/22 16:37:15
对于 legacy java.util.Calendar,用于Calendar.getInstance()获取当前日期时间,并使用SimpleDateFormat.
package com.mkyong.app;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); // 2021/03/22 16:37:15
}
}
输出
2021/03/22 16:37:15
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习