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();
}
Related
How to get duration between selected time (using graphical date and time picker) and date and time now in seconds?
Both methods getSelectedDate() and getSelectedTime() look fine, selected date and time is fine. But there is something wrong with the method getCountDuration(). The value is incorrect. And I can't find an error.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
int YEAR = 0, DATE = 0, MONTH = 0, HOUR = 0, MINUTE = 0;
Button dateButton;
TextView dateTextView, timeTextView, textViewDateAndTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateButton = findViewById(R.id.dateButton);
dateTextView = findViewById(R.id.dateTextView);
timeTextView = findViewById(R.id.timeTextView);
textViewDateAndTime = findViewById(R.id.textViewDateTimeDuration);
dateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getSelectData();
getSelectTime();
getCountDuration();
}
});
}
public LocalDate getSelectData() {
final Calendar calendarDate = Calendar.getInstance();
YEAR = calendarDate.get(Calendar.YEAR);
MONTH = calendarDate.get(Calendar.MONTH);
DATE = calendarDate.get(Calendar.DATE);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int date) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, year);
calendar1.set(Calendar.MONTH, month);
calendar1.set(Calendar.DATE, date);
String dateText = DateFormat.format("dd.MM.yyyy", calendar1).toString();
dateTextView.setText(dateText);
}
}, YEAR, MONTH, DATE);
datePickerDialog.show();
return null;
}
public LocalTime getSelectTime() {
Calendar calendar = Calendar.getInstance();
final LocalDateTime nowIs = LocalDateTime.now();
Date currentDate = new Date();
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");//("dd.MM.yyyy HH:mm"
final LocalDateTime dateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
HOUR = calendar.get(Calendar.HOUR);
MINUTE = calendar.get(Calendar.MINUTE);
boolean is24HourFormat = DateFormat.is24HourFormat(this);
final TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
Log.i(TAG, "onTimeSet: " + hour + minute);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR, hour);
calendar1.set(Calendar.MINUTE, minute);
String dateText = DateFormat.format("hh:mm", calendar1).toString();
timeTextView.setText(dateText);
String dateNowText = formatter.format(dateTime).toString();
}
}, HOUR, MINUTE, is24HourFormat);
timePickerDialog.show();
return null;
}
public void getCountDuration() {
final Calendar setDate = Calendar.getInstance();
setDate.set(YEAR, MONTH, DATE, HOUR, MINUTE, 0);
long subs = Calendar.getInstance().getTimeInMillis() - setDate.getTimeInMillis();
textViewDateAndTime.setText(subs + " duration in milliseconds");
}
}
I have two date picker From and To, here i can set From datepicker value as minimum date of to datepicker.
Now my requirement is ,if i select To datepicker date, i want to set it as maximum date of From datepicker date. I tried one solution, but its not working in my case.
private String fromDate;
private DatePickerDialog fromDatePicker;
private DatePickerDialog.OnDateSetListener fromDatePickerListener;
private Calendar fromDateCalendar;
private String toDate;
private DatePickerDialog toDatePicker;
private DatePickerDialog.OnDateSetListener toDatePickerListener;
private Calendar toDateCalendar;
Fromdatepicker OnDateSetListener method
fromDateCalendar = Calendar.getInstance();
fromDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
fromDateCalendar.set(Calendar.YEAR, year);
fromDateCalendar.set(Calendar.MONTH, monthOfYear);
fromDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
fromDate = fromDateCalendar.get(Calendar.YEAR) + "-" +
(fromDateCalendar.get(Calendar.MONTH)+1) + "-" +
fromDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewFrom.setText(fromDate);
toDatePicker.getDatePicker().setMinDate(fromDateCalendar.getTimeInMillis());
}
};
To datepicker OnDateSetListener method
toDateCalendar = Calendar.getInstance();
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
textviewTo.setText(toDate);
todatepicker.getDatePicker().setMinDate(-2208902400000L);
fromdatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());
}
};
toDatePicker = new DatePickerDialog(getContext(),toDatePickerListener,
toDateCalendar.get(Calendar.YEAR), toDateCalendar.get(Calendar.MONTH),
toDateCalendar.get(Calendar.DAY_OF_MONTH));
fromDatePicker = new DatePickerDialog(getContext(),fromDatePickerListener,
fromDateCalendar.get(Calendar.YEAR), fromDateCalendar.get(Calendar.MONTH),
fromDateCalendar.get(Calendar.DAY_OF_MONTH));
Textview listner for call datepicker dialog
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fromDatePicker.show();
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toDatePicker.show();
}
});
I tried
fromDatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());
in onDateSet method of toDatePickerListener, but its not
working
I have tried the following way,Hope it may help you.
private String selectedToDate = "";
private String selectedFromDate = "";
private Calendar calToDate, calFromDate;
Write this code in onCreate() method
calFromDate = convertToCalender(selectedFromDate);
calToDate = convertToCalender(selectedToDate);
Todate and FromDate TextView Clicks
textviewFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showFromDatePicker(fromdateListener, calFromDate, calToDate);
}
});
textviewTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showToDatePicker(todateListener, calToDate, calFromDate);
}
});
toDate and fromDate Listener
public DatePickerDialog.OnDateSetListener fromdateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calFromDate.set(year, monthOfYear, dayOfMonth);
selectedFromDate = returnFormattedDate(calFromDate.getTimeInMillis());
textviewFrom.setText(selectedFromDate);
}
};
DatePickerDialog.OnDateSetListener todateListener =
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calToDate.set(year, monthOfYear, dayOfMonth);
selectedToDate = returnFormattedDate(calToDate.getTimeInMillis());
textviewTo.setText(selectedToDate);
}
};
private String returnFormattedDate(long timeInMilliSec) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
return dateFormat.format(new Date(timeInMilliSec));
}
Methods to show DatePicker
private void showFromDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar minDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
if (minDateCalender != null) {
dialog.getDatePicker().setMinDate(minDateCalender.getTimeInMillis());
}
dialog.show();
}
private void showToDatePicker(DatePickerDialog.OnDateSetListener listener,
Calendar calendar,
Calendar maxDateCalender) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(),
listener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMaxDate(maxDateCalender.getTimeInMillis());
dialog.show();
}
Method to convert string date to Calender
private Calendar convertToCalender(String date) {
Calendar mCalender = Calendar.getInstance();
if (date == null) {
return mCalender;
} else {
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date returnDate = formatter.parse(date);
mCalender = Calendar.getInstance();
mCalender.setTime(returnDate);
return mCalender;
} catch (ParseException e) {
e.printStackTrace();
return mCalender;
}
}
}
If you are are using DatePicker widget in xml the following will work .
toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
toDateCalendar.set(Calendar.YEAR, year);
toDateCalendar.set(Calendar.MONTH, monthOfYear);
toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
(toDateCalendar.get(Calendar.MONTH)+1) + "-" +
toDateCalendar.get(Calendar.DAY_OF_MONTH);
fromdatePicker.setMaxDate(toDateCalendar.getTimeInMillis());
//etToDate.setText(toDate);
}
};
Or if you opening DatePickerDialog oen after another . Then you should save the previously selected date and pass it to next DatePickerDialog before calling show.
pickerDialog.getDatePicker().setMaxDate(maxTime);// Set Max and min here
pickerDialog.show();
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
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();
Hey guys i am working on an application that can be used to predict the due date of a babies birth but i have failed to figure out how to increment the date from the date time picker dialog below is my code
public class Dday extends Activity implements OnClickListener{
private EditText fromDateEtxt;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dday);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
findViewsById();
setDateTimeField();
}
private void findViewsById() {
fromDateEtxt = (EditText) findViewById(R.id.etxt_fromdate);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
}
private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dday, menu);
return true;
}
public void showDate (View view)
{
// Adding the days to the date
// Button action
Intent dts = new Intent(this,Details.class);
startActivity(dts);
}
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
}
}
You can use the Calendar Object and create a date from Today and add 39 weeks using the Calendar class by setting Date objects based on the Calendar and modified Calendar like so:
// Formatter for readable comparison
DateFormat sdf = new SimpleDateFormat("MM dd yyyy", Locale.getDefault());
// Today
Calendar now = Calendar.getInstance();
Date dateToday = new Date();
dateToday.setTime(now.getTimeInMillis());
// 39 weeks from today
Date futureDate = new Date();
// Calendar supports roll over with year so no need to worry about modifying year separate
now.add(Calendar.WEEK_OF_YEAR, 39);
futureDate.setTime(now.getTimeInMillis());
// Print out for comparison
System.out.println(sdf.format(dateToday));
System.out.println(sdf.format(futureDate));
Which prints out:
I/System.out﹕ 09 16 2015 <-- Today
I/System.out﹕ 06 15 2016 <-- 39 weeks later
You can increment a Calendar object by 39 weeks with something like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_YEAR, 39);
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
Calendar c = Calendar.getInstance();
c.set(selectedYear, selectedMonth, selectedDay);
c.add(Calendar.DATE, 39);
}
for more see this thread How do I set the date for a date picker?