Getting string from date picker dialog fragment - android

I am using the example from the documentation.
From the method
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
I want the string "Mon, Sep 14, 2014". How do I get it? Ideally I really want "Mon, Sep 14, 2014 10:30PM" but I can't find a combined date and time picker.

Here ya go date time combined.
This is no meant to be mean just that you said you were having trouble finding one.
how to find it : http://bit.ly/1doSxEf
Which one I use:
https://github.com/bendemboski/DateSlider

// try this way
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
Time chosenDate = new Time();
chosenDate.set(dayOfMonth, monthOfYear, year);
long dt = chosenDate.toMillis(true);
CharSequence strDate = DateFormat.format("KKK MMM dd,yyyy", dt);
// mow use strDate..
}

Related

Displaying a "day-of-week" dialog in android

I'm making an app that requires the user to pick a time and day of week for a recurring event. I've already implemented a TimePicker dialog, but I can't seem to find one for picking the day of the week. I've triedDatePicker, but that's only for day of month. What can I do?
I you want the day of week you will have use SimpleDateFormat and use EEEE
for example.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay)
{
SimpleDateFormat dateformat = new SimpleDateFormat("EEEE");
Date date = new Date(selectedYear, selectedMonth, selectedDay-1);
String dayOfWeek = dateformat.format(date);
}

Date format issue in android when locale is arabic

I have a serious problem here, I am building an app that will work on Arabic devices, and I need to send dates to the server, I am using Android DatePickerDialog to get the date, but the date always sent with Arabic characters, and when i try to display it again it gives me Unparsable date exception
I have tried the following solutions but no results
mDateTime = Calendar.getInstance(Locale.US).getTime();
mDateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));
but non of them worked for me
any help please.
My date picker dialog code is like following
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
private TextView mUserView;
private Date mAffectedDate;
private SimpleDateFormat mDateFormater;
private Date mInitialDate;
public TextView getUserView() {
return mUserView;
}
public void setUserView(TextView userView) {
this.mUserView = userView;
}
public Date getAffectedDate() {
return mAffectedDate;
}
public void setAffectedDate(Date mAffectedDate) {
this.mAffectedDate = mAffectedDate;
}
public SimpleDateFormat getDateFormater() {
return mDateFormater;
}
public void setDateFormater(SimpleDateFormat mDateFormater) {
this.mDateFormater = mDateFormater;
}
public Date getInitialDate() {
return mInitialDate;
}
public void setInitialDate(Date mInitialDate) {
this.mInitialDate = mInitialDate;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance(Locale.US);
c.setTime(mInitialDate);
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) {
Date date = new Date();
date.setYear(year - 1900);
date.setMonth(month);
date.setDate(day);
mAffectedDate.setYear(year - 1900);
mAffectedDate.setMonth(month);
mAffectedDate.setDate(day);
mUserView.setText(mDateFormater.format(date));
}
}
You can use:
Calendar.set(int year, int month, int day, int hourOfDay, int minute, int second)
Sets the year, month, day of the month, hour of day, minute, and second fields. Other fields are not
changed; call clear first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.
Like this:
Calendar cal = Calendar.getInstance(Locale.US);
public void onDateSet(DatePicker view, int year, int month, int day) {
cal.set(year, month, day, 0, 0, 0);
// get time in milliseconds
Long timeInmilliseconds = cal.getTimeInMillis();
// print time
Log.v("log", "Date "+ new Date(cal.getTimeInMillis()));
}
Also see this:
A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developers test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale.
For example, if you are formatting integers some locales will use non-ASCII decimal digits. As another example, if you are formatting floating-point numbers some locales will use ',' as the decimal point and '.' for digit grouping. That is correct for human-readable output, but likely to cause problems if presented to another computer (parseDouble(String) cannnot parse such a number, for example).

Get long timestamp from date picker onDateSet

#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateET.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
}
I want to avoid user setting date past to current date.
So, is there a way i can get the long timestamp (may be 12:00 A.M of the date set), so that i can compare it with the current timestamp and if it is less, the date will be set to default(current date + 3 days).
Thank You
You have to do it yourself:
Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth);
calendar.getTimeInMillis();

Android: CalendarView onDateChangelistener Milliseconds value to DatePicker Milliseconds value

I can't seem to correctly compare the two milleseconds value from teh CalendarView using cw.getDate() method
and the Calendars c.set() and c.getTimeInMillis inside the OnDateListner of The DatePicker
Calendar c = Calendar.getInstance();
OnDateSetListener datePickerListener = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mDay = dayOfMonth;
mMonth = monthOfYear;
c.set(mYear, mMonth, mDay);
dateView.setText(formatDate(c.getTimeInMillis()));
dateViewMilli.setText(Long.toString(c.getTimeInMillis()));
}
};
Here is the picture for difference
So how do i get the Milliseconds in DatePicker wherein it is completely the same value in CalendarView?.. cuz i can't query perfectly the values of the two cuz they have different long value.. i mean both timestamp are different despite it having same day, month, year... dunno wat happend.. maybe its on the parsing or wat.. for those any of you who have some knowledge about this please do share
UPDATE
I have just read some article, and a friend also told me that both produces different timestamp values, thus best thing to do is to fomrat the timestamp first, say MM/DD/YYYY, then store is as a string den that's the time you compare..
Any other inputs are welcome also...

Android: Date Picker Should not accept current date and future dates

How to restrict date picker from accepting current and future dates in android i am using google api...any idea..?
Since API level 11 there is a method for that:
DatePicker.setMaxDate(long maxDate)
If it has to work in previous versions, use this method:
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
You could pass your own OnDateChangedListener which "resets" invalid dates to the newest valid one:
DatePicker picker = ...
int year = ...
int monthOfYear = ...
int dayOfMonth = ...
picker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// check if current date is OK
boolean dateOk = ...
if (!dateOk) {
// correct the date, but be sure the corrected date is OK
// => otherwise you might get endless recursion
year = ...
monthOfYear = ...
dayOfMonth = ...
// update the date widget with the corrected date values
view.updateDate(year, monthOfYear, dayOfMonth);
}
}
});

Categories

Resources