Reputation: 305
I have an app in which a user sets a date and time using a DatePicker and a TimePicker. I am trying to save the information as a timestamp. I am currently getting the timestamp based on the date picked by the user (see code below):
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
final Calendar c = cal.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
DatePickerDialog datePickerDialog = new DatePickerDialog(AddEventActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
txtDate.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
c.set(year, monthOfYear, dayOfMonth);
timestamp = c.getTimeInMillis();
}
This works fine, but since I don't give a time, it sets the time to the time I create the item. For example, say I set an event on 27/4/2019 at 19:00, and save it at 13:30, the timestamp recorded reads 27/4/2019 at 13:30.
The code for my TimePicker is below:
txtTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddEventActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
//adding extra 0 to the front of 1 digit hours and minutes so format is consistent.
String hourFormat = String.format("%02d", selectedHour);
String minFormat = String.format("%02d", selectedMinute);
txtTime.setText(hourFormat + ":" + minFormat);
}
}, hour, minute, true);
mTimePicker.show();
}
});
Is there a way to combine the date and time selected to create an accurate timestamp?
Upvotes: 1
Views: 6487
Reputation: 56
Timestamp contains the date and time you selected from Datepicker and Timepicker
timepicker = (Timepicker)findViewById(R.id.timepicker);
datepicker = (Datepicker)findViewById(R.id.datepicker);
int year = datepicker.getYear();
int month = datepicker.getMonth();
int day = datepicker.getDay();
int hour= timepicker.getCurrentHour();
int minute = timepicker.getCurrentMinute();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
Long timestamp = calendar.getTimeInMills();
System.out.println(timestamp);
Upvotes: 1
Reputation: 529
Have a class level Calendar instance and let's instantiate it the way you did it earlier:
Calendar combinedCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
So when you select date, set value as you do:
combinedCal.set(year, monthOfYear, dayOfMonth);
When you select time, just set selected hours and minutes to the same instance:
combinedCal.set(Calendar.HOUR_OF_DAY, selectedHour);
combinedCal.set(Calendar.MINUTE, selectedMinute);
Upvotes: 4
Reputation: 154
i had a similar issue while i was fetching dates so what i did was create a string which i used to pick the current date and time the merge it to create a perfect a time stamp like so
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(ServiceManDetails.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String month,day;
if(monthOfYear<9){
month="0"+ (monthOfYear + 1);
if(dayOfMonth<10){
day="0"+dayOfMonth;
}else{
day=dayOfMonth+"";
}
}else{
month= (monthOfYear + 1)+"";
if(dayOfMonth<10){
day="0"+dayOfMonth;
}else{
day=dayOfMonth+"";
}
}
//String selectedday=dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
pickedDate=year + "/" + month + "/" + day;
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(ServiceManDetails.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
String hr,min;
if(hourOfDay<10){
hr="0"+hourOfDay;
if(minute<10){
min="0"+minute;
}else{
min=minute+"";
}
}else{
hr=hourOfDay+"";
if(minute<10){
min="0"+minute;
}else{
min=minute+"";
}
}
time=hr + ":" + min + ":00";
date=pickedDate+" "+time;
scheduleDialog(date);
}
}, mHour, mMinute, true);
timePickerDialog.show();
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
also i dint want to set miliseconds so i set the date automatically set the seconds as 00 you can also add milliseconds as 000
Upvotes: 0