java && 类似%E4%B8%AD%E5%9B%BD这种字符转换问题

发布时间: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);

//解码:中国

Date---->String

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()));
}

}

String—>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;
}

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号