一、介绍
LocalDate:是一个不可变的日期时间对象,代表一个日期,通常被视为年-月-日;此类是不可变的并且是线程安全的。
LocalTime:是一个不可变的日期时间对象,代表一个时间,通常被视为时分秒。时间以纳秒精度表示;此类是不可变的并且是线程安全的。
LocalDateTime:是一个不变的日期时间对象,代表一个日期时间,通常被视为年-月-日-时-分-秒。也可以访问其他日期和时间字段,例如,一年中的某天,一周中的某天和一周中的某周。时间以纳秒精度表示;此类是不可变的并且是线程安全的。
二、具体例子如下:
1.获取当前时间
public static void main(String[] args) { //获取当前时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("本地当前日期时间:"+localDateTime); System.out.println(); //设置时区为美国纽约 System.out.println("美国当前日期时间:"+LocalDateTime.now(ZoneId.of("America/New_York"))); System.out.println();}
2.获取当前时间毫秒数/时间戳
public static void main(String[] args) { //获取当前时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("本地当前日期时间:"+localDateTime); long millisecond = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("获取时间戳:"+millisecond/1000); System.out.println("获取毫秒数:"+millisecond); System.out.println(); }
3.获取年,月,日或者星期几
public static void main(String[] args) { //获取当前时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("本地当前日期时间:"+localDateTime); //获取年、月、日、星期几 int year = localDateTime.getYear(); System.out.println("获取年份(第一种方式):"+year); year = localDateTime.get(ChronoField.YEAR); System.out.println("获取年份(第二种方式):"+year); System.out.println(); int month = localDateTime.getMonthValue(); System.out.println("获取月份(第一种方式):"+month); month = localDateTime.getMonth().getValue(); System.out.println("获取月份(第二种方式):"+month); month = localDateTime.get(ChronoField.MONTH_OF_YEAR); System.out.println("获取月份(第三种方式):"+month); System.out.println(); int dayOfWeek = localDateTime.getDayOfWeek().getValue(); System.out.println("获取星期几(第一种方式):"+dayOfWeek); dayOfWeek = localDateTime.get(ChronoField.DAY_OF_WEEK); System.out.println("获取星期几(第二种方式):"+dayOfWeek); System.out.println(); int dayOfMonth = localDateTime.getDayOfMonth(); System.out.println("获取本月当日(第一种方式):"+dayOfMonth); dayOfMonth = localDateTime.get(ChronoField.DAY_OF_MONTH); System.out.println("获取本月当日(第二种方式):"+dayOfMonth); System.out.println(); int dayOfYear = localDateTime.getDayOfYear(); System.out.println("获取本年当日(第一种方式):"+dayOfYear); dayOfYear = localDateTime.get(ChronoField.DAY_OF_YEAR); System.out.println("获取本年当日(第二种方式):"+dayOfYear); }
4.日期时间与字符串相互转换
private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; private static final String YYYYMMDD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss"; public static void main(String[] args) { //获取当前时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("本地当前日期时间:"+localDateTime); //日期与字符串相互转换 //日期转字符串 String dateTimeStr = localDateTime.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS)); System.out.println("日期时间格式化成字符串后:"+dateTimeStr); //字符串转日期 String convertTime = "2020/12/12 12:12:12"; LocalDateTime convertLocalDateTime = LocalDateTime.parse(convertTime,DateTimeFormatter.ofPattern(YYYYMMDD_HH_MM_SS)); System.out.println("由字符串转换成的日期时间为:"+convertLocalDateTime); }
5.Date与LocalDateTime相互转换
private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; private static final String YYYYMMDD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss"; public static void main(String[] args) { //获取当前时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("本地当前日期时间:"+localDateTime); //Date转换成LocalDateTime Date date = new Date(); LocalDateTime date2LocalDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); System.out.println("Date转换成LocalDateTime后>>>>"+date2LocalDateTime); //LocalDateTime转换成Date Date localDateTime2Date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); System.out.println("LocalDateTime转换成Date后>>>>"+localDateTime2Date); System.out.println("格式化一下>>>>"+FastDateFormat.getInstance(YYYY_MM_DD_HH_MM_SS).format(localDateTime2Date)); }
6.获取偏移日期时间
public static void main(String[] args) { //获取当前时间 LocalDateTime localDateTime = LocalDateTime.now(ZoneId.systemDefault()); System.out.println("本地当前日期时间:"+localDateTime); //获取偏移时间 //偏移天(正数:往后偏移;负数:往过去时间偏移) System.out.println("明日此时:"+localDateTime.plusDays(1)); System.out.println("明日此时1:"+localDateTime.minusDays(-1)); System.out.println("前天此时:"+localDateTime.plusDays(-2)); //偏移时 System.out.println("当前时间后2个小时:"+localDateTime.plusHours(2)); System.out.println("当前时间前5个小时:"+localDateTime.plusHours(-5)); //偏移年 System.out.println("去年此时:"+localDateTime.plusYears(-1)); //偏移月 System.out.println("下月此时:"+localDateTime.plusMonths(1)); //偏移周 System.out.println("上周此时:"+localDateTime.plusWeeks(-1)); } //不可变性LocalDateTime localDateTime2 = localDateTime.plusMonths(3);System.out.println(localDateTime);//默认输出当前时间System.out.println(localDateTime2);//默认输出当前时间的月份+3//with表示带属性LocalDateTime localDateTime3 =localDateTime.withHour(4);System.out.println(localDateTime);//输出当前时间System,out,println(LocalDateTime3);当前时间的小时数为4.
三、SimpleDataFormat
关于时间对象,我们在页面展示时,可能会想要不同的格式输出,常用的就是通过java.text.SimpleDateFormat设置不同的格式:
yyyy:年
MM:月
z:时区
dd : 日
HH : 小时(24小时制)
hh : 小时(12小时制)
mm : 分钟
ss : 秒
S : 毫秒
D : 一年中的第一几天
F : 一个月中的第几个星期(通过这个月的天数除7,例如5号那就是属于第一个星期)
W : 一个月中的第几个星期(根据实际情况计算)
w : 一年中的第几个星期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date()));//2019-08-05 14:56:23 sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); System.out.println(sdf.format(new Date()));//2019-08-05 02:56:23 下午 sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");// System.out.println(sdf.format(new Date()));//2019-08-05 03:10:32 下午 sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S"); System.out.println(sdf.format(new Date()));//2019-08-05 15:10:32:186 sdf = new SimpleDateFormat("yyyy-MM-dd 一年中的第D天"); System.out.println(sdf.format(new Date()));//2019-08-05 一年中的第217天 sdf = new SimpleDateFormat("yyyy-MM-dd E"); System.out.println(sdf.format(new Date()));//2019-08-05 星期一 sdf = new SimpleDateFormat("yyyy-MM-dd M月中的第W个星期"); System.out.println(sdf.format(new Date()));//2019-08-05 8月中的第2个星期 sdf = new SimpleDateFormat("yyyy-MM-dd M月中的第F个星期"); System.out.println(sdf.format(new Date()));//2019-08-05 8月中的第1个星期 sdf = new SimpleDateFormat("yyyy-MM-dd 一年中的第w个星期"); System.out.println(sdf.format(new Date()));//2019-08-05 一年中的第32个星期 sdf = new SimpleDateFormat("yyyy-MM-dd z"); System.out.println(sdf.format(new Date()));//2019-08-05 CST
四、DataTimeFormat
例如:将字符串“2017-08-18”转化为对应的Java.sql.Date类的对象。
.. 类似于SimpleDateFormat
方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;
方式二:本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG);
方式三:自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss E");
public void test3(){ //方式一:预定义的标准格式 DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; //格式化:日期-->字符串 DateTimeFormatter localDateTime = DateTimeFormatter.now(); String str1 = formatter.format(localDateTime); } //解析:字符串-->日期 TemporalAccessor parse = formatter.parse(test:"当前时间"); System.out.println(parse); //方式二: 本地化相关的格式,如:ofLocalizedDateTime() FormatStyle.Long/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle,Full); String str2=formatter2.format(LocalDate.now()); System.out.println(str2); //只输出当前日期 //自定义的格式 ofpattern("yyyy-MM-dd hh:mm:ss E")//里面字符串形式 DateTimeFormatter formatter3=DateTimeFormattern.ofPattern("yyyy-MM-dd hh:mm:ss E"); //格式化 String str3 = formatter3.format(LocalDateTime.now()); System.out.println(str3); formatter3.parse("时间");
get!