Help us understand the problem. What is going on with this article?

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

消費税率が 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");
  }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away