发布时间:2023-04-05 08:30
服务器端:
String encodeStr = URLEncoder.encode(“中国”, “utf-8”);
System.out.println(“处理后:” + encodeStr);
//处理后:%E4%B8%AD%E5%9B%BD
客户端:
String decodeStr = URLDecoder.decode(encodeStr, “utf-8”);
System.out.println(“解码:” + decodeStr);
//解码:中国
public class DateToString implements Converter
@Override
public String convert(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
DateToString dateToString = new DateToString();
System.out.println(new Date());
System.out.println(dateToString.convert(new Date()));
}
}
public class StringToDate implements Converter
@Override
public Date convert(String s) {
//不考虑空的情况
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(s);//将字符串转换为Date
} catch (Exception e) {
e.printStackTrace();
}
return null;
}