【1】Date 和 String 互转
// Date 和 String 互转。public static void main(String[] args) {SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");// Date 转 StringString curDateStr = formatter.format(new Date());System.out.println("curDateStr = " + curDateStr);// String 转 Datetry {Date curDate = formatter.parse(curDateStr);System.out.println("curDate = " + curDate);} catch (ParseException e) {e.printStackTrace();}}
// 打印结果
curDateStr = 20171028
curDate = Sat Oct 28 00:00:00 CST 2017
【2】判断字符串是否为数字
// 判断字符串是否为数字(通过正则表达式的匹配来判断,在think-in-java(13.6.2))public static void main(String[] args) {String str1 = "1760801";String str2 = "t1760801r";System.out.println(str1.matches("[0-9]+"));System.out.println(str1.matches("[0-9]{1,}"));System.out.println(str2.matches("[0-9]+"));System.out.println(str2.matches("[0-9]{1,}"));}
// 打印结果
true
true
false
false
【3】mysql for update 行级锁 (计数器自动生成编号)
1.被锁住的行数过多的话,那么行级锁就变成表级锁了;
2.在分布式环境下,给代码添加同步块或同步方法是没有任何意义的。因为查询同一条数据的两个线程可能在不同的机子上;所以这个时候对数据库表或行进行加锁显得尤为重要;
以下内容转自 http://blog.csdn.net/reyzelamp/article/details/78167394
【4】String.matches() 方法 与 Pattern.matcher() 使用 正则表达式 判断字符串格式的比较
/* String.matches() 方法 与 Pattern.matcher() 使用 正则表达式 判断字符串格式的比较 */
/* 注意:不要以为 formatter.parse() 方法可以判断 日期字符串的正确格式,实现需要做 String.matches() 或 Pattern.matcher() 比较 */
public class DateTest1103 {public static void main(String[] args) {SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式String dateStr = "22222017-10-31b"; // 日期荔枝String regex = "^\\d{4}-\\d{2}-\\d{2}$"; // 正则表达式Pattern p = Pattern.compile(regex); // 编译后的模式try {System.out.println("====== String.matches() 测试:");if (dateStr.matches(regex)) {System.out.println("planBrwDate = " + formatter.format(formatter.parse(dateStr)));} else {System.out.println("planBrwDate = error format");/* 即便 不是 日期类型的字符串,SimpleDateFormat.format() 方法 居然解析和格式化正确 */System.out.println("planBrwDateStr = " + formatter.parse(dateStr));System.out.println("planBrwDate = " + formatter.format(formatter.parse(dateStr)));}System.out.println("\n====== Pattern.compile() 测试:");Matcher m = p.matcher(dateStr);if (m.matches() == true) {System.out.println("planBrwDate = " + formatter.format(formatter.parse(dateStr)));} else {System.out.println("planBrwDate = error format");/* 即便 不是 日期类型的字符串,SimpleDateFormat.format() 方法 居然解析和格式化正确 */System.out.println("planBrwDateStr = " + formatter.parse(dateStr));System.out.println("planBrwDate = " + formatter.format(formatter.parse(dateStr)));}} catch (ParseException e1) {e1.printStackTrace();}}
}
// 打印结果:
====== String.matches() 测试:
planBrwDate = error format
planBrwDateStr = Tue Oct 31 00:00:00 CST 22222017
planBrwDate = 22222017-10-31====== Pattern.compile() 测试:
planBrwDate = error format
planBrwDateStr = Tue Oct 31 00:00:00 CST 22222017
planBrwDate = 22222017-10-31
【5】注意String.valueOf() 的使用
【看个荔枝】
public class StringValueOfTest {public static void main(String[] args) {Map<String, Object> map = new HashMap<>();String str = String.valueOf(map.get("tr"));
// String.valueOf(null); // 这里要抛出空指针异常System.out.println("str = " + str);// 输出 str = null}
}
// String.valueof() 方法定义
public static String valueOf(Object obj) {return (obj == null) ? "null" : obj.toString();}
【代码解说】: 当 传入 valueof 的参数为null时,打印 null 字符串;而当传入 null 对象时,会抛出空指针异常;所以在使用 String.valueOf() 的时候千万记得要判断传入的对象是否为null,避免当传入对象为null时,前台显示出 null 字符串的 业务看不懂的尴尬页面;
【6】