I use a DataPicker and in the method onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
I get year , monthOfYear and dayOfMonth, how I can merge this 3 int in a single Date which I can use to store in a database?
Asked
Active
Viewed 680 times
0

Slaiv206
- 309
- 2
- 14
-
possible duplicate of [Android, How can I Convert String to Date?](http://stackoverflow.com/questions/8573250/android-how-can-i-convert-string-to-date) – Jibran Khan Mar 12 '15 at 13:03
-
Have you searched on internet before posting question? There are LOT of similar questions – Apurva Mar 12 '15 at 13:35
-
If your output is int then you could use String.value Of(your int values in your format); – Jesson Atherton Mar 12 '15 at 14:11
5 Answers
0
Instantiate a calendar:
Calendar cal = Calendar.getInstance();
Use it's methods to set the date:
cal.set(int year, int month, int day);
Then either store it as a timestamp integer
cal.getTimeInMillis()
or as a String
cal.toString()

dev.bmax
- 8,998
- 3
- 30
- 41
0
You could convert it to a String
in the format dd/mm/yyyy
using SimpleDateFormatter
.
Then when you want to retrieve it, use some String
manipulation to retrieve each individual component and construct a Date
object.

StuStirling
- 15,601
- 23
- 93
- 150
0
you can do something like this
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
private void updateLabel()
{
String myFormat = "MM/dd/yy"; //In which format you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
textview.setText(sdf.format(myCalendar.getTime()));
}
Hope it helps

Umair
- 6,366
- 15
- 42
- 50
-
ok, it works, but if now I want to use this date that I have store in a database and for example execute some code when the date stored is equal to the corrent date in the device? – Slaiv206 Mar 13 '15 at 12:14
-
@Slaiv206 then you have to compare both dates . get the system date(device date) and get your date from database and compare them both and do whatever you are trying to do ... – Umair Mar 14 '15 at 06:18
0
Try this:
private SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
private void datePicker(Date initialDate) {
Calendar cal = Calendar.getInstance();
if (initialDate != null) {
cal.setTime(initialDate);
}
new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String data = String.format("%02d/%02d/%04d", dayOfMonth, monthOfYear + 1, year);
try {
Date newDate = format.parse(data);
saveMyDate(newDate);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
}
private void saveMyDate(Date newDate) {
// do something
}

Douglas Nassif Roma Junior
- 2,070
- 2
- 27
- 50
0
Use Calendar to set year, month, day
GregorianCalendar cal = new GregorianCalendar();
cal.set(GregorianCalendar.YEAR, year);
cal.set(GregorianCalendar.MONTH, monthOfYear);
cal.set(GregorianCalendar.DAY_OF_MONTH, dayOfMonth);
and store as milli seconds
cal.getTimeInMillis();

Pratik Popat
- 2,891
- 20
- 31