public class StringDate {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
int yyyy = today.getYear();
int dd = today.lengthOfMonth();
System.out.println("当年:" + yyyy);
System.out.println("当前月份有:" + dd + "天");
String StringDate_10 = today.toString();
System.out.println("当前的日期为:" + StringDate_10);
LocalDate endOfFeb = LocalDate.parse("2020-02-28");
System.out.println("10位字符串转换时间为:" + endOfFeb.toString());
System.out.println("当前的毫秒为:" + System.currentTimeMillis());
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("本月第1天日期为:" + firstDayOfThisMonth);
LocalDate secondDayOfThisMonth = today.withDayOfMonth(4);
System.out.println("本月第4天日期为:" + secondDayOfThisMonth);
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("本月第最后日期为:" + lastDayOfThisMonth);
LocalDate firstMondayOf = LocalDate.parse("2020-05-02").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println("获取2020年5月第一个星期一为:" + firstMondayOf);
/**
* 输出今天的周几
*/
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar calendar = Calendar.getInstance();
System.out.println(weekDays[calendar.get(Calendar.DAY_OF_WEEK) - 1]);
}
}
运行结果:
/**
* 判断一个时间是否在,某个时间段范围内
* isBefore:<
* isAfter:>
*/
LocalDateTime time = LocalDateTime.of(2022, 3, 28, 19, 45, 33);
System.out.println("time = " + time);
LocalDateTime time10 = time.plusDays(10);
System.out.println("time1 = " + time10);
LocalDateTime nowTime = LocalDateTime.now();
System.out.println("nowTime = " + nowTime);
if (nowTime.isAfter(time) && nowTime.isBefore(time10)) {
System.out.println(true);
} else {
System.out.println(false);
}