一个较简单的问题!
有一个string字符串
将这个字符串转换为日期格式
并提取出年度,月份
怎么实现?谢谢!
------解决方案--------------------String str = "2007-08-21 ";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd ");
Date date = sdf.parse(str);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.MONTH) + 1);
------解决方案--------------------import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class DateTest {
public static void main(String args[]) throws Exception {
String strDate = "2007-09-09 ";
if (strDate == null || strDate.equals( " ")) return;
SimpleDateFormat formatter =new SimpleDateFormat( "yyyy-MM-dd ");
Date date = formatter.parse(strDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String date_now=calendar.get(calendar.YEAR)+ "年 "+(calendar.get(calendar.MONTH)+1)+ "月 "+calendar.get(calendar.DATE)+ "日 ";
System.out.println(date_now);
}
}