Question

I have my date in this format yyyy-mm-dd and I save the date in this format as a String. Now I have the String and I want to know how can I get yyyy, mm or dd in 3 different String.

Was it helpful?

Solution

If you know for sure that every single time it will be in that same format you can do a string split on that string, and get the values from the resulting array.

String date = "2011-06-15";

String[] dates = date.split("-");

String year = dates[0];
String month = dates[1];
String day = dates[2];

This won't work if your date ends up in a different format.

OTHER TIPS

data = "2011-06-15";
String[] strings = data.split("-");
// strings[0] = "2011"
// strings[1] = "06"
// strings[2] = "15"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top