精华内容
下载资源
问答
  • LocalDate

    2021-02-02 14:04:48
    LocalDate localDate = LocalDate.now(); //构造指定的年月日 LocalDate localDate1 = LocalDate.of(2019, 9, 10); 获取年月日,星期几 int year = localDate.getYear(); int year1 = localDate.get(Ch

    LocalDate

    只会获取年月日
    创建LocaDate
    //获取当前年月日
    LocalDate localDate = LocalDate.now();
    //构造指定的年月日
    LocalDate localDate1 = LocalDate.of(2019, 9, 10);

    获取年月日,星期几
    int year = localDate.getYear();
    int year1 = localDate.get(ChronoField.YEAR);
    Month month = localDate.getMonth();
    int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
    int day = localDate.getDayOfMonth();
    int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
    DayOfWeek dayOfWeek = localDate.getDayOfWeek();
    int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);

    LocalTime

    只会获取几点几分几秒

    创建LocalTime
    LocalTime localTime = LocalTime.of(13, 51, 10);
    LocalTime localTime1 = LocalTime.now();

    获取时分秒
    //获取小时
    int hour = localTime.getHour();
    int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
    //获取分
    int minute = localTime.getMinute();
    int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
    //获取秒
    int second = localTime.getMinute();
    int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);

    LocalDateTime

    获取年月时分秒

    创建LocalDateTime
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
    LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
    LocalDateTime localDateTime3 = localDate.atTime(localTime);
    LocalDateTime localDateTime4 = localTime.atDate(localDate);

    获取LocalDate
    LocalDate localDate2 = localDateTime.toLocalDate();

    获取LocalTime
    LocalTime localTime2 = localDateTime.toLocalTime();

    年份增减
    LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 10,
    14, 46, 56);
    //增加一年
    localDateTime = localDateTime.plusYears(1);
    localDateTime = localDateTime.plus(1, ChronoUnit.YEARS);
    //减少一个月
    localDateTime = localDateTime.minusMonths(1);
    localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS);

    修改年份
    //修改年为2020
    localDateTime = localDateTime.withYear(2020);
    //修改为2022
    localDateTime = localDateTime.with(ChronoField.YEAR, 2022);

    格式化时间,DateTimeFormatter默认提供了多种格式化方式,如果默认提供的不能满足要求,可以通过DateTimeFormatter的ofPattern方法创建自定义格式化方式
    LocalDate localDate = LocalDate.of(2019, 9, 10);
    String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
    String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
    //自定义格式化
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(“dd/MM/yyyy”);
    String s3 = localDate.format(dateTimeFormatter);

    LocalDate localDate1 = LocalDate.parse(“20190910”, DateTimeFormatter.BASIC_ISO_DATE);
    LocalDate localDate2 = LocalDate.parse(“2019-09-10”, DateTimeFormatter.ISO_LOCAL_DATE);

    展开全文
  • LocalDate.of(2017,1,10) < LocalDate.of(2017,1,10) // false LocalDate.of(2017,1,10) <= LocalDate.of(2017,1,10) // true LocalDate.of(2017,1,10) > LocalDate.of(2017,1,10) // false LocalDate....
  • Java LocalDate

    2020-07-13 23:34:52
    Java LocalDate class is part of Java 8 Date API. Java LocalDate类是Java 8 Date API的一部分。 Java LocalDate (Java LocalDate) Java LocalDate is immutable class and hence thread safe. Java LocalDate是...

    Java LocalDate class is part of Java 8 Date API.

    Java LocalDate类是Java 8 Date API的一部分。

    Java LocalDate (Java LocalDate)

    • Java LocalDate is immutable class and hence thread safe.

      Java LocalDate是不可变的类 ,因此是线程安全的
    • LocalDate provides date output in the format of YYYY-MM-dd.
      Java LocalDate

      LocalDate以YYYY-MM-dd格式提供日期输出。
    • The LocalDate class has no time or Timezone data. So LocalDate is suitable to represent dates such as Birthday, National Holiday etc.

      LocalDate类没有时间或时区数据。 因此LocalDate适合代表生日,国定假日等日期。
    • LocalDate class implements Temporal, TemporalAdjuster, ChronoLocalDate and Serializable interfaces.

      LocalDate类实现TemporalTemporalAdjusterChronoLocalDateSerializable接口。
    • LocalDate is a final class, so we can’t extend it.

      LocalDate是最后一个类,因此我们无法扩展它。
    • LocalDate is a value based class, so we should use equals() method for comparing if two LocalDate instances are equal or not.

      LocalDate是一个基于值的类,因此我们应该使用equals()方法比较两个LocalDate实例是否相等。

    导入Java LocalDate类 (Importing Java LocalDate Class)

    Java LocalDate class is in java.time package. So you can import it using import statement:

    Java LocalDate类在java.time包中。 因此,您可以使用import语句将其导入:

    import java.time.LocalDate;

    创建LocalDate实例 (Creating LocalDate instance)

    There are four standard ways to create LocalDate instance.

    有四种创建LocalDate实例的标准方法。

    1. Invoking the static now() method that returns the current date from system clock.
      LocalDate ldObj = LocalDate.now();
      		
      System.out.println(ldObj); //prints "2017-11-10"

      调用静态now()方法从系统时钟返回当前日期。
    2. By passing Year, Month, and Day values to LocalDate of() method
      LocalDate ldObj = LocalDate.of(2017, 11, 6);
      		
      System.out.println(ldObj); //prints "2017-11-06"

      通过将Year,Month和Day值传递给LocalDate of()方法
    3. Using LocalDate parse() method.
      LocalDate ld = LocalDate.parse("2017-11-10");

      使用LocalDate parse()方法。
    4. Using LocalDate ofInstant() method as shown below.
      LocalDate ld = LocalDate.ofInstant(Instant.now(), ZoneId.systemDefault());

      使用LocalDate ofInstant()方法,如下所示。

    从LocalDate类检索日期信息 (Retrieving Date Information from LocalDate Class)

    Java LocalDate class provides a lot of useful methods to get the more details about date, for example year, month, day of year etc. Let’s look into these through some example programs.

    Java LocalDate类提供了许多有用的方法来获取有关日期的更多详细信息,例如年,月,日等。让我们通过一些示例程序来研究这些内容。

    1. getYear(): returns the year contained within the LocalDate object.
      LocalDate ld = LocalDate.of(2017, 11, 10);
      		
      int year = ld.getYear();
      		
      System.out.println(year); //prints "2017"

      getYear() :返回LocalDate对象中包含的年份。
    2. getMonth(): returns the month contained within the LocalDate object.
      LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
      
      Month month = ld.getMonth();
      
      System.out.println(month + "," + month.getValue()); // prints "NOVEMBER,11"

      getMonth() :返回LocalDate对象中包含的月份。
    3. getDayOfMonth(): returns the day of the month contained within the LocalDate object.
      LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
      
      int dayOfMonth = ld.getDayOfMonth();
      
      System.out.println(dayOfMonth); // prints "10"

      getDayOfMonth() :返回LocalDate对象中包含的月份中的一天。
    4. getDayOfWeek(): returns the day of the week from the LocalDate object.
      LocalDate ld = LocalDate.now(); // today is 10-Nov-2017
      
      DayOfWeek dayOfWeek = ld.getDayOfWeek();
      
      System.out.println(dayOfWeek + "," + dayOfWeek.getValue()); // prints "FRIDAY,5"

      getDayOfWeek() :从LocalDate对象返回星期几。
    5. getDayOfYear(): returns the day of the year from the LocalDate instance.
      LocalDate ld = LocalDate.of(2017, 2, 10);
      
      int dayOfYear = ld.getDayOfYear();
      
      System.out.println(dayOfYear); // prints "41" i.e. 31+10

      getDayOfYear() :从LocalDate实例返回一年中的一天。

    Java LocalDate方法–加号和减号 (Java LocalDate methods – plus and minus)

    1. plusYears(long yearsToAdd): adds the yearsToAdd to the year value and return the copy of LocalDate object. Since LocalDate is immutable, the specified object doesn’t change. Also if the new date is invalid then the last valid date is returned.
      LocalDate ld = LocalDate.of(2017, 2, 10);
      
      LocalDate ldNew = ld.plusYears(3);
      
      System.out.println(ldNew + ":" + ld); // prints "2020-02-10:2017-02-10"
      
      ld = LocalDate.of(2016, 2, 29);
      ldNew = ld.plusYears(1); // 2017-02-29 is invalid date, so it will return 2017-02-28
      System.out.println(ldNew); // prints "2017-02-28"

      plusYears(long yearsToAdd) :将yearsToAdd添加到年份值并返回LocalDate对象的副本。 由于LocalDate是不可变的,因此指定的对象不会更改。 同样,如果新日期无效,则返回最后一个有效日期。
    2. plusWeeks(long weeksToAdd): returns a new LocalDate instance after adding weeksToAdd weeks, no change in the specified object.
      LocalDate ld = LocalDate.of(2017, 11, 10);
      
      LocalDate ldNew = ld.plusWeeks(3);
      
      System.out.println(ldNew); // 2017-12-01

      plusWeeks(long weeksToAdd) :在添加weeksToAdd周之后,返回一个新的LocalDate实例,指定对象没有变化。
    3. plusMonths(long monthsToAdd): returns a copy of specified LocalDate object after adding monthsToAdd months.
      LocalDate ld = LocalDate.of(2017, 11, 10);
      
      LocalDate ldNew = ld.plusMonths(3);
      
      System.out.println(ldNew); // 2018-02-10

      plusMonths(long monthsToAdd) :添加monthsToAdd months后,返回指定LocalDate对象的副本。
    4. plusDays(long daysToAdd): returns a copy of specified LocalDate object after adding daysToAdd days.
      LocalDate ld = LocalDate.of(2017, 11, 10);
      
      LocalDate ldNew = ld.plusDays(10);
      
      System.out.println(ldNew); // 2017-11-20

      plusDays(long daysToAdd) :添加daysToAdd天后,返回指定LocalDate对象的副本。
    5. minusDays(long daysToSubtract): returns a copy of specified LocalDate object after subtracting daysToSubtract days.
      LocalDate ld = LocalDate.of(2017, 11, 10);
      
      LocalDate ldNew = ld.minusDays(10);
      
      System.out.println(ldNew); // 2017-10-31

      minusDays(long daysToSubtract) :减去daysToSubtract days之后,返回指定LocalDate对象的副本。
    6. minusWeeks(long weeksToSubtract): returns a copy of specified LocalDate object after subtracting weeksToSubtract weeks.
      LocalDate ld = LocalDate.of(2017, 11, 10);
      
      LocalDate ldNew = ld.minusWeeks(1);
      
      System.out.println(ldNew); // 2017-11-03

      minusWeeks(long weeksToSubtract) :在减去weeksToSubtract周之后,返回指定LocalDate对象的副本。
    7. minusMonths(long monthsToSubtract): returns a copy of specified LocalDate object after subtracting monthsToSubtract months.
      LocalDate ld = LocalDate.of(2017, 11, 10);
      
      LocalDate ldNew = ld.minusMonths(1);
      
      System.out.println(ldNew); // 2017-10-10

      minusMonths(long monthsToSubtract) :减去monthsToSubtract months之后,返回指定LocalDate对象的副本。
    8. minusYears(long yearsToSubtract): returns a copy of specified LocalDate object after subtracting yearsToSubtract years. If the new LocalDate is invalid (29-Feb), then last valid date is returned.
      LocalDate ld = LocalDate.of(2016, 2, 29);
      
      LocalDate ldNew = ld.minusYears(2);
      
      System.out.println(ldNew); // 2014-02-28

      minusYears(long yearsToSubtract) :减去yearsToSubtract years之后,返回指定LocalDate对象的副本。 如果新的LocalDate无效(2月29日),则返回上一个有效日期。

    迄今为止的Java LocalDate (Java LocalDate to Date)

    We should avoid using legacy java.util.Date class but sometimes we have to convert LocalDate to Date for legacy support. Below are two ways to convert LocalDate to Date, you can use any of them.

    我们应该避免使用旧版java.util.Date类,但有时我们必须将LocalDate转换为Date以获取旧版支持。 以下是将LocalDate转换为Date的两种方法,您可以使用其中任何一种。

    package com.journaldev.java;
    
    import java.time.LocalDate;
    import java.time.ZoneId;
    import java.util.Date;
    
    public class JavaLocalDateExample {
    
    	public static void main(String args[]) {
    
    		LocalDate ld = LocalDate.now();
    
    		Date date = Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant());
    
    		System.out.println(date); // prints "Fri Nov 10 00:00:00 IST 2017"
    		
    		Date date1 = java.sql.Date.valueOf(ld);
    		
    		System.out.println(date1); // prints "2017-11-10"
    	}
    }

    That’s all for java LocalDate class.

    Java LocalDate类就这些了。

    References: API Doc, StackOverflow Article

    参考: API DocStackOverflow文章

    翻译自: https://www.journaldev.com/16896/java-localdate

    展开全文
  • 主要介绍了String类型转localDate,date转localDate的实现代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
  • LocalDate看看新的LocalDate怎么用:// 取当前日期:LocalDate today = LocalDate.now(); // -> 2019-03-29// 根据年月日取日期,04月就是04:LocalDate crischristmas = LocalDate.of(2019, 04, 22); // -> ...

    LocalDate

    看看新的LocalDate怎么用:

    // 取当前日期:

    LocalDate today = LocalDate.now(); // -> 2019-03-29

    // 根据年月日取日期,04月就是04:

    LocalDate crischristmas = LocalDate.of(2019, 04, 22); // -> 2019-04-22

    // 根据字符串取:

    LocalDate endOfFeb = LocalDate.parse(“2019-03-28”); // 严格按照ISO yyyy-MM-dd验证,03写成3都不行,当然也有一个重载方法允许自己定义格式

    LocalDate.parse(“2019-02-31”); // 无效日期无法通过:DateTimeParseException: Invalid date

    日期转换经常遇到,比如:

    // 取本月第1天:

    LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2019-03-01

    // 取本月第2天:

    LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2019-03-02

    // 取本月最后一天,再也不用计算是28,29,30还是31:

    LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2019-03-31

    // 取下一天:

    LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusDays(1); // 变成了2019-04-01

    // 取2019年1月第一个周一,这个计算用Calendar要死掉很多脑细胞:

    LocalDate firstMondayOf2015 = LocalDate.parse(“2019-01-01”).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2019-01-05

    LocalTime

    LocalTime只包含时间,以前用java.util.Date怎么才能只表示时间呢?答案是,假装忽略日期。

    LocalTime包含毫秒:

    LocalTime now = LocalTime.now(); // 11:09:09.240

    你可能想清除毫秒数:

    LocalTime now = LocalTime.now().withNano(0)); // 11:09:09

    构造时间也很简单:

    LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00

    LocalTime mid = LocalTime.parse(“12:00:00”); // 12:00:00

    时间也是按照ISO格式识别,但可以识别以下3种格式: 12:00 12:01:02 12:01:02.345

    JDBC

    最新JDBC映射将把数据库的日期类型和Java 8的新类型关联起来:

    SQL -> Java

    date -> LocalDate

    time -> LocalTime

    timestamp -> LocalDateTime

    展开全文
  • LocalDate相关

    2020-09-16 09:13:26
    LocalDate相关 // 获取当前日期 LocalDate now = LocalDate.now(); // 2020-09-16 // 获取当前时间 LocalTime now1 = LocalTime.now().withNano(0); // 09:05:19 // 获取当前日期和时间 LocalDateTime ...

    LocalDate相关

            // 获取当前日期
            LocalDate now = LocalDate.now(); // 2020-09-16
    
    		// 获取当前时间
            LocalTime now1 = LocalTime.now().withNano(0); // 09:05:19
    
    		// 获取当前日期和时间
            LocalDateTime localDateTime = now.atTime(now1); // 2020-09-16T09:05:19
    
    	    // yyyy-MM-dd HH:mm:ss
            LocalDateTime now2 = LocalDateTime.now().withNano(0); // 2020-09-16T09:07:36
    
            // 获取当前日期的月份--Month类型
            Month month = LocalDate.now().getMonth(); // SEPTEMBER
            
            // 获取当前日期的月份--int类型
            int value = LocalDate.now().getMonth().getValue(); // 9
    
            // 获取2020-09-16的LocalDate类型
            LocalDate parse = LocalDate.parse("2020-09-16"); // 2020-09-16
    
            // 取本月的第一天
            LocalDate with = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()); // 2020-09-01
    
            // 取本月的第一个周一
            LocalDate with1 = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2020-09-07
    
            // 取2020年6月的第一个周五
            LocalDate with2 = LocalDate.parse("2020-06-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY)); // 2020-06-05
    
            // 取2020年3月的最后一个周一
            LocalDate with3 = LocalDate.parse("2020-03-01").with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY)); // 2020-03-30
    
            // 取当前日期的下一天
            LocalDate localDate = LocalDate.now().plusDays(1); // 2020-09-17
    
    展开全文
  • LocalDate

    2020-01-13 17:08:39
    import java.time.LocalDate.*; import java.time.*; public class CalenderTest{ public static void main(String[] args){ LocalDate today = LocalDate.now(); int month = today.getMonthValue(); int d...
  • ----原因:一个程序员问的问题,应该是在练习java,时间应该用Date,后续会补充Date等写法----内容:LocalDate startDate = LocalDate.of(1995, Month.MARCH, 2);System.out.println("出生: "+startDate);LocalDate ...
  • 在做报表统计时,需要对指定时间内的数据做统计,则需要使用到时间日期API在此使用的是java.util.Date的完美私生子LocalDateLocalDate方法介绍now() : 从默认时区的系统时钟中获取当前日期。LocalDate l =...
  • LocalDate笔记

    2020-06-05 17:07:41
    来源 ...LocalDate:本地日期,不包含具体时间 例如:2014-01-14 可以用来记录生日、纪念日、加盟日等。 LocalTime:本地时间,不包含日期。 LocalDateTime:组合了日期和时间,但不包含时差和时区信
  • LocalDate简介

    2017-06-16 18:35:38
    看看新的LocalDate怎么用: // 取当前日期: LocalDate today = LocalDate.now(); // -> 2014-12-24 // 根据年月日取日期,12月就是12: LocalDate crischristmas = LocalDate.of(2014, 12, 25); // -> 2014-12-...
  • String类型转localDate,date转localDate

    万次阅读 2017-12-28 15:31:11
    LocalDate beginDateTime = LocalDate.parse(beginDate, DateTimeFormatter.ofPattern(“yyyy-MM-dd”));
  • LocalDate方法

    2021-01-05 12:00:06
    import java.time.LocalDate; import java.time.Month; import java.util.Date; public class dateTest { public static void main(String[] args) { // System.out.println(new Date()); // // String s = new Date...
  • LocalDate基本方法

    2021-03-28 15:01:15
    标题 LocalDate基本方法 LocalDate基本方法 LocalDate today = LocalDate.now() //获取当前日期 年月日 LocalDate 与 String 之间的转换 LocalDate today = LocalDate.now(); DateTimeFormatter fmt = ...
  • localdate java

    2019-08-28 10:52:38
    String startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String endTime = LocalDateTime.now().format(DateTimeForma...
  • 一.LocalDate用法1.1.获取当前时间,最大时间,最小时间 /** * 获取当前时间,最大时间,最小时间 */ @Test public void demo01() { LocalDate now = LocalDate.now(); LocalDate max = LocalDate.MAX; Lo...
  • LocalDate与LocalTime

    2020-04-03 15:52:17
    LocalDate 不变性:新的日期/时间API中,所有的类都是不可变的,这对多线程环境有好处。 LocalDate localDate = LocalDate.now(); //获取当前日期 LocalDate localDate = LocalDate.of(1999,12,31); //获取日期 ...
  • Date转LocalDate

    2021-03-08 16:41:37
    import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; public class DateUtils{ /** * LocalDate转Date * @param localDate * @return */ ...
  • localdate Java 8对日期和时间API进行了重大更改,这是在JSR 310:Date and Time API的JDK中包括了Joda Time API 。 此JSR由Joda Time的创建者Stephen Colebourne领导。 有许多惊人的API可用于日期和时间。 在本文...
  • 看代码吧~String 类型转localDateLocalDate beginDateTime = LocalDate.parse(beginDate, DateTimeFormatter.ofPattern(“yyyy-MM-dd”));date类型转localDateDate dates = new SimpleDateFormat(“yyyyMM”).parse...
  • LocalDate常用技巧

    2020-10-19 15:40:02
    LocalDate是Java8新增的处理日期的类,使用起来比java.utils.date方便了许多。记录一些常用技巧: // 取当前日期: LocalDate today = LocalDate.now(); // -> 2014-12-24 // 根据年月日取日期: LocalDate ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 5,272
精华内容 2,108
热门标签
关键字:

localdate