5
5

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.

17歳で年齢を計算する。

5
Last updated at Posted at 2014-03-04

Ushio@githubさんの投稿「ゆかりんの年齢を計算する」
http://qiita.com/Ushio@github/items/88e42961ad3b78acd837
のネタにあいのりです。
あまりテストとかしていないです。間違っているかも???
エラー処理もないです。

BD17.java
import java.util.Calendar;

public class BD17 {
    private final static int bdY = 1976;
    private final static int bdM = 2;
    private final static int bdD = 27;

    private final static int age = 17;
    
    public void showBD17() {
        Calendar nowCal = Calendar.getInstance();

        Calendar bdCal = Calendar.getInstance();
        bdCal.set(Calendar.YEAR, bdY);
        bdCal.set(Calendar.MONTH, bdM - 1);
        bdCal.set(Calendar.DATE, bdD);
        int m = 0;
        int d = 0;

        bdCal.add(Calendar.YEAR, age);
        while (bdCal.get(Calendar.YEAR) != nowCal.get(Calendar.YEAR) || bdCal.get(Calendar.MONTH) != nowCal.get(Calendar.MONTH)) {
            bdCal.add(Calendar.MONTH, 1);
            m++;
        }
        if (bdCal.get(Calendar.DATE) > nowCal.get(Calendar.DATE)) {
            bdCal.add(Calendar.MONTH, -1);
            m--;
        }
        while (bdCal.get(Calendar.DATE) != nowCal.get(Calendar.DATE)) {
            bdCal.add(Calendar.DATE, 1);
            d++;
        }

        System.out.printf("%d歳%dヶ月%d日", age,m,d);
    }
}
5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?