使用 LocalDateTime 计算用户年龄
Calculate the user age with LocalDateTime
我正在尝试制作一个程序,提示用户输入生日年份(例如 2001 年)。然后做一些数学的事情(现在时间 - 用户写的年份(2021 - 2001))。但是,我收到此错误:
Operator "-" cannot be applied to string int.
如何解决这个错误?
代码:
package com.myApp.User;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class GetUserAge {
public String localtimeYear;
public int userBirthdayYear;
public static void main(String[] args) {
Scanner scannerObject = new Scanner(System.in);
GetUserAge getUserAgeObject = new GetUserAge();
LocalDateTime localDateObject = LocalDateTime.now();
DateTimeFormatter localDateObjectFormatted = DateTimeFormatter.ofPattern("yyyy");
getUserAgeObject.localtimeYear = localDateObject.format(localDateObjectFormatted);
// System.out.println(getUserAgeObject.localtimeYear); => 2021
getUserAgeObject.userBirthdayYear = scannerObject.nextInt();
System.out.println(calculateTrueBirthdayYear(getUserAgeObject.userBirthdayYear));
}
static int calculateTrueBirthdayYear(int userBirthdayYear) {
GetUserAge getUserAgeObject = new GetUserAge();
return (getUserAgeObject.localtimeYear - userBirthdayYear);
}
}
String
不是数字
你的错误一定来自这一行:
return (getUserAgeObject.localtimeYear - userBirthdayYear);
... 因为成员字段localtimeYear
引用了一个String
对象。您不能在 String
对象上使用数字运算符 -
。
Year
class
如果只想关注年份,请使用 Year
class。
Year currentYear = Year.now() ;
Year birthYear = Year.of( 1970 ) ;
算一下。
int approximateYearsOld = currentYear.getValue() - birthYear.getValue() ;
示例 class.
package work.basil.age;
import java.time.Year;
public record Person( String name , Year birth )
{
public int calculateApproximateAge ( )
{
return ( Year.now().getValue() - this.birth.getValue() );
}
}
用法。
Person alice = new Person( "Alice" , Year.of( 1972 ) );
int approxAge = alice.calculateApproximateAge();
System.out.println( "alice = " + alice );
System.out.println( "approxAge = " + approxAge );
alice = Person[name=Alice, birth=1972]
approxAge = 49
为什么不利用可用的 API 你可以做类似...
LocalDate ld = LocalDate.of(1972, Month.MARCH, 8);
Period p = Period.between(ld, LocalDate.now());
System.out.println(p.getYears());
System.out.println(p.getMonths());
System.out.println(p.getDays());
我正在尝试制作一个程序,提示用户输入生日年份(例如 2001 年)。然后做一些数学的事情(现在时间 - 用户写的年份(2021 - 2001))。但是,我收到此错误:
Operator "-" cannot be applied to string int.
如何解决这个错误?
代码:
package com.myApp.User;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class GetUserAge {
public String localtimeYear;
public int userBirthdayYear;
public static void main(String[] args) {
Scanner scannerObject = new Scanner(System.in);
GetUserAge getUserAgeObject = new GetUserAge();
LocalDateTime localDateObject = LocalDateTime.now();
DateTimeFormatter localDateObjectFormatted = DateTimeFormatter.ofPattern("yyyy");
getUserAgeObject.localtimeYear = localDateObject.format(localDateObjectFormatted);
// System.out.println(getUserAgeObject.localtimeYear); => 2021
getUserAgeObject.userBirthdayYear = scannerObject.nextInt();
System.out.println(calculateTrueBirthdayYear(getUserAgeObject.userBirthdayYear));
}
static int calculateTrueBirthdayYear(int userBirthdayYear) {
GetUserAge getUserAgeObject = new GetUserAge();
return (getUserAgeObject.localtimeYear - userBirthdayYear);
}
}
String
不是数字
你的错误一定来自这一行:
return (getUserAgeObject.localtimeYear - userBirthdayYear);
... 因为成员字段localtimeYear
引用了一个String
对象。您不能在 String
对象上使用数字运算符 -
。
Year
class
如果只想关注年份,请使用 Year
class。
Year currentYear = Year.now() ;
Year birthYear = Year.of( 1970 ) ;
算一下。
int approximateYearsOld = currentYear.getValue() - birthYear.getValue() ;
示例 class.
package work.basil.age;
import java.time.Year;
public record Person( String name , Year birth )
{
public int calculateApproximateAge ( )
{
return ( Year.now().getValue() - this.birth.getValue() );
}
}
用法。
Person alice = new Person( "Alice" , Year.of( 1972 ) );
int approxAge = alice.calculateApproximateAge();
System.out.println( "alice = " + alice );
System.out.println( "approxAge = " + approxAge );
alice = Person[name=Alice, birth=1972]
approxAge = 49
为什么不利用可用的 API 你可以做类似...
LocalDate ld = LocalDate.of(1972, Month.MARCH, 8);
Period p = Period.between(ld, LocalDate.now());
System.out.println(p.getYears());
System.out.println(p.getMonths());
System.out.println(p.getDays());