0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

sfdcで指定した二つ日付の間の月を取得

Posted at

・指定した二つ日付の間の月を取得
 startFy:202009
 endFY:202109
・返却値:
 202009,202010,202011,202012,202101,202102,202103,202104,202105,202106,202107,202108,202109

String startFy = '202009';
String endFY= '202109';
// getFYを呼び出す
Set<String> setFY = getFY(startFy, endFY)

/*
 * 年月を取得
*/
public Set<String> getFY(String startFy, String endFY){
    Set<String> setFY = new Set<String>();
    
    if (String.isEmpty(startFY) || String.isEmpty(endFY)) {
        return setFY;
    }
    // 削除開始年月の取得
    Integer startYear = Integer.valueOf(startFY.substring(0, 4));
    Integer startMonth = Integer.valueOf(startFY.substring(4, 6));
    // 削除終了年月の取得
    Integer endYear = Integer.valueOf(endFY.substring(0, 4));
    Integer endMonth = Integer.valueOf(endFY.substring(4, 6));
    for (integer i = startYear; i <= endYear; i++) {
        String dateFy = '';
        if (startYear == endYear) {
            for (integer j = startMonth; j <= endMonth; j++) {
                if (j < 10) {
                    dateFy = i + '0' + j;
                } else {
                    dateFy = i + '' + j;
                }
                setFY.add(dateFy);
            }
        } else {
            if (i == startYear) {
                for (integer j = startMonth; j <= 12; j++) {
                    if (j < 10) {
                        dateFy = i + '0' + j;
                    } else {
                        dateFy = i + '' + j;
                    }
                    setFY.add(dateFy);
                } 
            } else if (i == endYear) {
                for (integer j = 1; j <= endMonth; j++) {
                    if (j < 10) {
                        dateFy = i + '0' + j;
                    } else {
                        dateFy = i + '' + (j + 1);
                    }
                    setFY.add(dateFy);
                }
            } else {
                for (integer j = 1; j <=12; j++) {
                    if (j < 10) {
                        dateFy = i + '0' + j;
                    } else {
                        dateFy = i + '' + j;
                    }
                    setFY.add(dateFy);
                }
            }
        }
    }
    return setFY;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?