1
1

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 5 years have passed since last update.

Javaで税率を返すメソッドを作成する

Last updated at Posted at 2019-10-01

消費税率が 8% から 10% になりました。

税率は

  public static final String TAX_PERCENT = "0.08";

みたいに定数として持たせると危険です。

2014年04月 8%
2019年10月 10%

のようにして税率は変わってきており、今後も増税のタイミングが発生することが予想されます。

このように日時によって税率が変わるため、以下のように税率を返すメソッドを用意するのがおすすめです。

  public static final String EIGHT_TAX_PERCENT = "0.08";

  public static final String TEN_TAX_PERCENT = "0.10";

  public static final String TEN_TAX_PERCENT_CHANGE_DAY = "2019-10-01 00:00:00";

  public static BigDecimal getTaxPercent() {
    try {
      Date now = new Date();
      SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date dateTime = sdformat.parse(TEN_TAX_PERCENT_CHANGE_DAY);
      return now.getTime() >= dateTime.getTime()
        ? new BigDecimal(TEN_TAX_PERCENT)
        : new BigDecimal(EIGHT_TAX_PERCENT);
    } catch (ParseException e) {
      System.out.println(e);
    }
    // きっと永遠にこない
    return new BigDecimal("0");
  }
1
1
2

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?