package com.ydlclass.date;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalTimeTest {//强调本地的时间API
//由于相似,可以直接查阅文档即可正常使用了
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println(now);//打印出来的只有时间,这个和localDate只强调日期是十分不同的,但是用法却十分相似
LocalDateTime now1 = LocalDateTime.now();
System.out.println(now1);
}
}
package com.ydlclass.date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterTest {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");//船舰了一个格式化器
//当然这个格式化器可以用匿名内部类的方式实现
String format = now.format(dateTimeFormatter);//将当前的locatedatetime日期时间按照我们想要的格式输出,其中格式化器需要我们自己去出啊
//传入
//我们可以使用localdatetime的方法来将时间转换为字符串
//LocalDateTime parse = LocalDateTime.parse("2022年03月26日");//使用这个方法可以将字符串转化为时间,并且这个时间的格式是有要求的。没有指定时分秒
LocalDate parse = LocalDate.parse("2022年03月26日",dateTimeFormatter);//如果不加上第二个参数时,那么默认的格式会存在问题,
System.out.println(parse);
System.out.println(parse);
System.out.println(format);
}
}