Android/Java - Date Difference in days

前端 未结 18 1251
感动是毒
感动是毒 2020-11-22 14:17

I am getting the current date (in format 12/31/1999 i.e. mm/dd/yyyy) as using the below code:

Textview txtViewData;
txtViewDate.setText(\"Today is \" +
              


        
18条回答
  •  长情又很酷
    2020-11-22 14:47

    tl;dr

    ChronoUnit.DAYS.between( 
        LocalDate.parse( "1999-12-28" ) , 
        LocalDate.parse( "12/31/1999" , DateTimeFormatter.ofPattern( "MM/dd/yyyy" ) ) 
    )
    

    Details

    Other answers are outdated. The old date-time classes bundled with the earliest versions of Java have proven to be poorly designed, confusing, and troublesome. Avoid them.

    java.time

    The Joda-Time project was highly successful as a replacement for those old classes. These classes provided the inspiration for the java.time framework built into Java 8 and later.

    Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

    LocalDate

    The LocalDate class represents a date-only value without time-of-day and without time zone.

    Parsing strings

    If your input strings are in standard ISO 8601 format, the LocalDate class can directly parse the string.

    LocalDate start = LocalDate.parse( "1999-12-28" );
    

    If not in ISO 8601 format, define a formatting pattern with DateTimeFormatter.

    String input = "12/31/1999";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM/dd/yyyy" );
    LocalDate stop = LocalDate.parse( input , formatter );
    

    Elapsed days via ChronoUnit

    Now get a count of days elapsed between that pair of LocalDate objects. The ChronoUnit enum calculates elapsed time.

    long totalDays = ChronoUnit.DAYS.between( start , stop ) ; 
    

    If you are unfamiliar with Java enums, know they are far more powerful and useful that conventional enums in most other programming languages. See the Enum class doc, the Oracle Tutorial, and Wikipedia to learn more.


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

提交回复
热议问题