关于Java日期简单应用
//日期格式化
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String myString1 = formatter1.format(calendar.getTime());
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String myString2 = formatter2.format(calendar.getTime());
//字符串转换成日期格式,获得年月日
String strDate = "2013/01/01";
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy/MM/dd");
Date date = formatter3.parse(strDate);
Calendar myCalendar = Calendar.getInstance();
myCalendar.setTime(date);
//年
System.out.println(myCalendar.get(Calendar.YEAR));
//月(java中月份是从0开始的)
System.out.println(myCalendar.get(Calendar.MONTH)+1);
//日
System.out.println(myCalendar.get(Calendar.DAY_OF_MONTH));
//根据生日计算年龄
String birth = "1983/12/24";
SimpleDateFormat formatter4 = new SimpleDateFormat("yyyy/MM/dd");
Date dateBirth= formatter4.parse(birth);
long day=(date.getTime()- dateBirth.getTime())/(24*60*60*1000) + 1;
String year=new DecimalFormat("#").format(day/365);
System.out.println(year);