Rahul Patil
Rahul Patil

Reputation: 2727

Date , String , format java , android datepicker

I have a String which contains date in the format dd/mm/yyyy. For example lastDate = "1/2/2012" -> I get this date from date picker dialog. Now I have questions for this . I need to add 0 infront of date or month , if it is single digit . after the operation my date should be 20120201 ,any how I have done removing the delimeters and reversing the string , but I am stuck up , In adding 0 for day or month , if they are less than 10 , or single digit.

Upvotes: 2

Views: 1178

Answers (2)

binW
binW

Reputation: 13712

You can use @NikitaBeloglazov method if you are using DatePicker. But in other situations, where you have a date as string, you can use SimpleDateFormat class to do the parsing and formatting for you. For details and tutorial plz check http://www.xyzws.com/javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142

Upvotes: 1

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15419

If you use DatePicker then you get year, month and day as ints. So to build desired string you can use String.format:

String.format("%d%02d%02d", year, month, day);

Upvotes: 2

Related Questions