【Java】【常⽤类】LocalDateTime当前⽇期时间类相关LocalDate主要的三个API类:
java.time.LocalDate;
java.time.LocalDateTime;
java.time.LocalTime;
LocatDate对象获取:
@Test
void contextLoads() {
// 获取⽅式
LocalDate localDate = w();
System.out.println(localDate);
LocalDate localDate1 = LocalDate.of(2020, 5, 23);
System.out.println(localDate1);
// 2020-09-28
// 2020-05-23
int year = Year(); // 取年
int monthValue = MonthValue(); // 取⽉
Month month = Month(); // 取⽉的枚举对象
int monthValue1 = Value(); // 具体值可以通过该枚举对象获取
// 除此之外Month还有⼀些获取其他信息的⽅法
int maxDaysLength = month.maxLength(); // 该⽉的最⼤天数
int minDaysLength = month.minLength(); // 该⽉的最⼩天数
int dayOfMonth = DayOfMonth(); // 按当⽉取天数
int dayOfYear = DayOfYear(); // 按本年取天数
DayOfWeek dayOfWeek = DayOfWeek(); // 按本周取天数?
// 和⽉枚举对象⼀样,这⾥也是⼀个周枚举对象
int value = Value();
System.out.println(dayOfWeek); // 打印为 MONDAY ....
}
private static void cryptTest() {
final String PASSWORD = "123456";
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encode = de(PASSWORD);
System.out.println(encode);
boolean matches = passwordEncoder.matches(PASSWORD, encode);
System.out.println(matches);
}
LocalDateTime & LocalTime:
@Test
void contextLoads() {
LocalDateTime localDateTime1 = w();
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 2, 2, 13, 24, 52);
System.out.println(localDateTime1); // 2020-09-28T16:01:48.248
System.out.println(localDateTime2); // 2020-02-02T13:24:52
LocalTime localTime1 = w();
LocalTime localTime2 = LocalTime.of(20, 2, 2);
System.out.println(localTime1); // 16:03:41.330
System.out.println(localTime2); // 20:02:02
int hour = Hour();
int minute = Minute();
int second = Second();
int nano = Nano();
System.out.println("时 -> " + hour);        // 时 -> 16
System.out.println("分 -> " + minute);      // 分 -> 6
System.out.println("秒 -> " + second);      // 秒 -> 26
System.out.println("Nano -> " + nano);      // Nano -> 206000000
}
GET⽅法
SET⽅法
增加时间
减少时间
public class DateTest {
public static void main(String[] args) {
LocalDate now1 = w();
LocalTime now2 = w();
LocalDateTime now3 = w();
System.out.println("w() -> " + now1);
System.out.println("w() -> " + now2);
System.out.println("w() -> " + now3);
/*
*/
LocalDateTime localDateTime = w();
System.out.println("DayOfWeek() -> " + DayOfWeek() );    // 按周算天        System.out.println("DayOfMonth() -> " + DayOfMonth() );  // 按⽉算天        System.out.println("DayOfYear() -> " + DayOfYear() );    // 按年算天/*
*/
System.out.println( Second()      );// 取秒
System.out.println( Minute()      );// 取分
System.out.println( Hour()        );// 取时
System.out.println( Month()        );// 取⽉英⽂⼤写
System.out.println( MonthValue()  );// 取⽉数值表⽰
System.out.println( Year()        );// 取年
// set ×  with √
}
}
instant 瞬时时间,精度达到纳秒级
public class DateTest {
public static void main(String[] args) {
Instant instant = w();
System.out.println(instant);
//  2020-04-19T13:47:58.712Z 本初⼦午线的标准时间
// 我们是东⼋时区,添加8⼩时的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
/
/ 从实例获取毫秒数时间戳
long epochMilli = EpochMilli();
System.out.println(epochMilli);
// 通过时间戳注⼊产⽣instant实例
java时间日期格式转换
Instant epochMilli1 = Instant.ofEpochMilli(epochMilli);
System.out.println(epochMilli1);
}
}
DateTimeFormatter
public class DateTest {
public static void main(String[] args) {
/
/ 预定义的标准格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 事件对象
LocalDateTime now = w();
// 转换格式⽇期 -> 字符串格式
String format = dateTimeFormatter.format(now);
// 格式
System.out.println( now );
System.out.println( format );
// ⽇期转字符串格式
TemporalAccessor parse = dateTimeFormatter.parse("2020-03-19T22:09:46.345");
System.out.println(parse);
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
// ofLocalizedDateTime();
// ofLocalizedDate(); 按Date就⽀持FULL全格式
// ofPattern("⾃定义格式"); "yyyy-MM-dd hh:mm:ss"
// FormatStyle.FULL    2020年4⽉19⽇星期⽇
// FormatStyle.LONG    2020年4⽉19⽇下午10时15分01秒
// FormatStyle.MEDIUM  2020-4-19 22:14:28
// FormatStyle.SHORT    20-4-19 下午10:16
String format1 = dateTimeFormatter1.format(now);
System.out.println(now);
System.out.println(format1);
TemporalAccessor parse1 = dateTimeFormatter1.parse(format1);        System.out.println(parse1);
}
}
案例,制作⽇历:
@Test
void contextLoads() {
localDateCalendar();
}
private static void localDateCalendar() {
LocalDate now = w();
int year = Year();
int month = Month().getValue();
int dayOfMonth = DayOfMonth();
// 设置这个⽉的第⼀天
now = now.minusDays(dayOfMonth - 1);
// 到这⼀天为周⼏
int value = DayOfWeek().getValue();
// 开始渲染控制台输出样式
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for (int i = 1; i < value; i++) System.out.print("  ");
while (MonthValue() == month) {
System.out.printf("%3d", DayOfMonth());
if (DayOfMonth() == dayOfMonth) System.out.print("*");
else System.out.print(" ");
now = now.plusDays(1);
if (DayOfWeek().getValue() == 1) System.out.println();
}
if (DayOfWeek().getValue() != 1) System.out.println();
}
打印结果:
Mon Tue Wed Thu Fri Sat Sun
1  2  3  4  5  6
7  8  9  10  11  12  13
14  15  16  17  18  19  20
21  22  23  24  25  26  27
28* 29  30
其他API
ZoneId
ZoneDateTime
Clock
Duration
Period
TemporalAdjuster
TemporalAdjusters

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。