Get Date based on day count in Java

后端 未结 7 1179
感情败类
感情败类 2021-01-13 21:46

Simple Question, but Google surprisingly had little on this. I have the number of days from Jan 1st of the year. How can I convert that to a date i

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 22:29

    tl;dr

    Year.of( 2017 )
        .atDay( 159 )
    

    2017-06-08

    …or for current year…

    Year.now( ZoneId.of( "America/Montreal" ) )
        .atDay( 159 )
    

    2018-06-08

    Going the other direction, from date to day-of-year.

    LocalDate.now( ZoneId.of( "America/Montreal" )
             .getDayOfYear()
    

    159

    Using java.time

    The modern way to handle date-time is with the java.time classes. The troublesome old date-time package. The troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleTextFormat are now legacy, supplanted by the java.time classes.

    The Year class represents a year, obviously. To get the current year we need the current date.

    A time zone is crucial in determining a date, and therefore year. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec. In the same way, a few minutes after midnight in Paris on New Year's Day is a new year while still “last year” in Québec.

    Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

    ZoneId z = ZoneId.of( "America/Montreal" );
    Year year = Year.now( z );
    

    If you have a specific year in mind, pass the year number.

    Year year = Year.of( 2017 );
    

    The Year class includes a method atDay for generating a LocalDate when passed a day-of-year number running from 1 to 365 or 366 in a Leap Year. The LocalDate class represents a date-only value without time-of-day and without time zone.

    LocalDate localDate = year.atDay( 159 );
    

    Going the other direction, you can interrogate a LocalDate for its day-of-year by calling LocalDate::getDayOfYear.

    int dayOfYear = localDate.getDayOfYear() ;
    

    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.

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, 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 Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.

提交回复
热议问题