人生を一日とすると今日は何時か.

Sonyの採用企画でプログラミングコンテストをやっているので解きました.

  • あなたの一生を24時間にたとえると今日は何時何分何秒ですか?
    • ただしあなたはあなたの誕生日(a年b月c日)の0時ちょうどに生まれてn歳まで生きる(n歳のときは生きていてn+1歳にはなれない)とし、bとcは一般的な月日の範囲とします。
import java.text.SimpleDateFormat;
import java.util.Date;


public class ClockOfLife {

	public static void main(String[] args) {
		int year = 1988;
		int month = 1;
		int day = 28;
		int life = 80;
		ClockOfLife clockOfLife = new ClockOfLife(year,month,day,life); 
		System.out.println(clockOfLife.getAnswer());
	}

	private Day birthDay;
	private Day deathDay;
	
	public ClockOfLife(int year,int month,int day,int life){
		this.birthDay = new Day(year,month,day);
		this.deathDay = new Day(year+life+1,month,day);		
	}

	public void setBirthDay(int year,int month,int day){
		this.birthDay = new Day(year,month,day);
	}

	public void setLife(int life){
		this.deathDay = new Day(this.birthDay.year+life+1,this.birthDay.month,this.birthDay.day);
	}
	public void setDeathDat(int year,int month,int day){
		this.deathDay = new Day(year,month,day);
	}
	
	public String getAnswer(){
		return this.getTimeFromMinutes(this.getNowSeconds());
	}
	private String getTimeFromMinutes(int minutes){
		int minute = minutes%60;
		int second = ((minutes - minute)%3600/60);
		int hour = (minutes - minute - second*60)/3600;
		return hour+"時"+second+"分"+minute+"秒";
	}
	
	private int getNowSeconds(){
		int oneDayMinutes = 60*60*24;
		Date date = new Date();
		SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
		SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
		SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
	    
		int nowDay = Integer.parseInt(dayFormat.format(date));
		int nowMonth = Integer.parseInt(monthFormat.format(date));
		int nowYear = Integer.parseInt(yearFormat.format(date));
		
		Day today = new Day(nowYear,nowMonth,nowDay);
		int nowLostDay = this.getDays(this.birthDay,today);
		int lifeAllDay = this.getDays(this.birthDay,this.deathDay);
		return oneDayMinutes*nowLostDay/lifeAllDay;
	}
	private int getDays(Day fromDay,Day toDay){
		int lifeAllDay = 0;
		for(int i=fromDay.year;i<toDay.year+1;i++){
			lifeAllDay += 365;
			if( this.isUrudoshi(i) ){
				lifeAllDay++;
			}
		}
		lifeAllDay -= this.getLostDayTheYear(fromDay);
		lifeAllDay -= this.getLostDayTheYear(new Day(toDay.year,12,32)) - this.getLostDayTheYear(toDay);
		return lifeAllDay;
	}
	private int getLostDayTheYear(Day day){
		int days = 0;
		for(int i=1;i<day.month;i++){
			if ( i == 2){
				if( this.isUrudoshi(day.year) ){
					days += 29;
				}else{
					days += 28;
				}
			}else if( i == 4 || i == 6 || i == 9 || i == 11 ){
				days += 30;
			}else{
				days += 31;
			}
		}
		days += day.day - 1;
		return days;
	}
	private boolean isUrudoshi(int year){
		if( year%4 != 0 ){
			return false;
		}else if( year%100 != 0 ){
			return true;
		}else if( year%400 == 0 ){
			return true;
		}else{
			return false;
		}
	}
}
class Day {
	public int year;
	public int month;
	public int day;
	public Day(int year,int month,int day){
		this.year = year;
		this.month = month;
		this.day = day;
	}
}