Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get datepicker value in date format?

I have a problem with my Datapicker

i use the code for getting date,month & year shown below

           DatePicker datePicker;
           datePicker = (DatePicker) findViewById(R.id.dateselect);

           int   day  = datePicker.getDayOfMonth();
           int   month= datePicker.getMonth() + 1;
           int   year = datePicker.getYear();

but when i print the date it shows the value 7 not 07 and for month it shows the value 2 not 02

I want these integer data in a date format ie; eg: 02-02-2013, 24-12-2013
Is there any possible way????

like image 278
Sibin Francis Avatar asked Feb 13 '13 10:02

Sibin Francis


People also ask

How do I change date format in datepicker?

In the Data type Format dialog box, do one of the following: To format the control to show the date only, select the display style that you want in the Display the date like this list. To format the control to show the time only, select the display style that you want in the Display the time like this list.

How do I change datepicker format from DD MM to YYYY?

The jQuery DatePicker plugin supports multiple Date formats and in order to set the dd/MM/yyyy Date format, the dateFormat property needs to be set. The following HTML Markup consists of a TextBox which has been made Read Only.

What is datepicker format?

DateTimePicker allows you to define the text representation of a date and time value to be displayed in the DateTimePicker control. The format specified is achieved by the dateTimeFormat property. Default value of this property is M/d/yyyy h: mm tt.


1 Answers

You can format the date like this :

int   day  = datePicker.getDayOfMonth();
int   month= datePicker.getMonth();
int   year = datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String formatedDate = sdf.format(calendar.getTime());

You can parse the String back to Date object by calling

Date date = sdf.parse(formatedDate);
like image 139
zdesam Avatar answered Oct 12 '22 02:10

zdesam