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);
}
Related
I have a DatePickerDialog in my app and I want the date selected by the user to be restricted until the current date. I am comparing the date selected from the DatePickerDialog with the newDate() and if the selected date is before, I am throwing an error. Please find the code as follows.
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String visitDate = dateFormat.format(calendar.getTime());
try {
Date appointDate = dateFormat.parse(visitDate);
Date currentDate = new Date();
if(appointDate.before(currentDate)){
appointmentDateInputLayout.setError("Date selected is not within range!");
}else{
appointmentDateEditText.setText(visitDate);
appointmentDateInputLayout.setError(null);
}
} catch (ParseException e) {
e.printStackTrace();
}
appointmentDateEditText.setText(visitDate);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
But when I do this, I am getting an error for selecting today's date also. I want the user to allow the current date. What am I missing here?
Kindly help.
Look, the
Date currentDate = new Date() ;
allocates the date object at the time it was instantiated in the degree of Milliseconds so alternating the allocation and putting date object at the first of the code would give the very early time so the time that you take to choose visitdate would be eventually greater (after) so the number of Milliseconds in the currentDate will be less than number of Milliseconds in the Picked new Date
Try to compare with calendar getTime() method instead of current date.:
If(appointDate.before(calendar.getTime())
I am trying to update system time and date. Setting time works fine as i have system permissions in my app and application is signatured as system app also setting date works fine for Months Jan upto Nov, but when i select December as month it is setting month to Jan. this all is cause of i am adding 1 to month but in a case i dont add 1 it creates problem for every month. if I select Dec it is Showing Nov, if I am selecting Jan it works fine , When I select Feb it again selects Jan, If I select March it selects Feb. every time it is setting one month less than the selected one.
here is my code
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
mHour = hourOfDay;
mMinute = minutes;
Calendar c = Calendar.getInstance();
c.set(mYear, mMonth+1, mDay, mHour, mMinute);
Intent intent = new Intent();
intent.setAction(Action);
intent.putExtra("time", c.getTimeInMillis());
getContext().sendBroadcast(intent);
}
};
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
mYear = arg1;
mMonth = arg2;
mDay = arg3;
mTimePickerDialog.show();
}
};
at the other end
long when = intent.getLongExtra("time", -1);
if (DEBUG)
Log.i(TAG, "Incoming time: " + when);
if (when / 1000 < Integer.MAX_VALUE) {
SystemClock.setCurrentTimeMillis(when);
}
Time time = new Time();
TvManager tvmng = TvManager.getInstance();
TimerManager timerMgr = null;
if(tvmng!=null)
timerMgr = tvmng.getTimerManager();
time.set(when);
try {
timerMgr.setClkTime(time, true);
} catch (TvCommonException e) {
e.printStackTrace();
}
Please correct the code as mentioned below.
Calendar c = Calendar.getInstance();
c.set(mYear, mMonth, mDay, mHour, mMinute);
remove the +1 you added after the month.
When you set the month in calender than it should be between 0 to 11 when you pass month +1 and your month is 11 then it will be 11+1 =12 and calender consider it as Jan not December.
When you use the date selected for the purpose of showing to user then you have to add month+1 so it will show exact month ,
But when you pass it to calender again for the purpose of storing users selection store it as it is without adding +1 if you added it before then make it month-1 and then add to calender object.
It should work as you expected.
Also look at my solution to over come the issue.
to get formatted date to display user
public static String getDate(Calendar calendar){
String formattedDate;
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy");
formattedDate=format.format(calendar.getTime());
return formattedDate;
}
To set date to calender instance after selecting date from date picker use it like below.
DialogFragment newFragment = new DatePickerDialogFragment() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
super.onDateSet(datePicker, year, month, day);
Calender curDateInstance=Calender.getInstance();
curDateInstance.set(Calendar.YEAR, year);
curDateInstance.set(Calendar.MONTH, month);
curDateInstance.set(Calendar.DAY_OF_MONTH, day);
txtPickUpDate.setText(DateUtils.getDate(curDateInstance));
}
};
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..
}
The past few days I've been searching for ways to get a 'readable' date out of my calendarview from android 4.0. I can't manage to find a solution or example that suits my problem. I did get it in miliseconds but not in a date format.
My problem is: I have a calendarview and I want the selected date by the user, shown in logcat in a dateformat yy-mm-dd.
I was used to the datepicker from android 2.2 and I'm not familiar with calendarview and can't find much about it either. Does anyone know a solution for this?
Okay so here is how to do this. When you fire your calendarview activity or a calendarview inside your activity it sets the date to the current date(meaning today). To get this current date just use the Calendar object provided by the java api to get this date example below:
Calendar date = Calendar.getInstance();
// for your date format use
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
// set a string to format your current date
String curDate = sdf.format(date.getTime());
// print the date in your log cat
Log.d("CUR_DATE", curDate);
to get a date changed you must do this
CalendarView myCalendar = (CalendarView) findViewById(R.id.myCalenderid);
myCalendar.setOnDateChangeListener(myCalendarListener);
OnDateChangeListener myCalendarListener = new OnDateChangeListener(){
public void onSelectedDayChange(CalendarView view, int year, int month, int day){
// add one because month starts at 0
month = month + 1;
// output to log cat **not sure how to format year to two places here**
String newDate = year+"-"+month+"-"+day;
Log.d("NEW_DATE", newDate);
}
}
kandroidj's answer helps to create date, but not date of correct format.
So to format selected date:
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
String sDate = sdf.format(calendar.getTime());
Log.d(TAG, "sDate formatted: " + sDate);
}
});
You should use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String selectedDate = sdf.format(new Date(calendar.getDate()));
long date = calenderView.getDate();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
int Year = calendar.get(Calendar.YEAR);
int Month = calendar.get(Calendar.MONTH);
int Day = calendar.get(Calendar.DAY_OF_MONTH);
//customize According to Your requirement
String finalDate=Year+"/"+Month+"/"+Day;
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...