I am using MaterialCalendarView for my app calendar.
I want to read the month and year when the user clicks next/previous on the calendar. Here is my code:
calendarView = (CalendarView) view.findViewById(R.id.calendarView);
calendarView.setOnPreviousButtonClickListener(new OnNavigationButtonClickListener() {
#Override
public void onClick() {
Calendar calendar = calendarView.getCurrentPageDate();
int previousMonth = calendar.MONTH;
int previousYear = calendar.YEAR;
Log.i(TAG, "--- previous month: " + previousMonth + "/" + previousYear + " - " + calendar);
}
});
calendarView.setOnForwardButtonClickListener(new OnNavigationButtonClickListener() {
#Override
public void onClick() {
Calendar calendar = calendarView.getCurrentPageDate();
int nextMonth = calendar.MONTH;
int nextYear = calendar.YEAR;
Log.i(TAG, "--- next month: " + nextMonth + "/" + nextYear + " - " + calendar);
}
});
However when I click the next/previous arrows the MONTH and YEAR values returned to me are incorrect. So for example at this instance its JAN 2018. Previous month should be 11 and year should be 2017, and next month should be 1 and 2018.
However for previous click in the logs, I see:
I/CalendarFragment: --- previous month: 2/1 -
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=11,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
For next click I see:
I/CalendarFragment: --- next month: 2/1 -
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=1,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
If you see the logs, the calendar instance value does have the right MONTH and YEAR. But when I access it outside it returns 2 and 1. Why?
This same thing is happening when I do setOnDayClickListener()
calendarView.setOnDayClickListener(new OnDayClickListener() {
#Override
public void onDayClick(EventDay eventDay) {
Calendar clickedDayCalendar = eventDay.getCalendar();
Log.i(TAG, "------- clicked day: " + clickedDayCalendar.DAY_OF_MONTH + " - " + clickedDayCalendar);
}
});
Does not matter which day I click in January, it gives me clicked day as 5. Here is the log:
I/CalendarFragment: ------- clicked day: 5 -
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=0,WEEK_OF_YEAR=2,WEEK_OF_MONTH=2,DAY_OF_MONTH=12,DAY_OF_YEAR=12,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=10,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
I/CalendarFragment: ------- clicked day: 5 -
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=java.util.SimpleTimeZone[id=GMT,offset=0,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=0,WEEK_OF_YEAR=3,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=20,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=3,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
Any idea why this is happening? Or what is causing it?
MONTH and YEAR are public static int vars. So accessed them with Calendar class.
Calendar calendar = calendarView.getCurrentPageDate();
int previousMonth = calendar.get(Calendar.MONTH) + 1;
int previousYear = calendar.get(Calendar.YEAR);
Log.i(TAG, "--- previous month: " + previousMonth + "/" + previousYear);
The date from DateRangePicker displays as: "yyyy-M-d".
But I want it to display the date as: "yyyy-MM-dd".
I have tried out the following code:
#Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth, int yearEnd, int monthOfYearEnd, int dayOfMonthEnd) {
String selection1 = year + "-" + (monthOfYear+1) + "-" + dayOfMonth;
String selection2 = yearEnd + "-" + (monthOfYearEnd+1) + "-" + dayOfMonthEnd;
Long firstDateSelection = Long.parseLong(selection1);
Long secondDateSelection = Long.parseLong(selection2);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = new Date(firstDateSelection);
Date endDate = new Date(secondDateSelection);
formatter.format(startDate); formatter.format(endDate);
But I get NumberFromatException when I run it at "Long firstDateSelection = Long.parseLong(selection1)"?
Help is much appreciated :)
Thanks!
You are getting NumberFormatException because "1234-12-23" is not a valid Long number and you are trying to convert it to a Long here:
Long firstDateSelection = Long.parseLong(selection1);
Since you already have the numbers for day, month and year you can simply format it(no need for SimpleDateFormat):
String dateString = String.format("%1$04d-%2$02d-%3$02d", year, monthOfYear, dayOfMonth);
If you check Javadoc about parseLong(string)
Throws
NumberFormatException
if the string does not contain a parsable long.
As you can see a string with "-" is not a long number and it throws that exception.
If you want to do it correctly you have to create a Calendar, add it the values you are passing by parameters (year, monthOfYear...) with set(...) and then make getTime() to get Date object.
Don't create a Date object directly (new Date...) because its methods setHour, etc. are deprecated.
Once you have that, you will be able to do .format(dateObject).
Good day.I am building an chat application.For purpose i decided to put a date of message had been sent.No wounder that in different countries the date must show different.For example let's take 2 different countries and assume the difference between them are 2 hours.CountryX and CountryY.The user send message from CountryX which time is lets say 15:00.I am saving this on server with exact timezone time as user sent,exactly 15:00 as CountryX.Second user receives the message in CountryY which is more in 2 hours from CountryX,so basically The time which I MUST show in CountryY must be 17:00.This is the issue.How can i convert an already received time with known timezone to local in order to show correct?I did google a lot but all i came up,were solutions where you would simply just get an time for exact country,and not convert the CountryX sent Time to CountryY local time to show it correctly in CountryY.Please can you provide an help?Thank you very much beforehand.
For everyone suffering because of this.I have ended up creating my own class with my own logic and it works flawlessly and as an extra bonus couple of handy methods with time
public class Time {
public static String getCurrentTime() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String finalFormat = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
if (month < 10) {
String finalMonth = "0" + month;
finalFormat = year + "-" + finalMonth + "-" + day + " " + hour + ":" + minute + ":" + second;
}
return finalFormat;
}
public static String convertToLocalTime(String timeToConvert) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone(Constants.SERVER_TIME_ZONE));//where server time zone is the time zone of your server as defauul,E.X -America/Los_Angeles
Date parsed;
try {
parsed = sourceFormat.parse(timeToConvert);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
return result;
}
public static String getTimeZone() {
return TimeZone.getDefault().getID();
}
}
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();
}
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);
}