日期和时间
//当前时间
Date date = new Date();
System.out.println(date);//Sat Dec 02 21:14:10 CST 2023
//把毫秒值转为Date对象
Date date1 = new Date(1000L);
System.out.println(date1);//Thu Jan 01 08:00:01 CST 1970
//返回从1970年1月1日 00:00:00 到date对象的毫秒数
long time = new Date().getTime();
System.out.println(time);//1701571872876
Date date = new Date();
date.setTime(0L);
System.out.println(date);//Thu Jan 01 08:00:00 CST 1970
把日期格式化
SimpleDateFormat()
默认格式
SimpleDateFormat(String pattern)
指定格式
String format = new SimpleDateFormat().format(new Date());
System.out.println(format);//23-12-3 上午11:47
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(format);//2023-12-03 11:50:15
Date date = new SimpleDateFormat().parse("23-12-3 上午11:47");
System.out.println(date);//Sun Dec 03 11:47:00 CST 2023
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2023-12-03 11:50:15");
System.out.println(date);//Sun Dec 03 11:50:15 CST 2023
此刻时间对应的日历,可以单独获取、修改日历中的年、月、日、时、分、秒等。
注意:
Calendar是抽象类
直接子类:GregorianCalendar
//多态,Calendar calendar是抽象类,calendar.getInstace()是子类对象
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);//java.util.GregorianCalendar[time=1701577676959,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=29,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2023,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=2,DAY_OF_MONTH=3,DAY_OF_YEAR=337,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=0,HOUR_OF_DAY=12,MINUTE=27,SECOND=56,MILLISECOND=959,ZONE_OFFSET=28800000,DST_OFFSET=0]
注意:
Month的范围:[0,11],记得做+1操作。
星期的顺序:星期日开始,因此需要提前设计一个数组[’ ',‘日’,‘一’,‘二’,‘三’,‘四’,‘五’,‘六’],‘日’是索引1。
int i = Calendar.getInstance().get(Calendar.YEAR);
System.out.println(i);//2023
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2024);
System.out.println(calendar.get(Calendar.YEAR));//2024
Calendar calendar = Calendar.getInstance();
calendar.set(2008,7,8);
System.out.println(""+calendar.get(Calendar.YEAR)+(calendar.get(Calendar.MONTH)+1)+calendar.get(Calendar.DAY_OF_MONTH));//200888
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 1);
System.out.println(calendar.get(Calendar.YEAR));//2024
日历类
LocalDate:年、月、日、星期
LocalTime:时、分、秒、纳秒
LocalDateTime:年、月、日、时、分、秒、星期、纳秒
时间类
Instant:时间戳/时间线
ZoneId:时区
ZonedDateTime:带时区的时间
日期格式化类
DateTimeFormatter:用于时间的格式化和解析
工具类
Period:时间间隔(年、月、日)
Duration:时间间隔(时、分、秒、纳秒)
ChronoUnit:时间间隔(所有单位)
LocalDateTime now = LocalDateTime.now();
System.out.println(now);//2023-12-03T14:40:10.491
LocalDate of = LocalDate.of(2023, 12, 3);
LocalTime of1 = LocalTime.of(15, 0);
LocalDateTime of2 = LocalDateTime.of(of, of1);
LocalDateTime of3 = LocalDateTime.of(2023, 12, 3, 15, 0, 10);
System.out.println(of);//2023-12-03
System.out.println(of1);//15:00
System.out.println(of2);//2023-12-03T15:00
System.out.println(of3);//2023-12-03T15:00:10
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
System.out.println(localDate);//2023-12-03
System.out.println(localTime);//15:16:04.801
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
Month month = now.getMonth();
int monthValue = now.getMonthValue();
int value = now.getMonth().getValue();
int dayOfMonth = now.getDayOfMonth();
System.out.println(year);//2023
System.out.println(month);//DECEMBER
System.out.println(monthValue);//12
System.out.println(value);//12
System.out.println(dayOfMonth);//3
//获取星期
DayOfWeek dayOfWeek = now.getDayOfWeek();
System.out.println(dayOfWeek);//SUNDAY
int value1 = dayOfWeek.getValue();
System.out.println(value1);//7
注意:获取月份的三种方式,获取星期
static DateTimeFormatter ofPattern(格式)
String format(时间对象)
String format = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss").format(LocalDateTime.now());
System.out.println(format);//2023/12/4 10:59:00
String format = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now());
System.out.println(format);//2023/12/04 10:59:00
LocalDate.parse(“解析字符串”, 格式化对象)
LocalTime.parse(“解析字符串”, 格式化对象)
LocalDateTime.parse(“解析字符串”, 格式化对象)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2023/12/04 10:26:26", formatter);
System.out.println(parse);
注意:在解析时,M和d,只能写1个,不然有可能报错!因为dd解析不出4,只能解析04。