用Java编写一个程序,要求如下:
已知含有学生生日信息的字符串,如:"张小三2001-11-22,李小四2001-02-05,王小五2003-10-04",请编程计算每个学生的周岁是多少(与程序执行日相差...
已知含有学生生日信息的字符串,如:"张小三 2001-11-22,李小四 2001-02-05,王小五 2003-10-04",请编程计算每个学生的周岁是多少(与程序执行日相差多少整年)。并将结果生成结果字符串,如:"张小三 20岁,李小四 21岁,王小五 18岁",并显示。生成结果字符串时为提高效率,请使用StringBuilder类。学生数据也可以自行编制。
展开
展开全部
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
String str="张小三 2001-11-22,李小四 2001-02-05,王小五 2003-10-04";
String dateComent="0123456789-";
StringBuilder name=new StringBuilder();
StringBuilder date=new StringBuilder();
Boolean clear=false;
Calendar cd = Calendar.getInstance();
for(int i=0;i<str.length();i++){
if(clear==true){
name.delete(0, name.length());
date.delete(0, date.length());
clear=false;
}
if(dateComent.indexOf(str.charAt(i))>-1){
date.append(str.charAt(i));
}else if(','==str.charAt(i) || i==str.length()-1){
cd.setTime(new Date());
Calendar cdThat = Calendar.getInstance();
cdThat.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date.toString()));
cd.get(Calendar.YEAR);
System.out.println(name.toString()+" "+(cd.get(Calendar.YEAR)-cdThat.get(Calendar.YEAR))+"岁");
clear=true;
}else {
name.append(str.charAt(i));
}
}
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
String str="张小三 2001-11-22,李小四 2001-02-05,王小五 2003-10-04";
String dateComent="0123456789-";
StringBuilder name=new StringBuilder();
StringBuilder date=new StringBuilder();
Boolean clear=false;
Calendar cd = Calendar.getInstance();
for(int i=0;i<str.length();i++){
if(clear==true){
name.delete(0, name.length());
date.delete(0, date.length());
clear=false;
}
if(dateComent.indexOf(str.charAt(i))>-1){
date.append(str.charAt(i));
}else if(','==str.charAt(i) || i==str.length()-1){
cd.setTime(new Date());
Calendar cdThat = Calendar.getInstance();
cdThat.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date.toString()));
cd.get(Calendar.YEAR);
System.out.println(name.toString()+" "+(cd.get(Calendar.YEAR)-cdThat.get(Calendar.YEAR))+"岁");
clear=true;
}else {
name.append(str.charAt(i));
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询