0%

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();
        }