Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Date API - Get ordinal position of the day in the month

Tags:

java

date

java-8

Inspired by the following posts

  • Get the first Monday of a month
  • Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012)

I need a function that returns the ordinal position for a given day in the month, for example:

01/01/1970 = 1 because it's the first Thursday in January, 1970

02/01/1970 = 1 because it's the first Friday in January, 1970

19/01/1970 = 3 because it's the third Monday in January, 1970

31/01/1970 = 5 because it's the fifth Saturday in January, 1970

What have I tried? - Nothing...I don't even know where to start; the Java 8 date/time API is pretty new to me.

Ideally I want a function with this signature:

public int getOrdinalPosition(TemporalAccessor temporal) {
    ...
}
like image 248
Matthew Layton Avatar asked Jan 06 '19 02:01

Matthew Layton


3 Answers

Well, take 19/01/1970. Subtract 7 days from that and it's.. still january. Subtract 7 days again and.. still january. Subtract 7 days a 4th time and.. oh hey it's no longer january. The 4th time you removed 7 days, it ceased to be the right month.

That's all you need here.

Relevant methods: a for loop, a counter, and.. the minusDays(7) method, and getMonth().

like image 197
rzwitserloot Avatar answered Nov 13 '22 22:11

rzwitserloot


ChronoField.ALIGNED_WEEK_OF_MONTH

You can use LocalDate.get() and ChronoField.ALIGNED_WEEK_OF_MONTH.

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;

/**
 *
 * @author Sedrick
 */
public class JavaApplication19 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        LocalDate localDate1 = LocalDate.of(1970, 01, 01);
        LocalDate localDate2 = LocalDate.of(1970, 02, 01);
        LocalDate localDate3 = LocalDate.of(1970, 01, 19);
        LocalDate localDate4 = LocalDate.of(1970, 01, 31);


        System.out.println(localDate1.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
        System.out.println(localDate2.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
        System.out.println(localDate3.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
        System.out.println(localDate4.get(ChronoField.ALIGNED_WEEK_OF_MONTH));

        System.out.println(getOrdinalPosition(localDate4));
    }

    static public int getOrdinalPosition(TemporalAccessor temporal) {
        return LocalDate.from(temporal).get(ChronoField.ALIGNED_WEEK_OF_MONTH);
    }
}

Output:

run:
1
1
3
5
5
BUILD SUCCESSFUL (total time: 0 seconds)
like image 3
Sedrick Avatar answered Nov 13 '22 20:11

Sedrick


What you need is ChronoField.ALIGNED_WEEK_OF_MONTH

LocalDate.of(1970,1,1).get(ChronoField.ALIGNED_WEEK_OF_MONTH)      //1
LocalDate.of(1970,1,2).get(ChronoField.ALIGNED_WEEK_OF_MONTH)      //1
LocalDate.of(1970,1,19).get(ChronoField.ALIGNED_WEEK_OF_MONTH)     //3
LocalDate.of(1970,1,31).get(ChronoField.ALIGNED_WEEK_OF_MONTH)     //5
like image 2
Pankaj Singhal Avatar answered Nov 13 '22 21:11

Pankaj Singhal