I have a Calendarview and an Edittext. I wish to take the date entered in the edittext in the format DD/MM/YYYY and set it on the Calendarview. The view should update the current date bubble to the set date.
I have already tried:
How to set focus on a specific date in CalendarView knowing date is "dd/mm/yyyy"
This is my current code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2019);
calendar.set(Calendar.MONTH, Calendar.JUNE);
calendar.set(Calendar.DAY_OF_MONTH, 20);
long milliTime = calendar.getTimeInMillis();
CalendarView calendarView = findViewById(R.id.calendar);
calendarView.setFocusedMonthDateColor(Color.BLUE); //apparently this is deprecated so won't work. Not working without it either.
calendarView.setDate (milliTime); //this is supposed to change the date but isn't
Thanks in advance!
I have read your question and i have try to solve this problem. pass date(time in milliseconds) in long type value to set date in calendar view like this calendar_view.setDate(milliTime); try this code to achieve your goal .
Code
calendar_view = findViewById(R.id.calendar_view);
//replace date variable static value to your edit text
value
String date = "25/06/2019";
String parts[] = date.split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
long milliTime = calendar.getTimeInMillis();
calendar_view.setDate(milliTime);
Related
I'm using a DatePickerDialog to select a begin / end Date to search in an article list.
Thing is DatePickerDialog's setMinDate actually set also default position of dialog box when opened and block user from selecting an anterior date:
Here's my date gestion method:
private void createDisplay(DatePickerDialog.OnDateSetListener dateSetListener) {
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
DatePickerDialog dialog = new DatePickerDialog(
Objects.requireNonNull(getActivity()),
R.style.DatePickerDialogTheme,
dateSetListener,
day, month, year);
dialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
cal.add(Calendar.YEAR, -5);
dialog.getDatePicker().setMinDate(cal.getTimeInMillis());
dialog.show();
}
I mean, it should be today as default position but if I use setMinDate I can't select an anterior position of today's date...
Use it like this -
Example:
// Adding 3 days
calendar.add(Calendar.DATE, 3);
// Set the Calendar new date as maximum date of date picker
dpd.getDatePicker().setMaxDate(calendar.getTimeInMillis());
// Subtract 6 days from Calendar updated date
calendar.add(Calendar.DATE, -6);
// Subtract 6 months from Calendar updated date
calendar.add(Calendar.MONTH, -6);
Or you can use
dpd.getDatePicker().setMinDate(new Date().getTime());
Note - put any date as you wish and modify it like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);
Date curDate = new Date();
long curMillis = curDate.getTime();
String curTime = formatter.format(curDate);
String oldTime = "05.01.2011, 12:45";
Date oldDate = formatter.parse(oldTime);
long oldMillis = oldDate.getTime();
And then
dpd.getDatePicker().setMinDate(oldMillis);
I want to to get the elapsed time from the beginning of a month to now in android programmatically.
preferably using Calendar.getInstance().
For example today is 12/10/2018. so the duration in millisecs will be 12/01/2018 to 12/10/2018
To retrieve the beginning of the month:
val cal = Calendar.getInstance()
cal.set(Calendar.HOUR_OF_DAY, 0)
cal.clear(Calendar.MINUTE)
cal.clear(Calendar.SECOND)
cal.clear(Calendar.MILLISECOND)
cal.set(Calendar.DAY_OF_MONTH, 1)
Then to calculate elapsed in milliseconds:
val current = Calendar.getInstance()
val timePassedMilliseconds=current.timeInMillis-cal.timeInMillis
is your problem creating a new Calendar and populating it? you can create an empty one, and just populate with the fields that you are interested in.
Calendar now = Calendar.getInstance();
Calendar startOfMonth = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_MONTH, 1); //first day of month
calendar.set(Calendar.MONTH, now.get(Calendar.MONTH));
calendar.set(Calendar.YEAR, now.get(Calendar.YEAR);
timeElapsed = now.getTimeInMillis() - startOfMonth.getTimeInMillis() ;
You can use calendar.set(year,month,1,0,0,0); to get the timestamp of the first day of the month.
Calendar calendar = Calendar.getInstance();
Date d = new Date(1544371200000L); //12/10/2018
calendar.setTime(d);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
calendar.set(year,month,1,0,0,0);
Date firstDayOfMonth = calendar.getTime();
long duration = d.getTime() - firstDayOfMonth.getTime();
Here's my onCreateDialog:
DatePickerDialog dialog = new DatePickerDialog(this, dpickerListener, year_x, month_x, day_x);
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dialog.getDatePicker().setMaxDate();
As you can see, setMaxDate() is empty. I want to set the max date of the datePickerDialog to one month after the current date.
Here's how I get the current date:
cal2 = Calendar.getInstance();
currentDay = cal2.get(Calendar.DAY_OF_MONTH);
currentMonth = cal2.get(Calendar.MONTH);
currentYear = cal2.get(Calendar.YEAR);
currentDate = new Date();
currentDate.setDate(currentDay);
currentDate.setMonth(currentMonth);
currentDate.setYear(currentYear);
Use this snippet
Calendar cal=Calendar.getInstance()
cal.add(Calendar.MONTH,1)
long afterTwoMonthsinMilli=cal.getTimeInMillis()
DatePickerDialog.getDatePicker().setMaxDate(afterTwoMonthsinMilli);
It reads the current system date into a calendar object and adds 1 to the specified field,In this case MONTH and returns a calendar object
Similarly if you want to add values to other fields,Specify the field in the field type as:
Calendar.DAY_OF_MONTH
Calendar.YEAR
Calendar.HOUR
etc.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis);
You can use "add" to add to the Calendar.MONTH
you have to override OnDateChangedListener
`new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int month, int day) {
Calendar newDate = Calendar.getInstance();
newDate.add(Calendar.MONTH, 1);
if (calendar.before(newDate)) {
view.init(minYear, minMonth, minDay, this);
}
}
});`
I want to get time from user(with editbox like year/month/day/hour/minutes).
How can i convert it to millisecond like getTime function or convert to minutes like getTime()/60000?
You can use a DatePicker and then use the getters from that to either make a Calendar object or to get the time in millis.
I use this function in my projects to get the Calendar from the DatePicker, you may want to extend it to set the hours/ minutes as well:
public static Calendar getCalenderFromDatePicker(DatePicker dp) {
int day = dp.getDayOfMonth();
int month = dp.getMonth();
int year = dp.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar;
}
Once you have the Calendar you can use myCalendar.getTimeInMillis() which returns a long of the time in millis since the epoch.
Consider java.util.Calendar class and the following code:
Calendar calendar = Calendar.getInstance();
int year = 2015;
int month = 4; // "May", months are counting from 0 to 11
int day = 14;
int hour = 14;
int minutes = 15;
int seconds = 16;
// for more clarity I set year/day/month/hours/minutes/seconds separately
// you can also set them at once via calendar.set(year, month, day);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
long composedDateInMilliseconds = calendar.getTimeInMillis();
I have some date and I want to get last x day before this date so this is my code:
Calendar today = Calendar.getInstance();
today.add(Calendar.DATE, -x);
date = new Date(today.getTimeInMillis()))
this code works only if some day is actual day. How I can change it. Is there some method to get calendar from date ?
Use the setTime method to set the date of your calendar :
Calendar aDay = Calendar.getInstance();
aDay.setTime(aDate);