Java Calendar对象与字符串的转换

字符串要先转换成Date对象:


		String dateStr="2012-08-02";  
		String pattern="yyyy-MM-dd";  
		SimpleDateFormat dateFormat=new SimpleDateFormat(pattern);  
		try {
			Date date=dateFormat.parse(dateStr);
			Calendar calendar=Calendar.getInstance();  
			calendar.setTime(date);  
			System.out.println(calendar.get(Calendar.YEAR));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记