DevExperience(1710)

【1】Date 和 String 互转

// Date 和 String 互转。
	public static void main(String[] args) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		// Date 转 String
		String curDateStr = formatter.format(new Date());
		System.out.println("curDateStr = " + curDateStr);
		// String 转 Date
		try {
			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】





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值