I'm trying to get the actual month from the Calendar using the following:
Calendar c = Calendar.getInstance();
String time = String.valueOf(c.get(Calendar.MONTH));
According the system settings "Settings --> Date & Time" actual month is 10 while get(Calendar.MONTH) returns 09.
Keep in mind that months values start from 0, so October is actually month number 9.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#MONTH
Calendar.MONTH returns month which is zero based that is why it is giving 1 less than actual month
Add 1 to get correct value
String time = String.valueOf(c.get(Calendar.MONTH)+1);
Calendar.MONTH
returns
0 for 1st month (jan)
1 for 2nd month (feb)
.
.
11 for 12th month (dec)
Docs
So change your code to
String time = String.valueOf(c.get(Calendar.MONTH)+1);// added 1
Calendar.MONTH value starts from 0 to 11 not 1 to 12.
You may check the value of Calendar.JANUARY is 0 not 1.
Refer: http://developer.android.com/reference/java/util/Calendar.html#JANUARY
I suggest trying
#Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(), month+1 + "/" + day + "/" + year, Toast.LENGTH_SHORT).show();
calDate = month+1 + "/" + day + "/" + year; }
});
use this
public void DateDialog(){
DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
int Month;
Month=monthOfYear+1;
fromDate.setText(year + "-" + Month + "-" + dayOfMonth);
}};
DatePickerDialog dpDialog=new DatePickerDialog(getActivity(), listener, year, month, day);
dpDialog.getDatePicker().setMinDate(mcalendar.getTimeInMillis());
dpDialog.show();
}
Related
//When user clicks "other".
public void setDate(View view) {
DateTime dateTime=new DateTime();
new DatePickerDialog(CreateEventActivity.this, listener, dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfWeek()).show();
}
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
DateTime eventDate = new DateTime();
eventDate.withYear(year);
eventDate.withMonthOfYear(monthOfYear + 1);
eventDate.withDayOfMonth(dayOfMonth);
time = eventDate;
Context appContext = getApplicationContext();
Toast.makeText(appContext, dayOfMonth + "/" + (monthOfYear + 1) + "/" + year + "," +eventDate.dayOfWeek().getAsText(), Toast.LENGTH_LONG).show();
dateTxt.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
dayTxt.setText(eventDate.dayOfWeek().getAsText());
otherButton.setBackgroundColor(Color.parseColor("#77B3FC"));
todayButton.setBackgroundColor(Color.parseColor("#DBDBDB"));
tomorrowButton.setBackgroundColor(Color.parseColor("#DBDBDB"));
}
};
I have a problem with this part: eventDate.dayOfWeek().getAsText()
It shows the wrong day of the week, but the date in numbers is fine.
(Just started using Joda-Time so I'm not sure about it...)
EDIT:
The date in numbers like: 23.7.16 is printed correctly, But I want it to show which day is it in the week, like "Monday"... I've noticed it always writes today's name. In the toast and in the text view...
For example, for couple of different dates it will show:
23.6.16, Friday|
15.7.16, Friday|
30.8.17, Friday
the others data are right because you are using the data of the DataPicker and not of the JodaTime, may have some error in your construction try:
DateTime eventDate = new DateTime(year,monthOfYear+1,dayOfMonth,0,0,0);
the 0,0,0 are hours, minutes and seconds, remove the 3 lines :
eventDate.withYear(year);
eventDate.withMonthOfYear(monthOfYear + 1);
eventDate.withDayOfMonth(dayOfMonth);
When i press the button to display the DatePickerDialog, the dialog displays one month greater. For instance, if i initiate with the current date like this(with the DateTime of joda library):
DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
DateTime dt = new DateTime(zone);
int year = dt.getYear();
int month = dt.getMonthOfYear();
int day = dt.getDayOfMonth();
which is 07/08/2014, the date dialog displays one month greater 07/09/2014.
I do not understand why this happens.
The fragment which represents the datePickerFragment is:
#SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
#SuppressLint("ValidFragment")
TextView txtDate;
GlobalData appState;
public DatePickerFragment(TextView txtDate) {
super();
this.txtDate = txtDate;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
DateTime dt = new DateTime(zone);
int year = dt.getYear();
int month = dt.getMonthOfYear();
int day = dt.getDayOfMonth();
Log.i("DatePickerFragment day month year", day +" "+ month + " "+ year + "");
// 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) {
appState.setDateUserFrom(year, month, day);
Log.i("Date day month yerar", "Date changed." + day+" " + month + " " +year);
txtDate.setText(new StringBuilder().append(day)
.append("-").append(month).append("-").append(year)
.append(" "));
}
}
DatePickerDialog takes monthOfYear that is 0 to 11 [0 for Jan... 11 for Dec], and your DateTime returns 1 to 12. So you need to do -1 with month value.
Use this:
return new DatePickerDialog(getActivity(), this, year, month - 1, day);
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
DateTime dt = new DateTime(zone);
int year = dt.getYear();
int month = dt.getMonthOfYear()-1;
int day = dt.getDayOfMonth();
Log.i("DatePickerFragment day month year", day +" "+ month + " "+ year + "");
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
Month in date picker starts with zero. So you should subtract one from the getMonthOfYear() to set it on datepicker.
I am pretty sure the reason behind this is because the Android DatePickerDialog expects 0 based month values. Jodatime return them as you would expect it (more human friendly). So just subtract 1 from the month.
Just to clarify, most date functions/libraries are designed with 0 based month values by default. The exception is where explicitly noted, or third party libraries like Jodatime, which make working with date stuff a joy.
Just a guess. Joda month starts at 1 for january but java date at 0? So if you use the current date init with joda the date picker will show the wrong month. Easy solution:
month = dt.getMonthOfYear() - 1;
Try this hope it's worked:
DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
DateTime dt = new DateTime(zone);
int year = dt.getYear();
int month = dt.getMonth();
int day = dt.getDayOfMonth();
OR
DateTimeZone zone = DateTimeZone.forID("Europe/Athens");
DateTime dt = new DateTime(zone);
int year = dt.getYear();
int month = dt.getMonthOfYear() - 1;
int day = dt.getDayOfMonth();
I have been struggling with this for a while.
Is there a method to ALWAYS display the date format in YYYY/MM/DD in a DatePicker widget regardless of user specific locales?
I have been searching the web for the whole day but I can only find how to get the date/time and convert it to other formats but not how to actually get the cursed widget to display a different time format.
The only related answer that I can find on stackoverflow is this. Surely there has to be an easier way than to brute force the API.
To clarify my context and usage: I'm using my DatePicker in a Fragment(NOT DialogFragment extending a DatePickerDialog).
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int month = monthOfYear + 1;
String formattedMonth = "" + month;
String formattedDayOfMonth = "" + dayOfMonth;
if(month < 10){
formattedMonth = "0" + month;
}
if(dayOfMonth < 10){
formattedDayOfMonth = "0" + dayOfMonth;
}
searchText.setText(formattedDayOfMonth + "/" + formattedMonth + "/" + year);
}
#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();
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);
}
}
});