Android - How to limit date time picker to only every X minutes? - java

I am storing date and time but I need to have it limited to only be allowed to pick intervals of 30 minutes when the time picker is displayed so I can't be able for example to pick time 06:11 .
Here is my code that I have.
private void showDateTimeDialog(final EditText date_time_in) {
final Calendar calendar=Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
//format of the calender
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm");
//add the booking in Firebase
date_time_in.setText(simpleDateFormat.format(calendar.getTime()));
Bookings booking = new Bookings(year,month+1,dayOfMonth,hourOfDay,minute);
rootDatabaseRef.push().setValue(booking);
Toast.makeText(BookingActivity.this, "Appointment successful ", Toast.LENGTH_SHORT).show();
date_time_in.setText(getString(R.string.select_a_date_and_timeText));
}
};
new TimePickerDialog(BookingActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
};
new DatePickerDialog(BookingActivity.this,dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
What changes do I need to make to limit to 30 minute intervals ?

Related

How to set restrictions for DatePicker and TimePicker in Android

I want to set restrictions on the Datepicker so that it will allow the user to only choose the date 7 days from now. I also want to set restrictions on the Timepicker so that the user can only choose the time between 11:00 am and 6:00 pm. Here is my Code for the DatePicker and TimePicker.
private void showTimeDialog(final EditText timeInput) {
final Calendar calendar = Calendar.getInstance();
TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
timeInput.setText(enteredTime.format(calendar.getTime()));
time = enteredTime.format(calendar.getTime());
}
};
new TimePickerDialog(OrderPickupActivity.this, timeSetListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false).show();
}
private void showDateDialog(final EditText dateInput) {
final Calendar calendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
dateInput.setText(enteredDate.format(calendar.getTime()));
date = enteredDate.format(calendar.getTime());
}
};
new DatePickerDialog(OrderPickupActivity.this, dateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
Can you please help me out?
private void showDateDialog(final EditText dateInput) {
final Calendar calendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
dateInput.setText(enteredDate.format(calendar.getTime()));
date = enteredDate.format(calendar.getTime());
}
};
DatePickerDialog datePickerDialog = new
DatePickerDialog(OrderPickupActivity.this, dateSetListener,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 7); // Adding 7 days
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
datePickerDialog.show();
}

how i can show traditional calendar?

I want to snow the traditional(Before material design) calendar in my application : counter calendar
I use the following code and this is the output
enter image description here
my code is :
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Log.e("Result is :", String.valueOf(year + month + day));
}
i need to show this calendar .. how i can ?
enter image description here

Setting "today date" in DatePicker by timezone (Android)

I am trying to set a datepicker by timezone specified by the app. I have succeed somewhat in applying the current selection and minDates but I can't get today date (bold number on date picker dialogue) to change. I have uploaded a picture which shows datepicker using getDefult timezone to set today date instead of specified timezone.
How can I specify today date?
billDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
billDateDialog.show();
billDateDialog.updateDate(billDateCal.get(Calendar.YEAR), billDateCal.get(Calendar.MONTH), billDateCal.get(Calendar.DAY_OF_MONTH));
}
});
billDateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
long correctedTime = new DayLightSavings(BillsAdd.this, year, monthOfYear, dayOfMonth, timeZone).getCorrectedTime();
billDateCal.setTimeInMillis(correctedTime);
billDate.setText(dateFormatter.format(billDateCal.getTimeInMillis()));
}
},billDateCal.get(Calendar.YEAR), billDateCal.get(Calendar.MONTH), billDateCal.get(Calendar.DAY_OF_MONTH));
long oneDay = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
long oldRawOffset = TimeZone.getDefault().getRawOffset();
long newRawOffset = timeZone.getRawOffset();
long rawOffset = oldRawOffset - newRawOffset;
billDateDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() + oneDay - rawOffset);
You can check this code.This might be helpful for you...
//Create calendar instance
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
final DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
tvStartdate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
tvStartdate.setTextColor(Color.BLACK);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = sdf.format(new Date(year - 1900, monthOfYear, dayOfMonth));
sd = formatedDate;
}
}, year, month, day);
dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dpd.show();

Android TimePicker not giving the desired result

Hello I'm trying to calculate user's sleeping hours of 7.5.the app should suggest that the user should be at bed at an exact time. For example, she chooses 7:00 Am as her waking time, the app will suggest that by 11:30 Am she should be going to bed What I tried so far is this:
public class TimepickerActivity extends Activity implements TimePickerDialog.OnTimeSetListener {
Button btn1, btn2;
TimePicker picker;
TextView tv;
private int hour;
private int minute;
static final int TIME_DIALOG_ID = 999;
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_timepicker );
btn1 = (Button) findViewById (R.id.btnShow);
btn2 = (Button) findViewById (R.id.btnSleepingTime);
tv = (TextView) findViewById (R.id.textView1);
picker = (TimePicker) findViewById (R.id.timePicker1);
setCurrentTimeOnView();
addListenerOnButton();
}
// display current time
public void setCurrentTimeOnView() {
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
tv.setText( new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
// set current time into timepicker
picker.setCurrentHour(hour);
picker.setCurrentMinute(minute);
}
public void addListenerOnButton() {
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this,
timePickerListener, hour, minute,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, - selectedHour);
c.add(Calendar.MINUTE, - selectedMinute);
// set current time into timepicker
picker.setCurrentHour(hour + 7);
picker.setCurrentMinute(minute + 30);
// set current time into textview
//tv.setText(new StringBuilder().append(pad(hour))
// .append(":").append(pad(minute)));
tv.setText( picker.getCurrentHour().toString() + ":" + picker.getCurrentMinute().toString() );
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
}
}
When I input 7:00 am as my waking time, the result is giving me 2:30 pm. But it should be 12:30 pm. Any help is appreciated thanks.
No, it's correct. If you input 7:00am + 7.5 hours = 14:30 = 2:30 am. The program is working correctly.
EDIT: you almost did it:
public class TimepickerActivity extends Activity implements TimePickerDialog.OnTimeSetListener {
private static final int SUB_HOUR = -7;
private static final int SUB_MINUTE = -30;
[...]
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
// Gets current time
Calendar c = Calendar.getInstance();
// Assign hour set in the picker
c.set(Calendar.HOUR, selectedHour)
c.set(Calendar.MINUTE, selectedMinute);
// Have Calendar calculate the substraction of hours and minutes
c.add(Calendar.HOUR, SUB_HOUR);
c.add(Calendar.MINUTE, SUB_MINUTE);
// Get the hour and the minute calculated
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
[...]
Of course this means this previous assignment:
hour = selectedHour;
minute = selectedMinute;
is now useless and you can remove it.
By the way, you didn't take into account the day, e.g. choosing 1:30 am would return 6:00 pm but for the day before.
EDIT: I corrected my code.

date picker display problem in android

in my app i am trying to show a date picker in OnTouchEvent. When the date picker dialog appears it shows the Month in text such as May, June, July etc. When i select one among them it displays in numbers. I want it to be viewed as text itself. How to get it.....
Following is the part of my code
bd =(EditText)findViewById(R.id.dob);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
private OnTouchListener bdListener = new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
showDialog(DATE_DIALOG_ID);
return false;
}
};
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private void updateDisplay()
{
bd.setText(new StringBuilder().append(mMonth+1).append("-").append(mDay).append("-").append(mYear));
}
So you have the month as an integer and you want the string representation.
I think you need DateFormatSymbols
monthText = new DateFormatSymbols().getMonths()[month];

Categories

Resources