Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing week of year with Joda-Time

What is the best way to represent week of year with Joda-Time library? I'm looking something as elegant as YearMonth is for representing month of year.

like image 982
michal.kreuzman Avatar asked Jul 31 '12 13:07

michal.kreuzman


People also ask

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

What is the replacement of Joda-time?

time (JSR-310) which is a core part of the JDK which replaces joda library project.

How do I change the date format in Joda-time?

How to change the SimpleDateFormat to jodatime? String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat. forPattern("yyyy-MM-dd'T'HH:mm:ss. SSSS"); DateTime instant = dtf.

Does Joda datetime have TimeZone?

An interval in Joda-Time represents an interval of time from one instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone.


2 Answers

After some searching I found solution using Partial class for this problem.

Partial yearWeek = new Partial(
                   new DateTimeFieldType[] {DateTimeFieldType.weekyear(), 
                                            DateTimeFieldType.weekOfWeekyear()}, 
                                            new int[] {year, week});
like image 147
michal.kreuzman Avatar answered Oct 22 '22 18:10

michal.kreuzman


tl;dr

org.threeten.extra.YearWeek.of( 2018 , 28 ) 

java.time

The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

org.threeten.extra.YearWeek

The java.time classes are extended by the ThreeTen-Extra project. This project includes the YearWeek class, just what you need.

Beware that "week" can have many definitions. The one used here is defined by the ISO 8601 standard, where the week # 1 contains the first Thursday of the calendar year, and Monday starts each week. So a year has either 52 or 53 weeks. A week-based year may contain a few days from the previous/succeeding calendar year around the end of December and beginning of January.

Pass the number of the week-based year, and the week number.

YearWeek yw = YearWeek.of( 2018 , 7 ) ;

Get a date within that week by specifying a day-of-week via the DayOfWeek enum.

LocalDate wednesdayOf2018W07 = yw.atDay( DayOfWeek.THURSDAY ) ;

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, Java 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 Java 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.

like image 26
Basil Bourque Avatar answered Oct 22 '22 20:10

Basil Bourque