87网站建设工作室,汕头网站推广制作怎么做,视频教做家常菜的网站,九九电视剧免费观看完整版文章目录 1.Date2.LocalDate3.LocalTime4.LocalDateTime5.DateTimeFormatter6.Period7.Duration8.ZoneId9.ZonedDateTime总结 日期和时间是我们常见的操作对象#xff0c;Java 也提供了强大的日期和时间处理类库#xff0c;使我们可以方便的进行日期的表示、计算、格式化等。… 文章目录 1.Date2.LocalDate3.LocalTime4.LocalDateTime5.DateTimeFormatter6.Period7.Duration8.ZoneId9.ZonedDateTime总结 日期和时间是我们常见的操作对象Java 也提供了强大的日期和时间处理类库使我们可以方便的进行日期的表示、计算、格式化等。 1.Date
在 Java 早期版本中主要使用 java.util.Date 类来表示日期和时间
示例代码
public class Demo {public static void main(String[] args) {// 获取当前时间Date currentDate new Date();System.out.println(currentDate);// 获取时间戳long timestamp currentDate.getTime();System.out.println(timestamp);}
}运行结果
Thu Dec 07 20:58:11 CST 2023 1701953891262
2.LocalDate
表示日期 提供了丰富的方法来进行日期操作
示例代码
public class Demo {public static void main(String[] args) {// 获取当前日期LocalDate currentDate LocalDate.now();System.out.println(currentDate);}
}输出结果
2023-12-07
3.LocalTime
表示时间提供了丰富的方法来进行时间的操作
示例代码
public class Demo {public static void main(String[] args) {// 获取当前时间LocalTime currentTime LocalTime.now();System.out.println(currentTime);}
}输出结果
21:08:21.256
4.LocalDateTime
表示日期时间 提供了丰富的方法来进行时间的操作
示例代码
public class Demo {public static void main(String[] args) {// 获取当前日期时间LocalDateTime currentDateTime LocalDateTime.now();System.out.println(currentDateTime);}
}输出结果
2023-12-07T21:08:21.256
5.DateTimeFormatter
用于格式化和解析日期时间它提供了丰富的预定义格式同时也支持自定义格式
示例代码
public class Demo {public static void main(String[] args) {LocalDateTime currentDateTime LocalDateTime.now();// 使用预定义格式String formattedDateTime currentDateTime.format(DateTimeFormatter.ISO_DATE_TIME);System.out.println(formattedDateTime);// 使用自定义格式DateTimeFormatter customFormatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);String customFormattedDateTime currentDateTime.format(customFormatter);System.out.println(customFormattedDateTime);}
}输出结果
2023-12-07T21:02:23.796 2023-12-07 21:02:23
6.Period
用于表示日期之间的差距
示例代码
public class Demo {public static void main(String[] args) {LocalDate startDate LocalDate.of(2020, 1, 1);LocalDate endDate LocalDate.now();// 计算日期之间的差距Period period Period.between(startDate, endDate);System.out.println(Years: period.getYears() , Months: period.getMonths() , Days: period.getDays());}
}输出结果
Years: 3, Months: 11, Days: 6
7.Duration
用于表示时间之间的差距
示例代码
public class Demo {public static void main(String[] args) {LocalDate startDate LocalDate.of(2020, 1, 1);LocalDate endDate LocalDate.now();// 计算时间之间的差距long daysDiff ChronoUnit.DAYS.between(startDate, endDate);System.out.println(Days Difference: daysDiff);}
}输出结果
Days Difference: 1436
8.ZoneId
用于表示时区
示例代码
public class Demo {public static void main(String[] args) {LocalDateTime localDateTime LocalDateTime.now();// 获取系统默认时区ZoneId systemZone ZoneId.systemDefault();ZonedDateTime zonedDateTime ZonedDateTime.of(localDateTime, systemZone);System.out.println(zonedDateTime);}
}输出结果
2023-12-07T21:07:11.52308:00[Asia/Shanghai]
9.ZonedDateTime
用于获取日期和时间以及时区信息
示例代码
public class Demo {public static void main(String[] args) {LocalDateTime localDateTime LocalDateTime.now();// 获取指定时区ZoneId newYorkZone ZoneId.of(America/New_York);ZonedDateTime newYorkDateTime ZonedDateTime.of(localDateTime, newYorkZone);System.out.println(newYorkDateTime);}
}输出结果
2023-12-07T21:07:11.523-05:00[America/New_York]
总结
Java 提供了丰富的日期和时间处理类包括 Date、LocalDate、LocalTime、LocalDateTime、DateTimeFormatter、Period、Duration、ZoneId 和 ZonedDateTime。选择合适的类取决于具体的需求但通常推荐使用新的日期和时间 API例如 LocalDateTime以获得更好的可读。