zoukankan      html  css  js  c++  java
  • LocalDateTime的方法总结

     LocalDateTime的使用方法

       1  public static LocalDateTime now():从指定时区的系统时钟获取当前的日期时间。

       2  public static LocalDateTime now(Clock clock) 从指定的时钟获取当前的日期时间,clock不能为空。

       3 public static LocalDateTime of(LocalDate date, LocalTime time)从日期和时间获取一个 LocalDateTime的实例。

       4 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) 从年,月,日,小时,分钟和秒获得LocalDateTime的实例,将纳秒设置为零。

           year - 从默认的 MIN_YEAR到 MAX_YEAR 的年份   

           month - 代表月份的1-12,不为空

           dayOfMonth - 代表月份中1-31,以月份中天数大小为准。

           hour - 从0到23的小时。

           minute - 从0到59的分钟。

           second - 从0到59的秒其他的同上


      5 public int getDayOfYear() 获得日期字段  (获得是年份中的第几天) 例如 2020/12/3 获得2020的第338天。
      6 public int getDayOfMonth() 获取月份字段 (获取月份中的第几天) 例如 2020/12/3 获得12月的第3天。
      7 public DayOfWeek getDayOfWeek()获取星期几字段,这是一个枚举DayOfWeek 。(获得整个星期的星期几)例如 2020/12/3 获得这周的星期四。
      8 public int getYear()获取年份字段。
      9 public Month getMonth()使用Month枚举获取月份字段(获得不是数组而是英文单词)。
      10 public int getHour()获取时间字段。
      11 public int getMinute()获取分钟字段。
      12 public int getSecond() 获得第秒的字段。


    13 public LocalDateTime withYear(int year)返回此年份变更的LocalDateTime的副本。 时间不影响计算,结果将相同。 如果一年中的日期无效,它将被更改为该月的最后一个有效日期。
    14 public LocalDateTime withMonth(int month)返回此年份更改的LocalDateTime的副本。 时间不影响计算,结果将相同。 如果一年中的日期无效,它将被更改为该月的最后一个有效日期。
    15 public LocalDateTime withDayOfMonth(int dayOfMonth)返回这个LocalDateTime的副本,并更改日期。 如果结果LocalDateTime无效,则会抛出异常。 时间不影响计算,结果将相同。
    16 public LocalDateTime withDayOfYear(int dayOfYear)返回此LocalDateTime的副本。 如果得到LocalDateTime是无效的,则抛出异常。
    17 public LocalDateTime withHour(int hour)返回此日期值更改的LocalDateTime的副本。
    18 public LocalDateTime withMinute(int minute)返回这个LocalDateTime的副本,小时值更改。
    19 public LocalDateTime withSecond(int second)返回这个LocalDateTime的副本,并更改秒数值。

    20 public LocalDateTime plusYears(long years)返回这个LocalDateTime的副本,并以指定的时间段添加。
    该方法通过三个步骤将指定的数量添加到年份字段:
      1.将输入年份添加到年份字段
      2.检查结果日期是否无效
    3.如果有必要,将月份调整到最后一个有效日期
      例如,2008-02-29(闰年)加上一年将导致无效日期2009-02-29(标准年)。 而不是返回无效结果,而是选择2009-02-28的最后一个有效日期。

    21 public LocalDateTime plusMonths(long months)返回这个LocalDateTime的副本,其中指定的时间段以月为单位。
      此方法通过三个步骤将指定的数量添加到月份字段:
    1.将输入的月份添加到月份字段
      2.检查结果日期是否无效
      3.如果有必要,将月份调整到最后一个有效日期
      例如,2007-03-31加上一个月将导致无效日期2007-04-31。 而不是返回无效结果,而是选择2007-04-30的最后一个有效日期。

    22 public LocalDateTime plusWeeks(long weeks)返回这个LocalDateTime的副本,其中指定的周期以周为单位。
      该方法将指定的数量以周为单位,根据需要增加月份和年份字段的日期字段,以确保结果保持有效。 如果超过最大/最小年份,结果将无效。
      例如,2008-12-31加上一周将导致2009-01-07。

    23 public LocalDateTime plusDays(long days)返回此LocalDateTime的指定期间的LocalDateTime的副本。
      此方法将指定的金额添加到天数字段中,根据需要增加月份和年份字段,以确保结果保持有效。 如果超过最大/最小年份,结果将无效。
      例如,2008-12-31加上一天会导致2009-01-01。

    24 public LocalDateTime plusHours(long hours)以指定的时间段返回此LocalDateTime的副本,小时数。
    25 public LocalDateTime plusMinutes(long minutes)以指定的时间(以分钟为单位)返回此LocalDateTime的副本。
    26 public LocalDateTime plusSeconds(long seconds)返回此副本LocalDateTime在加秒的规定时间。

    27 public LocalDateTime minusYears(long years)返回此LocalDateTime的副本,以减去的年份为单位。
      该方法从三个步骤中减去指定的数量:
    1.从年度字段减去输入年数
      2.检查结果日期是否无效
      3.如果有必要,将月份调整到最后一个有效日期
      例如,2008-02-29(闰年)减去一年将导致无效日期2009-02-29(标准年)。 而不是返回无效结果,而是选择2009-02-28的最后一个有效日期。

    28 public LocalDateTime minusMonths(long months)返回此LocalDateTime的副本,指定的时间以月为单位减去。
      该方法从以下三个步骤中减去月份字段中指定的数量:
      1.从月份字段减去输入月份
      2.检查结果日期是否无效
      3.如果有必要,将月份调整到最后一个有效日期
      例如,2007-03-31减去一个月将导致无效日期2007-04-31。 而不是返回无效结果,而是选择2007-04-30的最后一个有效日期。
    29 public LocalDateTime minusWeeks(long weeks)返回此LocalDateTime一个副本,以指定的周期扣除。
      该方法从必要的日期字段减去月和年字段中减去指定的数量,以确保结果保持有效。 如果超过最大/最小年份,结果将无效。
      例如,2009-01-07减去一周将导致2008-12-31。
    30 public LocalDateTime minusDays(long days)返回此LocalDateTime的副本,其中指定的时间间隔以天为单位。
      该方法从天数字段减去指定数量,根据需要增加月份和年份字段,以确保结果保持有效。 如果超过最大/最小年份,结果将无效。
      例如,2009-01-01减去一天会导致2008-12-31。
    31 public LocalDateTime minusHours(long hours)以指定的时间段返回此LocalDateTime的副本,以减少的小时数。
    32 public LocalDateTime minusMinutes(long minutes)返回此LocalDateTime的副本,其中指定的时间间隔以分钟为单位。
    33 public LocalDateTime minusSeconds(long seconds)返回此LocalDateTime的副本,其中指定的时间间隔以秒为单位。

    34 public String format(DateTimeFormatter formatter)使用指定的格式化程序格式化此日期时间。这个日期时间将传递给格式化程序以生成一个字符串。
    35 public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)使用特定的格式化LocalDateTime从文本字符串获取LocalDateTime的实例。
    36 public boolean isBefore(ChronoLocalDateTime<?> other) 检查此日期时间是否在指定的日期时间之前
    37 public boolean isAfter(ChronoLocalDateTime<?> other)检查这个日期时间是否在指定的日期之后。
    38 public boolean equals(Object obj) 检查这个日期时间是否等于另一个日期时间。
    39public boolean isEqual(ChronoLocalDateTime<?> other)检查此日期时间是否等于指定的日期时间。
    40 public String toString()将此日期时间输出为String,如2007-12-03T10:15:30。

     以下是测试方法,可以copy测试。

     1 @RunWith(SpringRunner.class)
     2 @SpringBootTest
     3 public class DataTimeUtil {
     4     @Test
     5     public void test(){
     6         LocalDateTime local=LocalDateTime.now();
     7         System.out.println("获取默认当前时间为 "+local);
     8 
     9         Clock clock=Clock.systemDefaultZone();
    10         System.out.println("获取默认当前的Clock "+clock.toString());
    11         LocalDateTime localDateTime1=LocalDateTime.now(clock);
    12         System.out.println("获取Clock时区的时间为"+localDateTime1);
    13 
    14         LocalDate localDate= LocalDate.now();
    15         LocalTime localTime=LocalTime.now();
    16         LocalDateTime localDateTime2=LocalDateTime.of(localDate,localTime);
    17         System.out.println("localDate是 "+localDate+" localTime是 "+localTime+" 获取的localDateTime2 "+localDateTime2);
    18 
    19         System.out.println("分割线-------------------------------------------------------------------分割线");
    20         Integer dayOfYear=local.getDayOfYear();
    21         Integer dayOfMonth=local.getDayOfMonth();
    22         DayOfWeek dayOfWeek=local.getDayOfWeek();
    23         System.out.println("获得是年份中的第几天 "+dayOfYear+" 获取月份中的第几天 "+dayOfMonth+" 获得整个星期的星期几 "+dayOfWeek);
    24 
    25         Integer year=local.getYear();
    26         Month month=local.getMonth();
    27         Integer hour=local.getHour();
    28         Integer minute=local.getMinute();
    29         Integer second=local.getSecond();
    30         System.out.println("获得年份 "+year+" 获得月份 "+month+" 获得小时 "+hour+" 获得分钟 "+minute+ " 获得秒钟 "+second);
    31 
    32         System.out.println("分割线-------------------------------------------------------------------分割线");
    33         LocalDateTime currentTimeWithDayOfYear=local.withDayOfYear(12);
    34         LocalDateTime currentTimeWithDayOfMonth=local.withDayOfMonth(11);
    35         LocalDateTime currentTimeWithYear=local.withYear(2019);
    36         LocalDateTime currentTimeWithMonth=local.withMonth(1);
    37         LocalDateTime currentTimeWithHour=local.withHour(12);
    38         LocalDateTime currentTimeWithMinute=local.withMinute(22);
    39         LocalDateTime currentTimeWithSecond=local.withSecond(12);
    40         System.out.println("年份中天变更 "+currentTimeWithDayOfYear+" 月份中天变更 "+currentTimeWithDayOfMonth+" 年份变更 "+currentTimeWithYear
    41                           +"月份变更 "+currentTimeWithMonth+" 时变更 "+currentTimeWithHour+" 分钟变更 "+currentTimeWithMinute
    42                           +"秒变更 "+currentTimeWithSecond);
    43 
    44         System.out.println("分割线-------------------------------------------------------------------分割线");
    45         LocalDateTime currentTimeAddYear=local.plusYears(1);
    46         LocalDateTime currentTimeAddMonth=local.plusMonths(1);
    47         LocalDateTime currentTimeAddDay=local.plusDays(1);
    48         LocalDateTime currentTimeAddWeek=local.plusWeeks(1);
    49         LocalDateTime currentTimeAddHour=local.plusHours(1);
    50         LocalDateTime currentTimeAddSecond=local.plusSeconds(1);
    51         LocalDateTime currentTimeAddMinutes=local.plusMinutes(1);
    52         System.out.println("加年份 "+currentTimeAddYear+" 加月份 "+currentTimeAddMonth+" 加天 "+currentTimeAddDay
    53                          +" 加周 "+currentTimeAddWeek+" 加小时 "+currentTimeAddHour+" 加分钟 "+currentTimeAddMinutes
    54                          +" 加秒 "+currentTimeAddSecond);
    55 
    56         LocalDateTime currentTimeMinusYear=local.minusYears(1);
    57         LocalDateTime currentTimeMinusMonth=local.minusMonths(1);
    58         LocalDateTime currentTimeMinusWeek=local.minusWeeks(1);
    59         LocalDateTime currentTimeMinusDay=local.minusDays(1);
    60         LocalDateTime currentTimeMinusHour=local.minusHours(1);
    61         LocalDateTime currentTimeMinusMinute=local.minusMinutes(1);
    62         LocalDateTime currentTimeMinusSecond=local.minusSeconds(1);
    63         System.out.println("减年份 "+currentTimeMinusYear+" 减月份 "+currentTimeMinusMonth+" 减天 "+currentTimeMinusDay
    64                          +" 减周 "+currentTimeMinusWeek+" 减小时 "+currentTimeMinusHour+" 减分钟 "+currentTimeMinusMinute
    65                          +" 减秒 "+currentTimeMinusSecond);
    66         System.out.println("分割线-------------------------------------------------------------------分割线");
    67 
    68         DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    69         //它会把时间格式化为指定格式的字符串 创建实例才能使用format()方法
    70         String nowTime=local.format(formatter);
    71         System.out.println("格式化后的数据时(得到String类型) "+nowTime);
    72         //parse()方法通过LocalDateTime
    73         String str11="2007-12-03T10:15:30";
    74         //注意当String str="2010-1-1 10:10:10"格式会报错
    75         String str="2010-01-01 10:10:10";
    76         LocalDateTime parseTime=LocalDateTime.parse(str11);
    77         LocalDateTime parseTime1=LocalDateTime.parse(str,formatter);
    78         System.out.println("格式化后的数据时(得到LocalDateTime类型) "+ parseTime+
    79                            "格式化后的数据时(得到LocalDateTime类型)有俩个参数 "+parseTime1);
    80         //传入的参数是LocalDateTime类型
    81         LocalDateTime date=parseTime;
    82         boolean flag=local.isBefore(date);
    83         boolean flag1=local.isAfter(date);
    84         System.out.println("传入的参数是 "+date+" 是否在传入的数据之前 "+flag+"是否在传入的数据之后 "+flag1);
    85         //是否相等
    86         boolean flag2=local.equals(date);
    87         System.out.println("localDateTime1和localDateTime2是否相等 "+flag2);
    88         boolean flag3=local.isEqual(date);
    89         System.out.println("localDateTime1和localDateTime2是否相等 "+flag3);
    90 
    91         String Str=date.toString();
    92         System.out.println("输出字符串 "+Str);
    93     }
    94 }
  • 相关阅读:
    HTML head 头标签
    p标签text-align:justify以及CSS文字两端对齐
    css雪碧图制作
    ps去掉图片上的文字
    Qt Quick之TableView的使用
    Qt Quick之Canvas
    Qt Quick中的信号与槽
    QT之在QML中使用C++类和对象
    rhel7+apache+c cgi+动态域名实现web访问
    (转载)JDOM/XPATH编程指南
  • 原文地址:https://www.cnblogs.com/linyu1/p/14079141.html
Copyright © 2011-2022 走看看