2

I am currently using jodaTime DateTime class for my Java project. But it does not have support for quarter of the year and week of the year.

Is there any Java library which can be used for that purpose?

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 1
    Quarter is trivial, you can implement that easily yourself. For week of the year, java.util.Calendar: http://stackoverflow.com/questions/10893443/understanding-java-util-calendar-week-of-year – ManoDestra Jul 25 '16 at 15:15
  • @ManoDestra what is the week format it supports? i.e. mon-sun or sun-sat ? – Supun Wijerathne Jul 25 '16 at 15:23
  • 2
    It defaults to the local locale, but you can set the locale yourself explicitly, of course. See the link I posted for further info. – ManoDestra Jul 25 '16 at 15:26
  • 1
    [Asked and answered](https://softwarerecs.stackexchange.com/q/48724/1255) on sister site, *Software Recommendations Stack Exchange*. – Basil Bourque Feb 19 '18 at 01:15

2 Answers2

4

In java 8 DateTimeFormatter supports quarters using q format and week of the year using w:

LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("qqq w");
String text = date.format(formatter);

Results in third quarter and 31st week of the year for todays date (July 25th):

"Q3 31"
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
1

ThreeTen-Extra

Is there any Java library which can be used for that purpose?

Yes.

The ThreeTen-Extra project has classes for:

  • Quarter
    An enum representing the 4 quarters of the year - Q1, Q2, Q3, and Q4. These are defined as January–March, April–June, July–September, and October–December.
  • YearQuarter
    A year-quarter in the ISO 8601 calendar system, such as 2007-Q2.
  • YearWeek
    A year-week in the ISO week date system such as 2015-W13.

No need to reinvent the wheel with your own. These classes also provide extra functionality such as math with plus and minus methods.

ThreeTen-Extra extends the java.time framework built into Java 8 and later, defined by JSR 310. See Oracle Tutorial. And ThreeTen-Extra is a proving ground for possible future additions to java.time.


Both Joda-Time and java.time are led by the same man, Stephen Colbourne.

The Joda-Time team advises migration to java.time. Joda-Time is still actively supported for maintenance but any major new work will be put into java.time and ThreeTen-Extra instead of Joda-Time.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154