olash12345 olash12345 - 12 days ago 5
Android Question

How to get day,month,year from String date?

I have a date on String, like this:

String date="25.07.2007";


And I want to divide it to:

String day;
String month;
String year;


What is the best way to do this?

Answer

One way to split a string in Java is to use .split("regex") which splits a string according to the pattern provided, returning a String array.

String [] dateParts = date.split(".");
String day = dateParts[0];
String month = dateParts[1];
String year = dateParts[2];

To get current date: You can also change the format of the date by changing the values passed to SimpleDateFormat. Don't forget the imports.

  DateFormat dateFormater = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
  Date date = new Date();