java-从date中获取年份

时间:2025-01-25 07:53:41

代码

package testJava;

import ;
import ;
import ;
import ;

public class GetYearForDate {

    public static void main(String[] args) {
	// TODO Auto-generated method stub
	("由时间中获取年");
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:", );// 设定格式
	String sToTimestamp = "2005-8-18 14:21:12.123";
	try {
	     timeDate = (sToTimestamp);
	    Calendar calendar = ();
	    (timeDate);	
	    int year = ();
	    ("year:"+year);
	} catch (Exception e) {
	    (());
	}

    }

}

运行结果

由时间中获取年
year:2005