2015-10-26 77 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);