I need to get the present year value in string so I did:
Calendar now = Calendar.getInstance();
DateFormat date = new SimpleDateFormat("yyyy");
String year = date.format(now);
It works on ubuntu but it's not working on windows 7.
Do you know why? Is there a safer way to do that?
Thanks
You can simple get the year from Calendar
instance using Calendar#get(int field)
method:
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
String yearInString = String.valueOf(year);
String thisYear = new SimpleDateFormat("yyyy").format(new Date());
In Java 8 there's a collection called java.time
in which you easily can obtain the current year from your computer's clock.
To get the current year as an integer you can simply write:
int thisYear = Year.now().getValue();
To get it as a String:
String thisYear = Year.now().toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With