Reputation: 585
I want to convert this year
SimpleDateFormat actualYear = new SimpleDateFormat("yyyy");
in an Integer variable.
Thank u!!
Upvotes: 0
Views: 10685
Reputation: 10299
If you want to get int year
from year string
, which is in yyyy format,
int year = Integer.parseInt("2014");
If you want to get int year
from current year
,
int year = Calendar.getInstance().get(Calendar.YEAR);
If you want to get int year
from date object
,
String yearString = new SimpleDateFormat("yyyy").format(date);
int year = Integer.parseInt(yearString);
Upvotes: 5
Reputation: 11413
The SimpleDateFormat
object just formats Date
objects into strings. It is not actually aware of the date till you ask for it to format a date. The below example will print the year 2015
SimpleDateFormat format = new SimpleDateFormat("yyyy");
String formattedDate = format.format(new Date());
int year = Integer.parseInt(formattedDate);
System.out.println(year);
Upvotes: 1