目录
7.str.split()根据分隔符切分字段,当值为空则丢失优化
-
9.java字符串 提取或去除 字母字符串
例:String str = "JYBG3743129_";
提取【数字和字母】:str = str.replaceAll("[^a-zA-Z0-9]", ""); 结果:JYBG3743129
去除【字母】:str = str.replaceAll("[a-zA-Z]", ""); 结果:3743129_
-
8.实体转json字符时,值为空的打印时被丢弃优化
public static void main(String[] args) {
HashMap<String, Object> maps = new HashMap<>();
maps.put("reportId", "111");
maps.put("status", "1");
maps.put("statusDes", null);
maps.put("updateTime", "2022-01-21 12:36:45");
System.out.println("被丢弃:" + JSONObject.toJSONString(maps));
System.out.println("不被丢弃:" + JSONObject.toJSONString(maps, SerializerFeature.WRITE_MAP_NULL_FEATURES));
}
结果:
被丢弃:{"reportId":"111","updateTime":"2022-01-21 12:36:45","status":"1"}
不被丢弃:{reportId:"111",statusDes:null,updateTime:"2022-01-21 12:36:45",status:"1"}
-
7.str.split()根据分隔符切分字段,当值为空则丢失优化
public static void main(String[] args) {
String str = "patient_name_";
//切分后得到两个元素,第三个值为空会被丢弃
String[] strArr = str.split("_");
//切分后得到三个元素,值为空不会被丢弃
String[] strArr2 = str.split("_", -1);
System.out.println(JSONObject.toJSONString(strArr.length) + ":" + JSONObject.toJSONString(strArr2.length));
}
结果:
2:3
-
6.对字符串根据某字符进行分割,字符为空报空指针问题
String str="张三#6岁#男";
//如下存在问题:如str为空,则会报空指针
String[] split = str.split("#");
//优化:如str为空,则split1为一个空数组(推荐)
String[] split1 = str.split("#", -1);
-
5.获取当前时间午别
public class GregorianTest
{
public static void main(String args[])
{
GregorianCalendar ca = new GregorianCalendar();
System.out.println(ca.get(GregorianCalendar.AM_PM));
}
}
结果为“0”是上午 结果为“1”是下午
-
4.字符编码的设置
//原字符
String str="男女";
//设置字符编码为"UTF-8"后目标字符串
String strNew = new String(str.getBytes(),"UTF-8");
-
3.字符编码获取方法
/**
* 字符编码获取方法
*
* @param str
* @return
*/
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GB2312
String s = encode;
return s; //是的话,返回“GB2312“,以下代码同理
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是ISO-8859-1
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是UTF-8
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
return ""; //如果都不是,说明输入的内容不属于常见的编码格式。
}
-
2.通过生日计算年龄
import java.util.Calendar;
import java.util.Date;
public class ParamUtil {
/**
* 通过生日计算年龄
*
* @param birthday
* @return
*/
private static int getAgeByBirth(Date birthday) {
int age = 0;
try {
Calendar now = Calendar.getInstance();
now.setTime(new Date());// 当前时间
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
if (birth.after(now)) {//如果传入的时间,在当前时间的后面,返回0岁
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
age += 1;
}
}
return age;
} catch (Exception e) {//兼容性更强,异常后返回数据
return 0;
}
}
}
-
1.java中判断字符是数字还是汉字
方法一:利用正则表达式
1 2 3 4 5 6 |
|
方法二:利用Pattern.
1 2 3 4 5 6 7 8 9 |
|