LocalDate
package javatest3;
import java.time.LocalDate;
public class TestLocalDate {
public static void main(String[] args) {
//LocalDate:代表本地日期(年、月、日、星期)
//0.获取本地日期对象;
LocalDate ld = LocalDate.now();
System.out.println(ld);
//1.获取日期对象中的信息;
int year = ld.getYear();//年
System.out.println("年:"+year);
int month = ld.getMonthValue();//月(1-12)
System.out.println("月:"+month);
int day = ld.getDayOfMonth();//日
System.out.println("日:"+day);
int dayOfYear = ld.getDayOfYear();//一年中第几天
System.out.println("一年中第几天:"+dayOfYear);
int dayOfWeek = ld.getDayOfWeek().getValue();//星期几
System.out.println("星期几:"+dayOfWeek);
//2.直接修改某个信息:withYear、withMonth、withDayOfMonth、withDayofYear;
LocalDate ld2 = ld.withYear(2003);//返回给一个新的对象,并不会改变原来的值;
System.out.println(ld2);
System.out.println(ld);
//3.把某个信息加多少:plusYears、plusMonths、plusDays、plusWeeks;
LocalDate ld4 = ld.plusYears(2);//返回给一个新的对象,并不会改变原来的值;
System.out.println(ld4);
System.out.println(ld);
//4.把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeks;
LocalDate ld5 = ld.minusYears(2);//返回给一个新的对象,并不会改变原来的值;
System.out.println(ld5);
System.out.println(ld);
//5.获取指定日期的LocalDate对象:public static LocalDate of(int year,int month,int dayOfMonth)
LocalDate ld8 = LocalDate.of(2003, 12, 12);
LocalDate ld9 = LocalDate.of(2003, 12, 12);
//6.判断2个日期对象是否相等,在前还是在后:equals、isBefore、isAfter
System.out.println(ld8.equals(ld9));
System.out.println(ld8.isBefore(ld));
System.out.println(ld8.isAfter(ld));
}
}
LocalTime
package javatest3;
import java.time.LocalTime;
public class TestLocalTime {
public static void main(String[] args) {
// TODO Auto-generated method stub
//LocalTime:代表本地时间(时、分、秒、纳秒)
//0.获取本地时间对象;
LocalTime lt = LocalTime.now();
System.out.println(lt);
//1.获得时间对象中的某个信息;
int hour = lt.getHour();//时
int minute = lt.getMinute();//分
int second = lt.getSecond();//秒
int nano = lt.getNano();//纳秒
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
System.out.println(nano);
//2.修改时间:withHour、withMinute、withSecond、withNano;
LocalTime lt1 = lt.withHour(10);
LocalTime lt2 = lt.withMinute(10);
LocalTime lt3 = lt.withSecond(10);
LocalTime lt4 = lt.withNano(10);
//3.加多少:plusHours、plusMinutes、plusSeconds、plusNanos;
LocalTime lt5 = lt.plusHours(10);
//4.减多少:minuHours、minuMinutes、minuSeconds、minuNanos;
LocalTime lt6 = lt.minusHours(10);
//5.获取指定时间的LocalTime对象;
LocalTime lt7 = LocalTime.of(12, 12, 12);
LocalTime lt8 = LocalTime.of(12, 12, 12);
//6.判断两个时间对象是否相等、在前还是在后:equals、isBefore、isAfter;
System.out.println(lt7.equals(lt8));
System.out.println(lt7.isBefore(lt));
System.out.println(lt7.isAfter(lt));
//LocalDateTime:代表本地日期时间(年、月、日、星期、时、分、秒、纳秒)
}
}
LocalDateTime
package javatest3;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class TestLocalDateTime {
public static void main(String[] args) {
//0.获取本地日期和时间对象;
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
//1.获取日期和时间所有信息;
/* getYear();//年
* getMonthValue();//月(1-12)
* getDayOfMonth();//日
* getDayOfYear();//一年中第几天
* getDayOfWeek().getValue();//星期几
* getHour();//时
* getMinute();//分
* getSecond();//秒
* getNano();//纳秒
*/
//2.修改信息;
//修改日期:withYear、withMonth、withDayOfMonth、withDayofYear;
//修改时间:withHour、withMinute、withSecond、withNano;
//3.加多少;
//plusYears、plusMonths、plusDays、plusWeeks、plusHours、plusMinutes、plusSeconds、plusNanos
//4.减多少;
//minusYears、minusMonths、minusDays、minusWeeks、minuHours、minuMinutes、minuSeconds、minuNanos
//5.获取指定日期和时间的LocalDateTime对象;
LocalDateTime ldt7 = LocalDateTime.of(12, 12, 12, 12, 12, 12);
LocalDateTime ldt8 = LocalDateTime.of(12, 12, 12, 12, 12, 12);
//6.判断两个对象是否相等、在前还是在后:equals、isBefore、isAfter;
//7.可以把LocalDateTime转换成LocalDate和LocalTime
//public LocalDate toLocalDate();
//public LocalTime toLocalTime();
//public static LocalDateTime of(LocalDate date,LocalTime time);
LocalDate ld = ldt.toLocalDate();
LocalTime lt = ldt.toLocalTime();
LocalDateTime ldt2 = LocalDateTime.of(ld, lt);
}
}
Period、Duration
package javatest3;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
public class Test_Period_Duration {
public static void main(String[] args) {
// TODO Auto-generated method stub
//period;用于计算两个LocalDate对象、相差的年数、月数、天数;
LocalDate start = LocalDate.of(2023,8,10);
LocalDate end = LocalDate.of(2023,8,15);
//1.创建period对象,封装两个日期对象;
Period period = Period.between(start, end);
//2.通过period对象获取两个日期对象相差的信息;
System.out.println(period.getYears());//间隔多少年;
System.out.println(period.getMonths());//间隔多少月;
System.out.println(period.getDays());//间隔多少天;
//Duration;用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数、支持LocalTime、LocalDateTime、Instant;
LocalDateTime start1 = LocalDateTime.of(2023, 11,11,11,11,11);
LocalDateTime end1 = LocalDateTime.of(2023, 11,11,11,11,12);
//1.得到duration对象;
Duration duration = Duration.between(start1, end1);
//获取两个时间对象间隔的信息;
System.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
package javatest3;
import java.time.Instant;
public class TestInstant {
public static void main(String[] args) {
//1.创建Instant对象;
Instant now = Instant.now();//不可变对象;
System.out.println(now);
//2.获取总秒数;
long second = now.getEpochSecond();
System.out.println(second);
//3.不够1秒的纳秒数;
int nano = now.getNano();
System.out.println(nano);
//4.加上
Instant instant = now.plusNanos(111);
//作用:可以用来记录代码的执行时间,或者用于记录用户操作某个事件的时间点;
}
}
?
?