根据session用户id读取用户信息,age根据当前时间减去birthday

此博客展示了一段Java代码,实现点击左边tree进入个人资料页面的功能。代码根据session中的用户id读取用户信息,包括个人资料、地址等,同时通过当前时间减去生日计算出用户年龄,并将这些信息存入上下文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**点击左边tree  进入个人资料 页面* @throws TNBusinessException */
public String queryPersonalInfo() throws TNBusinessException{
ActionContext ctx = ActionContext.getContext();
//取个人资料
@SuppressWarnings("rawtypes")
Map session = ctx.getSession();
UserSdto user = (UserSdto)session.get("user");
Person resultPerson = this.personRelevanceSo.inquiryPersonById(user.getPersonId(),QueryLevel.LEVEL1, initControl());
ctx.put("resultPerson",resultPerson);
Long age = getAgeByBirthday(resultPerson.getBirthday());
ctx.put("age",age);
Address resultAddress = this.personRelevanceSo.inquiryAddressByPersonID(String.valueOf(user.getPersonId()), initControl());
String address = resultAddress.getProvinceName() + resultAddress.getCityName() + resultAddress.getAreaName()
+ resultAddress.getCommunityName() + resultAddress.getBuildName()
+ resultAddress.getUnitName() + resultAddress.getHouseNum();
ctx.put("address",address);
return "queryPersonalInfo/success";
}

/**
* 将String类型的日期转换成date类型
*/
private Date strDateChangeToDate(String dateStr){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 根据生日,获取年龄
*/
private Long getAgeByBirthday(String birthday){
//获取当前时间
Calendar calendar = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = df.format(calendar.getTime());
Date  birthDate = strDateChangeToDate(birthday);
Date  currentDate = strDateChangeToDate(dateStr);
Long num1 = ((currentDate.getTime() -birthDate.getTime())/(24*60*60*1000))/365;
Long num2 = ((currentDate.getTime() -birthDate.getTime())/(24*60*60*1000))%365;
if(num2 > 0 ){
num1 = num1 +1;
}
return num1; 

}

以上JAVA代码实现了根据session中的用户id读取用户信息,其中age信息是根据当前时间减去birthday得到的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值