面试题,实现一个实体类的序列化和反序列化
public class Test {
private int id;
private String name;
private Integer age;
//set/get略
}
class User{
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化
ObjectOutputStream putStream = new ObjectOutputStream(new FileOutputStream("path"));
putStream.writeObject(test);
putStream.flush();
putStream.close();
//反序列化
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("path"));
for(;;){
Object object=inputStream.readObject();
if (object==null){
break;
}
System.out.println(object);
}
inputStream.close();
}
}
实现当前月份的上一个月的算法
//计算当前时间的上一个月
1.假如PayYm是当前月在实体类中
String payYm = bonusInfo.getPayYm();
Integer month = Integer.parseInt(payYm.substring(payYm.length()-1, payYm.length()));
if (month >= 2 && month <= 12) {
month = month-1;
}else if (month == 1) {
month = 12;
}
//得到当前月和当前月的上一个月,发送到页面
model.addAttribute("payYm",bonusInfo.getPayYm());
model.addAttribute("payYmShang",payYm);