import java.time.LocalDate;
import java.time.Period;
public class AgeCalculationExample {
public static void main(String[] args) {
// ユーザーã‹ã‚‰ã®å…¥åŠ›ã‚’ä»®å®šã—ã¦ã„ãªã„ã®ã§ã€ç”Ÿå¹´æœˆæ—¥ã‚’直接指定
String birthdateStr = "1990-05-15";
// 生年月日をLocalDateオブジェクトã«å¤‰æ›
LocalDate birthdate = LocalDate.parse(birthdateStr);
// ç¾åœ¨ã®æ—¥ä»˜ã‚’å–å¾—
LocalDate currentDate = LocalDate.now();
// 年齢を計算
Period agePeriod = Period.between(birthdate, currentDate);
int age = agePeriod.getYears();
// 年齢を表示
System.out.println("生年月日: " + birthdate);
System.out.println("ç¾åœ¨ã®æ—¥ä»˜: " + currentDate);
System.out.println("å¹´é½¢: " + age + "æ³");
}
}
ã“ã®ãƒ—ãƒã‚°ãƒ©ãƒ ã§ã¯ã€ç”Ÿå¹´æœˆæ—¥ã‚’æ–‡å—列ã¨ã—ã¦æŒ‡å®šã—ã€LocalDate.parse() メソッドを使用ã—㦠LocalDate オブジェクトã«å¤‰æ›ã—ã¦ã„ã¾ã™ã€‚ãã®å¾Œã€Period.between() メソッドã§å¹´é½¢ã‚’計算ã—ã€æœ€çµ‚çš„ã«è¨ˆç®—ã•れãŸå¹´é½¢ã‚’表示ã—ã¦ã„ã¾ã™ã€‚
「013 クラスライブラリã€å•題集リスト
🎯 実習ã§ç†è§£ã‚’æ·±ã‚よã†
ã“ã®å†…容ã«ã¤ã„ã¦JavaDrillã§å®Ÿéš›ã«æ‰‹ã‚’å‹•ã‹ã—ã¦å¦ç¿’ã§ãã¾ã™


コメント