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

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();

Related

How can I set the specific selected date to min date in android date picker? (java)

I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.
I set the Data which user selected in Calander startDate and set this data as MinDate at end date.
I've tried in this way but it's not working :(
I can't use Mateiral Range Date Picker because I have to use specific theme.
Also I'm not good at programming so I'd appreciate it if you could explain it in detail.
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
final Calendar c = Calendar.getInstance();
startYear = c.get(Calendar.YEAR);
startMonth = c.get(Calendar.MONTH);
startDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month+1;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
}
}, startYear, startMonth, startDay);
dpd.show();
}
});
Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
endYear = c.get(Calendar.YEAR);
endMonth = c.get(Calendar.MONTH);
endDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
endYear = year;
endMonth = month;
endDay = dayOfMonth;
ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
}
}, endYear,endMonth, endDay);
dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
dpd.show();
}
});
This code:
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
Should be inside
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()
So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog

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();
}

Change Month in name instead of number in DatePickerDialog

I have a DatePickerDialog, which is showing everything correct and also selecting the correct value. But I want to set the Month name Ex. "Jan" instead of M01.
Please see the below Image:
https://drive.google.com/file/d/1mhWPCPkARY4I1dvYZ6RdrXueUhHvMa4b/view
See the code:
public void setDueDate() {
if (mYear == 0) {
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(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, final int year,
final int monthOfYear, final int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String strDate = mYear + "-" + (mMonth + 1) + "-" + mDay;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(strDate);
tvDueDate.setText(format.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
datePickerDialog.setTitle("");
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

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