JAVA中的日期
嘟哇嘟 2024-10-09 14:35:02 阅读 63
获取当前的日期 LocalDate
<code> LocalDate today = LocalDate.now();
System.out.println("今天是:"+today);//今天是:2024-05-06
String format = today.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
System.out.println("今天是:" + format);//今天是:2024年05月06日
String format1 = today.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
System.out.println("今天是:" + format1);//今天是:2024年05月06日
获取年月日
LocalDate today = LocalDate.now();
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
//Year: 2024 Month: 5 Day: 6
System.out.printf("Year: %d Month: %d Day: %d%n", year, month, day);
日期创建
LocalDate today = LocalDate.of(2017, 9, 1);
//today = 2017-09-01
System.out.println("today = " + today);
两个日期判断是否相等
LocalDate today = LocalDate.now();
LocalDate before = LocalDate.of(2017, 9, 1);
if (today.equals(before)) System.out.println("相等");
else System.out.println("不相等");
YearMonth
YearMonth currentYearMonth = YearMonth.now();
//当前是 2023-05 月 共: 31 天 %n 表示换行
System.out.printf("当前是 %s 本月共: %d 天 %n", currentYearMonth, currentYearMonth.lengthOfMonth());
YearMonth creditCardExpiry = YearMonth.of(2029, Month.APRIL);
//你的信用卡将于 2029-04 到期
System.out.printf("你的信用卡将于 %s 到期", creditCardExpiry);
周期性时间
LocalDate today = LocalDate.now();
LocalDate birthdayDate = LocalDate.of(2017, 9, 1);
MonthDay birthday = MonthDay.of(birthdayDate.getMonth(), birthdayDate.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(today);
if (birthday.equals(currentMonthDay)) System.out.println("今天是你的生日");
else System.out.println("今天不是你的生日");
获取当前时间 LocalTime
LocalTime now = LocalTime.now();
//now=16:13:44.416679400
System.out.println("now="+now);
String timeWithMillis = now.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"));code>
//timeWithMillis=16:13:44.416
System.out.println("timeWithMillis="+timeWithMillis);
获取当前时间前后的时间
LocalTime now = LocalTime.now();
System.out.println("现在时间是:"+now);code>
LocalTime hourAfter = now.plusHours(2);
System.out.println("两小时之后的时间是:"+hourAfter);
LocalTime hourBefore = now.minusHours(2);
System.out.println("两小时之前的时间是:"+hourBefore);
LocalTime localTime = now.plusMinutes(2);
System.out.println("两分钟之后的时间是:"+localTime);
System.out.println("两分钟之前的时间是:"+now.minusMinutes(2));
System.out.println("两秒之后的时间是:"+now.plusSeconds(2));
System.out.println("两秒之前的时间是:"+now.minusSeconds(2));
System.out.println("两纳秒之后的时间是:"+now.plusNanos(2));
System.out.println("两纳秒之前的时间是:"+now.minusNanos(2));
获取当前日期前后的日期
LocalDate now = LocalDate.now();
//当前日期:2024-05-06
System.out.println("当前日期:"+now);
//一周后的日期:2024-05-13
System.out.println("一周后的日期:"+now.plusWeeks(1));
//一周前的日期:2024-04-29
System.out.println("一周前的日期:"+now.minusWeeks(1));
//一月后的日期:2024-06-06
System.out.println("一月后的日期:"+now.plusMonths(1));
//一月前的日期:2024-04-06
System.out.println("一月前的日期:"+now.minusMonths(1));
//一年后的日期:2025-05-06
System.out.println("一年后的日期:"+now.plusYears(1));
//一年前的日期:2023-05-06
System.out.println("一年前的日期:"+now.minusYears(1));
Clock 时钟类(时间戳)
Clock clock = Clock.systemUTC();
// 获取当前时间戳1714988314792
System.out.println(clock.millis());
Clock clock1 = Clock.systemDefaultZone();
// 获取当前时间戳1714988314808
System.out.println(clock1.millis());
// 也可以1714988314808
System.out.println(System.currentTimeMillis());
Instant timestamp = Instant.now();
//1714988314808
System.out.println(timestamp.toEpochMilli());
判断日期早晚于
LocalDate now = LocalDate.now();
System.out.println("今天是:"+now);//今天是:2024-05-06
LocalDate day = LocalDate.of(2024, 5, 6);
LocalDate yesterday = LocalDate.of(2024, 5, 5);
LocalDate tomorrow = LocalDate.of(2024, 5, 7);
if (now.isAfter(day)) {
//now和day是同一天,所以不会输出
System.out.println("now是day之后的日期");
}
if (now.isBefore(day)) {
//now和day是同一天,所以不会输出
System.out.println("now是day之前的日期");
}
if (now.isAfter(yesterday)) {
//正常输出 now是yesterday之后的日期
System.out.println("now是yesterday之后的日期");
}
if (now.isBefore(tomorrow)) {
//正常输出 now是tomorrow之前的日期
System.out.println("now是tomorrow之前的日期");
}
时区
ZoneId america = ZoneId.of("America/New_York");
LocalDateTime localtDateAndTime = LocalDateTime.now();
//当前本地时间:2024-05-06T16:52:11.234393300
System.out.println("当前本地时间:"+localtDateAndTime);
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america );
//TODO: 这一块输出的时间应该和currentDateTimeInNY一样吧。
//把当前时间转换成纽约时区的时间New_York Time Zone : 2024-05-06T16:52:11.234393300-04:00[America/New_York]
System.out.println("New_York Time Zone : " + dateAndTimeInNewYork);
ZonedDateTime currentDateTimeInNY = ZonedDateTime.now(ZoneId.of("America/New_York"));
//直接获取纽约时区的时间Current Time in New York: 2024-05-06T04:52:11.237388200-04:00[America/New_York]
System.out.println("Current Time in New York: " + currentDateTimeInNY);
闰年判断
LocalDate now = LocalDate.now();
if (now.isLeapYear()) {
// 闰年
System.out.println("闰年");
}else {
System.out.println("平年");
}
LocalDate leapYear = LocalDate.of(2018, 2, 5);
if (leapYear.isLeapYear()) {
System.out.println("闰年");
} else {
// 平年
System.out.println("平年");
}
日期间的天数和月数
public static void main(String[] args) {
LocalDate today = LocalDate.now();
//today = 2024-05-06
System.out.println("today = " + today);
LocalDate java = LocalDate.of(2018, 12, 14);
//参数1是开始时间,参数2是结束时间 五年四个月二十二天
Period result = Period.between(java,today);
//today和java相差了-4月
System.out.println(" java 和 today 相差了 "+ result.getMonths()+"月");
//today和java相差了-23天
System.out.println(" java 和 today 相差了 "+ result.getDays()+"天");
//today和java相差了-5年
System.out.println(" java 和 today 相差了 "+ result.getYears()+"年");
// java 和 today 相差了 1970天
System.out.println(" java 和 today 相差了 "+ daysBetween(java,today)+"天");
// java 和 today 相差了 64月
System.out.println(" java 和 today 相差了 "+ monthsBetween(java,today)+"月");
// java 和 today 相差了 5年
System.out.println(" java 和 today 相差了 "+ yearsBetween(java,today)+"年");
}
public static int daysBetween(LocalDate date1, LocalDate date2) {
return (int) ChronoUnit.DAYS.between(date1, date2);
}
public static int monthsBetween(LocalDate date1, LocalDate date2) {
return (int) ChronoUnit.MONTHS.between(date1, date2);
}
public static int yearsBetween(LocalDate date1, LocalDate date2) {
return (int) ChronoUnit.YEARS.between(date1, date2);
}
解析或格式化日期
String date = "20180205";
LocalDate formatted1 = LocalDate.parse(date, DateTimeFormatter.BASIC_ISO_DATE);
//20180205 格式化后的日期为: 2018-02-05
System.out.println(date+" 格式化后的日期为: "+formatted1);
LocalDate formatted2 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyyMMdd"));
//20180205 格式化后的日期为: 2018-02-05
System.out.println(date+" 格式化后的日期为: "+formatted2);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String format = formatted2.format(dateTimeFormatter);
//20180205 格式化后的日期为: 2018/02/05
System.out.println("20180205 格式化后的日期为: "+format);
日期字符串转换
LocalDateTime date = LocalDateTime.now();
//要是用yyyy/MM/dd 下面字符串转日期的要保持一致
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//日期转字符串
String str = date.format(format1);
//日期转换为字符串:2024-05-06 18:06:29
System.out.println("日期转换为字符串:"+str);
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//字符串转日期
LocalDateTime date2 = LocalDateTime.parse(str,format2);
//字符串转日期:2024-05-06T18:06:29
System.out.println("字符串转日期:"+date2);
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。