Java日期时间类:LocalDate、LocalTime、LocalDateTime
方法 | 描述 |
---|---|
now() / now(ZoneId zone) |
静态方法,根据当前时间创建对象/指定时区的对象 |
of(xx,xx,xx,xx,xx,xxx) |
静态方法,根据指定日期/时间创建对象 |
getDayOfMonth()/getDayOfYear() | 获得月份天数(1-31) /获得年份天数(1-366) |
getDayOfWeek() | 获得星期几(返回一个 DayOfWeek 枚举值) |
getMonth() | 获得月份, 返回一个 Month 枚举值 |
getMonthValue() / getYear() | 获得月份(1-12) /获得年份 |
getHours()/getMinute()/getSecond() | 获得当前对象对应的小时、分钟、秒 |
withDayOfMonth()/withDayOfYear()/withMonth()/withYear() | 将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象 |
with(TemporalAdjuster t) | 将当前日期时间设置为校对器指定的日期时间 |
plusDays(), plusWeeks(), plusMonths(), plusYears(),plusHours() | 向当前对象添加几天、几周、几个月、几年、几小时 |
minusMonths() / minusWeeks()/minusDays()/minusYears()/minusHours() | 从当前对象减去几月、几周、几天、几年、几小时 |
plus(TemporalAmount t)/minus(TemporalAmount t) | 添加或减少一个 Duration 或 Period |
isBefore()/isAfter() | 比较两个 LocalDate |
isLeapYear() | 判断是否是闰年(在LocalDate类中声明) |
format(DateTimeFormatter t) | 格式化本地日期、时间,返回一个字符串 |
parse(Charsequence text) | 将指定格式的字符串解析为日期、时间 |
Demo
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class LocalDateDemo {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now);
//字符串转时间
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format = now.format(dtf);
System.out.println(format);
String format1 = dtf.format(now);
System.out.println(format1);
//时间转字符串
LocalDateTime ld = LocalDateTime.parse("2022-01-29T13:20:30");
System.out.println(ld.getDayOfMonth());
System.out.println(ld.getHour());
LocalDateTime ldNew = ld.withDayOfMonth(1);
System.out.println(ldNew);
LocalDateTime ldNew2 = ld.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(ldNew2);
LocalDateTime ldNew3 = ld.plusDays(3);
System.out.println(ldNew3);
System.out.println(ldNew3.isAfter(ld));
}
}
练习:打印日历
import java.text.ParseException;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Cal {
public static void main(String ... args) throws ParseException {
String date_str = "2024-02-28";
getMyCalendar(date_str);
}
private static void getMyCalendar(String date_str) throws ParseException {
LocalDate localDate = LocalDate.parse(date_str);
//获取给定的时间是几号
System.out.println(localDate);
int day = localDate.getDayOfMonth();
//获取该月1号是本周第几天
int firstDayOfWeek = localDate.with(TemporalAdjusters.firstDayOfMonth()).getDayOfWeek().getValue();
System.out.println(firstDayOfWeek);
System.out.println(localDate);
//获取该月的最后一天是几号
int lastDay = localDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
//每个月多需要6行7列即可显示完整
int [] days = new int[6*7];
//为数组填充值
for(int i=1 ; i <= lastDay ; i++){
days[firstDayOfWeek-1] = i;
firstDayOfWeek++;
}
//打印日历
System.out.println("一\t二\t三\t四\t五\t六\t日");
for(int i = 0 ; i < days.length ; i++){
if(days[i]!=0){
if(days[i]==day){
System.out.print("*");
}
System.out.print(days[i]);
}
System.out.print("\t");
if((i+1)%7==0){
System.out.println();
}
}
}
}