日期格式归纳

日期格式归纳
  1. Java常用日期格式归纳

            package com.Calder;
    
            import java.time.LocalDate;
            import java.time.LocalDateTime;
            import java.time.LocalTime;
            import java.time.format.DateTimeFormatter;
            import java.time.format.FormatStyle;
            import java.time.temporal.TemporalAccessor;
    
            public class Time
            {
                public static void main(String[] args) {
                    System.out.println(LocalTime.now());
                    System.out.println(LocalDateTime.now());
                    System.out.println(LocalDate.now());
                    LocalDateTime.of(2022,4,11,9,20,25,260);
                    LocalDateTime localDateTime = LocalDateTime.now();
                    //对当前的月份加4
                    LocalDateTime localDateTime1 = localDateTime.plusMonths(4);
                    System.out.println(localDateTime1);
                    //对当前月份减2
                    System.out.println(localDateTime.minusMonths(2));
                    //改当前月份为9
                    System.out.println(localDateTime.withMonth(9));
    
                    //localDateTime--String
                    //设置格式DateTimeFormatter.ISO_DATE_TIME;
                    DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
                    //实例化一个类LocalDateTime
                    LocalDateTime LocalDateTime = localDateTime.now();
                    String str = isoDateTime.format(localDateTime);
                    System.out.println(str);
                    //String ---localDateTime
                    TemporalAccessor parse = isoDateTime.parse(str);
                    System.out.println(parse);
                    //常用方法:
    
                    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
                    //localDateTime--String
                    java.time.LocalDateTime now = localDateTime.now();
                    String format = dateTimeFormatter.format(now);
                    System.out.println(format);//2022年4月11日 星期一
    
                    DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
                    java.time.LocalDateTime now1 = localDateTime.now();
                    String format1 = dateTimeFormatter1.format(now1);
                    System.out.println(format1);//2022年4月11日 上午10时39分31秒
    
                    DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
                    java.time.LocalDateTime now2 = localDateTime.now();
                    String format2 = dateTimeFormatter2.format(now2);
                    System.out.println(format2);//2022-4-11 10:39:31
                    DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
                    java.time.LocalDateTime now3 = LocalDateTime.now();
                    System.out.println(dateTimeFormatter3.format(now3));//22-4-11 上午10:42
                }
    
            }
    
    
posted @ 2022-04-11 10:46  爱豆技术部  阅读(232)  评论(0)    收藏  举报