2015-10-26 21 views
-1

我想今年转换转换SimpleDateFormat的一年整数

SimpleDateFormat actualYear = new SimpleDateFormat("yyyy");

在整型变量。

谢谢你!

+0

是否要将'actualYear'转换为整数?你有没有尝试'Integer.parseInt(actualYear);'? –

+0

这是说“方法parseInt没有任何方法接收简单的日期格式” –

回答

2

如果你想从year string,这是yyyy格式int year

int year = Integer.parseInt("2014"); 

如果你想从current yearint year

int year = Calendar.getInstance().get(Calendar.YEAR); 

如果您想从date object得到int year

String yearString = new SimpleDateFormat("yyyy").format(date); 
int year = Integer.parseInt(yearString); 
1

SimpleDateFormat对象只是格式化Date对象到字符串中。除非您要求格式化日期,否则它实际上并没有意识到日期。下面的例子将打印一年2015

SimpleDateFormat format = new SimpleDateFormat("yyyy"); 
String formattedDate = format.format(new Date()); 
int year = Integer.parseInt(formattedDate); 
System.out.println(year);