Gen
Gen

Reputation: 2540

How to take only year from Date in string format?

I will get Date in string format like 11/01/2017. From that string I want to take only the year. Like

String stringDate="11/01/2017";

the output I want is just "2017";

How to split that string?

Thanks in advance.

Upvotes: 1

Views: 115

Answers (3)

pringi
pringi

Reputation: 4672

If you have a String and will always be dd/mm/yyyy, then you can simply do:

dateString.substring(6)

Upvotes: 2

kaushik
kaushik

Reputation: 312

You can use this as well.

String input="11/1/2017";
String year=input .substring(input .length()-4);

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521997

One option would be to split on forward slash and retain the third part:

String input = "11/01/2017";
String year = input.split("/")[2];

Upvotes: 3

Related Questions