LocalDateTime获取当地日期和时间
import java.time.LocalDateTime;
/*LocalDateTime.now() 获取当前时间*/
public class LocalDateTimeDemo {public static void main(String[] args) {LocalDateTime time1 = LocalDateTime.now();System.out.println(time1);//2024-06-01T13:20:41.336609500System.out.println(time1.getDayOfYear());System.out.println(time1.getDayOfMonth());System.out.println(time1.getHour());System.out.println(time1.getMinute());System.out.println(time1.getSecond());System.out.println(time1.getNano());//纳秒System.out.println(time1.getYear());System.out.println(time1.getMonthValue());System.out.println(time1.getDayOfWeek());//SATURDAY}
}
DateTimeFormatter对日期和时间自定义想要的格式
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;public class DateTimeFormatterDemo {public static void main(String[] args) {/*static DateTimeFormatter ofPattern(格式) 获取格式对象String format(时间对象) 按照指定方式格式化*/// 获取格式对象ZonedDateTime time = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));System.out.println(time);//2024-05-31T18:15:21.535714300+02:00[Europe/Berlin]// 自定义格式DateTimeFormatter form= DateTimeFormatter.ofPattern("yyyy年MM月dd日hh时mm分ss秒 星期e");System.out.println(form.format(time));//2024年05月31日06时20分54秒 星期5}
}
两个日期之间各种单位的差值
ChronoUnit获取两个日期和时间各种换算单位之间的总时差
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class ToolsChronoUnitDemo {public static void main(String[] args) {// 当前年月日LocalDateTime today = LocalDateTime.now();System.out.println(today);//2024-06-01T14:06:52.387788300// 出生年月日LocalDateTime BornDate = LocalDateTime.of(2004, 6, 14,0,0,0,0);System.out.println(BornDate);//2004-06-14T00:00System.out.println("相差的总年数:" + ChronoUnit.YEARS.between(BornDate,today));System.out.println("相差的总月数:" + ChronoUnit.MONTHS.between(BornDate,today));System.out.println("相差的总周数:" + ChronoUnit.WEEKS.between(BornDate,today));System.out.println("相差的总天数:" + ChronoUnit.DAYS.between(BornDate,today));System.out.println("相差的总时数:" + ChronoUnit.HOURS.between(BornDate,today));System.out.println("相差的总分数:" + ChronoUnit.MINUTES.between(BornDate,today));System.out.println("相差的总秒数:" + ChronoUnit.SECONDS.between(BornDate,today));System.out.println("相差的总毫秒数:" + ChronoUnit.MILLIS.between(BornDate,today));System.out.println("相差的总微秒数:" + ChronoUnit.MICROS.between(BornDate,today));System.out.println("相差的总纳秒数:" + ChronoUnit.NANOS.between(BornDate,today));System.out.println("相差的总半天数:" + ChronoUnit.HALF_DAYS.between(BornDate,today));System.out.println("相差的总十年数:" + ChronoUnit.DECADES.between(BornDate,today));System.out.println("相差的总世纪(百年)数:" + ChronoUnit.CENTURIES.between(BornDate,today));System.out.println("相差的总千年数:" + ChronoUnit.MILLENNIA.between(BornDate,today));System.out.println("相差的总纪元数:" + ChronoUnit.ERAS.between(BornDate,today));}
}
Period获取两个日期一年内的月份和天数之间的间隔
import java.time.LocalDate;
import java.time.Period;
public class ToolsDatePeriodDemo {public static void main(String[] args) {// 当前年月日LocalDate today = LocalDate.now();System.out.println(today);// 出生年月日LocalDate BornDate = LocalDate.of(2004, 6, 14);Period period = Period.between(BornDate, today);// 出生时间到今天的间隔时间System.out.println(period);//P19Y11M18DSystem.out.println(period.getYears());// 相差的年数System.out.println(period.getMonths());// 相差的月数System.out.println(period.getDays());// 相差的天数System.out.println(period.toTotalMonths());// 相差的总月数}
}
Duration获取两个日期和时间之间相差的总天数和各种单位的总时间数
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;public class ToolsDurationDemo {public static void main(String[] args) {// 本地日期时间对象。LocalDateTime today = LocalDateTime.now();System.out.println(today);// 出生的日期时间对象LocalDateTime BornDate = LocalDateTime.of(2004, 6, 14, 0, 0, 0);System.out.println(BornDate);Duration duration = Duration.between(BornDate, today);// 出生时间到今天的间隔时间System.out.println(duration);//PT175021H57M48.8411209SSystem.out.println(duration.toDays());// 相差的总天数System.out.println(duration.toHours());// 相差的总小时数System.out.println(duration.toMinutes());// 相差的总分钟数System.out.println(duration.toSeconds());// 相差的总秒数System.out.println(duration.toMillis());// 相差的总毫秒数System.out.println(duration.toNanos());// 相差总纳秒数}
}
获取时间和时区,对时间的值进行各种操作
Instant对时间进行增加,减少和差异判断
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class InstentDemo {public static void main(String[] args) {/*static Instant now() 获取当前时间的Instant对象(标准时间)static Instant ofXxxx(long epochMilli) 根据(秒/毫秒/纳秒)获取Instant对象ZonedDateTime atZone(ZoneIdzone) 指定时区boolean isxxx(Instant otherInstant) 判断时间差异的方法Instant minusXxx(long millisToSubtract) 减少时间的方法,需要放到一个新对象中Instant plusXxx(long millisToSubtract) 增加时间的方法,需要放到一个新对象中*/// 1.获取当前时间的对象Instant now = Instant.now();System.out.println(now);//标准时间,比北京时间少8个小时// 2.根据(秒/毫秒/纳秒)获取Instant对象Instant now2 = Instant.ofEpochSecond(67L);System.out.println(now2);//1970-01-01T00:01:07ZInstant now4 = Instant.ofEpochMilli(6000L);// 6000毫秒System.out.println(now4);//1970-01-01T00:00:06ZInstant now3 = Instant.ofEpochSecond(67L,1000000000L);System.out.println(now3);//1970-01-01T00:01:08Z 67秒加 (10亿纳秒=1秒)// 3.指定时区的时间ZonedDateTime localTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));System.out.println(localTime);// 上海时区// 4.判断时间差异boolean result1 = now2.isBefore(now3);//now2的时间在now3的前面System.out.println(result1);//trueboolean result2 = now2.isAfter(now3);//now2的时间在now3的后面System.out.println(result2);//false// 5.减少时间Instant result3 = now2.minusSeconds(7);// 减少了7秒System.out.println(result3);//1970-01-01T00:01:00Z// 6.增加时间Instant result4 = now2.plusSeconds(53);//增加了53秒System.out.println(result4);//1970-01-01T00:02:00Z}
}
ZonedDateTime对时间的值进行修改和增加,减少
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneDateTimeDemo {public static void main(String[] args) {/*static ZonedDateTime now() 获取当前时间(带当地的时区)static ZonedDateTime ofXxxx(。。。) 获取自己输入的时间的对象ZonedDateTime withXxx(时间) 修改时间的各种方法ZonedDateTime minusXxx(时间) 减少时间的各种方法ZonedDateTime plusXxx(时间) 增加时间的各种方法*/// 1.获取当前时间(带当地的时区)ZonedDateTime time1 = ZonedDateTime.now();System.out.println(time1);//2024-05-31T23:52:57.247911600+08:00[Asia/Shanghai]// 2.获取指定时间的对象(年月日时分秒纳秒的方式指定,带时区)ZonedDateTime time2 = ZonedDateTime.of(2024,5,31,23,57,11,11, ZoneId.of("Asia/Shanghai"));System.out.println(time2);//2024-05-31T23:57:11.000000011+08:00[Asia/Shanghai]// 3.修改时间的各种方法 withXxxxZonedDateTime time3 = time2.withHour(6);System.out.println(time3);//2024-05-31T06:57:11.000000011+08:00[Asia/Shanghai]ZonedDateTime time4 = time2.withYear(2025);System.out.println(time4);//2025-05-31T23:57:11.000000011+08:00[Asia/Shanghai]// 4. 减少时间的各种方法 minusXxxxZonedDateTime time5 = time2.minusDays(6);System.out.println(time5);//2024-05-25T23:57:11.000000011+08:00[Asia/Shanghai]ZonedDateTime time6 = time2.minusHours(6);System.out.println(time6);//2024-05-31T17:57:11.000000011+08:00[Asia/Shanghai]// 5.增加时间的各种方法 plusXxxxZonedDateTime time7 = time2.plusDays(6);System.out.println(time7);//2024-06-06T23:57:11.000000011+08:00[Asia/Shanghai]ZonedDateTime time8 = time2.plusMonths(6);System.out.println(time8);//2024-06-06T23:57:11.000000011+08:00[Asia/Shanghai]}
}
ZoneId获取Java支持的所有时区的地区名称
import java.util.Set;
import java.time.ZoneId;
public class ZoneIdDemo {public static void main(String[] args) {/*static set<String>getAvailableZoneIds() 获取Java中支持的所有时区static ZoneId systemDefault() 获取系统默认时区static ZoneId of(String zoneId) 获取一个指定时区*/// 1.获取所有的时区名称Set<String> zoneIds = ZoneId.getAvailableZoneIds();System.out.println(zoneIds.size());//603System.out.println(zoneIds);// 2.获取系统默认时间ZoneId id = ZoneId.systemDefault();System.out.println(id);//Asia/Shanghai// 3.获取指定的时区ZoneId id2 = ZoneId.of("Europe/Berlin");System.out.println(id2);//Europe/Berlin}
}