06.20
Date & SimpleDateFormat & Calendar(Calendar是单例对象)
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”); //大写HH表示24小时制
dateFormat.format(date);Calendar calendar = Calendar.getInstance(); calendar.set(年,月,日,时,分,秒); calendar.add(Calendar.YEAR(必须是类名,不是变量名), 10(通过正负数实现日期增减)); dateFormat.format(calendar.getTime()); //参数不能是对象,记得要调getTime()方法 字符串转日期: String str = "1991-07-30 05:30:30"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = dateFormat.parse(str); 算现在的年龄: Date now = new Date(); int age = now.getYear() - date.getYear(); 备注:在输入日期格式的时候有可能不符合日期格式出异常,异常(Exception)不代表错误(Error).有2种解决异常的办法,其一:将异常抛给系统:throws(可以抛给指定异常) ParesException, 第二种方法,通过try:catch:捕捉异常: try{ 正常运行代码 } catch (Exception e) { 捕捉到异常时候执行的代码 } finally { 这段代码一定会执行 }
Exception的子类
总结:今天内容还是比较简单和基础的,内容太少了,着重的介绍的try:catch:的各种子异常类,不用死记硬背,只要能读懂英文的意思,再加上方法的猜想,其实代码很容易就能敲出来.