get present year value to string
Asked Answered
C

10

10

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

Compose answered 5/10, 2013 at 12:53 Comment(1)
What does "not working" mean exactly?Caraway
B
26

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);
Baseler answered 5/10, 2013 at 12:54 Comment(0)
A
14
String thisYear = new SimpleDateFormat("yyyy").format(new Date());
Annalist answered 18/5, 2014 at 4:17 Comment(2)
You should probably explain a bit.Gonfanon
I use this. SimpleDateFormat uses Calendar. Format examples: SimpleDateFormatExceedingly
A
5

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();
Antigua answered 10/4, 2016 at 10:28 Comment(0)
P
3

Java 8:

java.time.Year.now();

here is the link.

Pitiful answered 25/3, 2016 at 8:52 Comment(0)
N
2

What about

Date currentDate = new Date();
String currentYear = String.valueOf(currentDate.getYear());
Nonaggression answered 5/10, 2013 at 12:54 Comment(2)
Date#getYear() is deprecated.Baseler
Also, that's year-1900.Lippe
A
2

You can also do it like this:

String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
Arzola answered 5/10, 2013 at 13:18 Comment(0)
A
1

try

Calendar now = Calendar.getInstance();
String year = String.valueOf(now.get(Calendar.YEAR));
Agapanthus answered 5/10, 2013 at 12:55 Comment(0)
C
1

Time Zone

Both the question and accepted answer ignore the question of time zone. At the time of a new year, the year number varies depending on your time zone.

Joda-Time

The bundled java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Use either Joda-Time or the new java.time package in Java 8.

Example in Joda-Time.

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = DateTime.now( timeZone );
int year = dateTime.year().get();
Caraway answered 18/5, 2014 at 7:56 Comment(0)
C
0
  //  enter code here
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy");
String year = format.format(date);
Conchitaconchobar answered 14/4, 2016 at 11:46 Comment(1)
This is a low quality answer. You don't describe how this helps solve the issue, the answer is just code that is almost identical to what the original OP has and doesn't explain how this solves the issue.Benefaction
R
0

In Java versions prior Java 8 you could also use java.sql.Date class. Convert to String and substring the year:

String currentYear = new java.sql.Date(System.currentTimeMillis()).toString().substring(0, 4);
Rightward answered 17/8, 2017 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.