How do I discover the Quarter of a given Date?

前端 未结 14 1064
小鲜肉
小鲜肉 2020-12-24 05:37

Given a java.util.Date object how do I go about finding what Quarter it\'s in?

Assuming Q1 = Jan Feb Mar, Q2 = Apr, May, Jun, etc.

相关标签:
14条回答
  • 2020-12-24 06:26

    tl;dr

    YearQuarter
    .from(
        LocalDate.of( 2018 , 1 , 23 ) 
    )
    

    ThreeTen-Extra

    The Answer by abdmob is correct: Using java.time is the wise way to go.

    In addition, the ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

    Its useful classes include:

    • Quarter
    • YearQuarter

    Current quarter

    To get the current quarter, specify a time zone. Determining a quarter means determining a date. And determining a date means specifying a time zone. For any given moment, the date varies around the globe by zone. Specify by using a proper time zone name in form of continent/region. Never use the 3-4 letter abbreviations such as EST or IST.

    ZoneId z = ZoneId.of( "America/Montreal" );
    YearQuarter yq = YearQuarter.now( z );
    

    Given a java.util.Date

    If given a java.util.Date first convert to a java.time type. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

    Instant instant = myUtilDate.toInstant();
    

    Assign a time zone to get a ZoneDateTime.

    ZoneId z = ZoneId.of( "America/Montreal" );
    ZonedDateTime zdt = instant.atZone( z );
    

    Generate a YearQuarter from that ZonedDateTime.

    YearQuarter yq = YearQuarter.from( zdt );
    

    Use throughout your code

    You should be passing around instances of YearQuarter rather than mere numbers or strings. Using objects provides type-safety, ensures valid values, and makes your code more self-documenting.


    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.

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

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

    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, Java SE 11, and later - 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
      • Most 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.

    0 讨论(0)
  • 2020-12-24 06:26

    with java8 you may use formatter:

    import java.time.format.DateTimeFormatter
    import java.time.LocalDate
    
    println("withQuarter: " + LocalDate.of("2016".toInt,"07".toInt,1).format(DateTimeFormatter.ofPattern("yyyyQMM")))
    

    withQuarter: 2016307

    0 讨论(0)
提交回复
热议问题