getdate from datepicker android
Solution 1
I use this:
/**
*
* @param datePicker
* @return a java.util.Date
*/
public static java.util.Date getDateFromDatePicker(DatePicker datePicker){
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar.getTime();
}
Solution 2
You should use SimpleDateFormat.format
to convert that Date
object to a String
, like this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = sdf.format(date1);
I think you just had it backwards. You need to use format
to go from Date to String, and parse
to go from a String to a Date.
Solution 3
I know this is old but i struggled to find it. So for posterity
Scenario: A View needs the date in yyyy-mm-dd
date_field = (EditText)findViewById(R.id.input_date);
date_field.setFocusable(false); // disable editing of this field
date_field.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
chooseDate();
}
});
private void chooseDate() {
final Calendar calendar = Calendar.getInstance();
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker =
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(final DatePicker view, final int year, final int month,
final int dayOfMonth) {
@SuppressLint("SimpleDateFormat")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
calendar.set(year, month, dayOfMonth);
String dateString = sdf.format(calendar.getTime());
date_field.setText(dateString); // set the date
}
}, year, month, day); // set date picker to current date
datePicker.show();
datePicker.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(final DialogInterface dialog) {
dialog.dismiss();
}
});
}
Hope this helps someone
Aymen Taarit
Web developer : Java EE (jpa,hibernate,jax-rs,jaxb,soap,jms),C ,C++,PHP ,HTML5 Javascript : Jquery(mobile,ui) Ajax,Cometd ,Dojo Toolkit Mobile : Android (native),Hybrid(Phonegap) IDE: IBM rational applcation developer,Eclipse,Netbeans Servers : WAS 8,Apache Tomcat
Updated on July 09, 2021Comments
-
Aymen Taarit almost 3 years
I want to get date from datepicker widget in android I have tried with this
Date date1= (Date) new Date (dpBirthDate.getYear(), dpBirthDate.getMonth(), dpBirthDate.getDayOfMonth()); date1 = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(date1.toString());
But I get a date like this
mon 7 dec 2011 time
... and all I want to get is theyyyy-MM-dd
format to store it in the database.I tried also to concat the year-month-day like this but the problem is for example today 2011-12-7 the day should be 07 to be valid
Could you help me please.
-
Aymen Taarit over 12 yearschanged the code like this Date date1= (Date) new Date(dpBirthDate.getYear(), dpBirthDate.getMonth(), dpBirthDate.getDayOfMonth()); dates =new SimpleDateFormat("yyyy-MM-dd").format(date1); and what I got 3911-12-07 whats wrong
-
skynet over 12 yearsWell then check what the year of the Date object is before you pass it in
-
Aymen Taarit over 12 yearshow can I fix the Date format please help ! I always get the date strats with 3911
-
skynet over 12 yearsThen you are creating the Date object incorrectly. As I said, you should debug what the value actually is. If you look at the documentation of
Date
you see that the year you pass in must be "the year minus 1900", so that is probably your error. Java'sDate
object has to be handled with care. -
Aymen Taarit over 12 yearsthank you now its clear I should do 3911 -1900 to get the proper year 2011
-
Chris over 10 yearsThis largely worked for me, except I needed to define the DatePicker as a android.widget.DatePicker instead. Thanks :)
-
lcnicolau over 5 yearsIf you are using Kotlin, you can do it even in a more elegant way.
-
Naimul Kabir over 2 yearsThanks a lot, man.