LoginSignup
6
0

More than 5 years have passed since last update.

プレミアムフライデーを求める記述をKotlinで書いてみた

Posted at

これは何?

プレミアムフライデーを求めるメソッドを作った(Java8版) - QiitaのコードをKotlinで書いてみたもの。

コード

import java.time.*
import java.time.Month.*
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters

object PremiumFridayKotlin {

    @JvmStatic fun main(args: Array<String>) {

        val years = intArrayOf(2017, 2018, 2019, 2020)
        for (year in years) values()
                    .filterNot { year == 2017 && it == JANUARY }
                    .map { YearMonth.of(year, it) }
                    .map { getMonthOfPremiumFriday(it) }
                    .forEach { println(DateTimeFormatter.ofPattern("yyyy/MM/dd(E)").format(it)) }
    }

    private fun getMonthOfPremiumFriday(ym: YearMonth): LocalDate {

        val premiumFriday = ym.atEndOfMonth().with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY))

        return premiumFriday

    }


}

すごーい、わかりやすい!

注意

あくまでも趣旨はプレミアムフライデーの日付を返すことなので、別のメソッドにしています。

懺悔

Kotlin全く書けないマンなので、IntelliJ IDEA the Java IDE を使ってJavaのコードをKotlinに変換してもらっただけです。

このコードはインチキだ!などとクレーム付けないでください。
こう書けるよ、という指摘は勉強になるのでお願いします。

何が言いたいかというと

Kotlin1.1のリリース、おめでとうございます

Kotlin 1.1 リリース – JavaScriptサポート、コルーチン(coroutine)等々 | JetBrains ブログ

IntelliJ IDEAは素晴らしいツール!

業務で使う機会あるかなぁ

6
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
6
0