2024-07-24
你必须要变强,才可以守护身边的人! --- 《坎公骑冠剑》 · 飞小RAN
YearMonth and LocalDate
在Java中,可以使用YearMonth类将其转换为LocalDate。YearMonth表示特定年份和月份的组合,而LocalDate则表示具体的日期,包括年、月和日。因此,在转换时,需要指定一个具体的日。例如,可以选择月份的第一天或者最后一天。
下面是一些示例代码,展示了如何将YearMonth转换为LocalDate:
将YearMonth转换为某个月的第一天
import java.time.YearMonth;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
YearMonth yearMonth = YearMonth.of(2023, 7); // 2023年7月
LocalDate firstDayOfMonth = yearMonth.atDay(1); // 该月的第一天
System.out.println("YearMonth: " + yearMonth);
System.out.println("First day of month: " + firstDayOfMonth);
}
}
将YearMonth转换为某个月的最后一天
import java.time.YearMonth;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
YearMonth yearMonth = YearMonth.of(2023, 7); // 2023年7月
LocalDate lastDayOfMonth = yearMonth.atEndOfMonth(); // 该月的最后一天
System.out.println("YearMonth: " + yearMonth);
System.out.println("Last day of month: " + lastDayOfMonth);
}
}
通过以上方法,可以根据需要将YearMonth转换为LocalDate,并且可以选择月份中的具体日期。
在Java中,可以通过LocalDate类的getYear()和getMonth()方法,使用YearMonth.of(int year, Month month)将LocalDate转换为YearMonth。以下是具体的示例代码:
import java.time.LocalDate;
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2023, 7, 23); // 2023年7月23 日
// 将LocalDate转换为YearMonth
YearMonth yearMonth = YearMonth.of(localDate.getYear(), localDate.getMonth());
System.out.println("LocalDate: " + localDate);
System.out.println("YearMonth: " + yearMonth);
}
}
代码解析
LocalDate.of(2023, 7, 23):创建一个特定日期的LocalDate对象。localDate.getYear():获取LocalDate对象的年份。localDate.getMonth():获取LocalDate对象的月份。YearMonth.of(localDate.getYear(), localDate.getMonth()):使用年份和月份创建YearMonth对象。
通过这种方式,可以将LocalDate转换为YearMonth,并且只保留日期中的年和月信息。
在Java中,YearMonth类实现了Comparable<YearMonth>接口,因此可以直接使用compareTo方法来比较两个YearMonth对象的大小。此外,还可以使用isBefore和isAfter方法来进行比较。
以下是一些示例代码,展示了如何使用这些方法比较两个YearMonth对象:
使用compareTo方法
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
YearMonth ym1 = YearMonth.of(2023, 7); // 2023年7月
YearMonth ym2 = YearMonth.of(2024, 3); // 2024年3月
int comparison = ym1.compareTo(ym2);
if (comparison < 0) {
System.out.println(ym1 + " is before " + ym2);
} else if (comparison > 0) {
System.out.println(ym1 + " is after " + ym2);
} else {
System.out.println(ym1 + " is equal to " + ym2);
}
}
}
使用isBefore方法
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
YearMonth ym1 = YearMonth.of(2023, 7); // 2023年7月
YearMonth ym2 = YearMonth.of(2024, 3); // 2024年3月
if (ym1.isBefore(ym2)) {
System.out.println(ym1 + " is before " + ym2);
} else if (ym1.isAfter(ym2)) {
System.out.println(ym1 + " is after " + ym2);
} else {
System.out.println(ym1 + " is equal to " + ym2);
}
}
}
使用isAfter方法
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
YearMonth ym1 = YearMonth.of(2023, 7); // 2023年7月
YearMonth ym2 = YearMonth.of(2024, 3); // 2024年3月
if (ym1.isAfter(ym2)) {
System.out.println(ym1 + " is after " + ym2);
} else if (ym1.isBefore(ym2)) {
System.out.println(ym1 + " is before " + ym2);
} else {
System.out.println(ym1 + " is equal to " + ym2);
}
}
}
通过以上方法,可以方便地比较两个YearMonth对象的大小,从而确定它们之间的先后顺序。
在Java中,YearMonth类提供了便捷的方法来计算两个YearMonth对象之间的差值。可以使用until方法来计算两个YearMonth之间的年数和月数差值。until方法返回一个Period对象,表示这两个YearMonth对象之间的时间差。
以下是一些示例代码,展示了如何计算两个YearMonth对象之间的差值:
计算两个YearMonth对象之间的差值(以年和月为单位)
import java.time.YearMonth;
import java.time.Period;
public class Main {
public static void main(String[] args) {
YearMonth start = YearMonth.of(2023, 7); // 2023年7月
YearMonth end = YearMonth.of(2024, 3); // 2024年3月
// 计算两个YearMonth之间的差值
Period period = start.until(end);
int years = period.getYears();
int months = period.getMonths();
System.out.println("差值: " + years + " 年 " + months + " 月");
}
}
计算两个YearMonth对象之间的总月数差值
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
YearMonth start = YearMonth.of(2023, 7); // 2023年7月
YearMonth end = YearMonth.of(2024, 3); // 2024年3月
// 计算两个YearMonth之间的总月数差值
int totalMonths = start.until(end, java.time.temporal.ChronoUnit.MONTHS);
System.out.println("总月数差值: " + totalMonths + " 月");
}
}
代码解析
start.until(end):计算两个YearMonth之间的差值,并返回一个Period对象。period.getYears():获取年数差值。period.getMonths():获取月数差值。start.until(end, java.time.temporal.ChronoUnit.MONTHS):计算两个YearMonth之间的总月数差值。
通过上述方法,可以轻松地计算两个YearMonth对象之间的年数和月数差值,以及总月数差值。
带有负载因子的集合
今天发现了我对于集合理解的一个误区
在Java集合框架中,负载因子(load factor)是一个影响集合扩容行为的重要参数。负载因子主要应用于基于哈希表的数据结构。以下是一些主要使用负载因子的集合:
1. HashMap
- 默认负载因子:0.75
- 用途:用于控制哈希表何时需要调整大小。当元素数量超过容量与负载因子的乘积时,触发扩容。
Map<Integer, String> map = new HashMap<>();