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);
Related
I have a method that collect the year, month and day from a DatePicker and stores them in separate integers.
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
yearD = year;
monthD = (month + 1);
dayD = dayOfMonth;
}
How can I transform these integers to a SimpleDateFormat with the pattern "yyyy-MM-dd"?
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
First you create a calendar object such as
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day, 0, 0);
Now format as per your requirement as below
Date date = c.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = dateFormat.format(date);
Though I have not tested the code on IDE but I hope it will give you the solution.
I guess that should do the trick:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = new Calendar().set(year, month, day);
formatter.format(cal);
Even though this is not strictly an answer, I would recommend you to jump from the old and error prune Calendar / DatePicker classes, to the intuitive new LocalDate class. If you are interested, let me know and I'll tell you how :)
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);
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 need to get the month and day of today's date and offset dates. This is how I do it:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 0);
Date today = calendar.getTime();
System.out.println(today);
Output:
Wed Aug 27 15:07:35 CEST 2014
Two things, I need the month and the day to be numeric, like 8/27. I understand how to do that with today's date like so:
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
String a = String.valueOf(day);
String b = String.valueOf(month);
System.out.println(b +"/" + a);
My issue is that I might need to add an offset to that date, if I want tomorrows date for example. Is there a way to do that because converting Wed Aug 27.... to 8/27 would just be a pain. Thanks
Use simple date format:
Something like:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 1);
Date today = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println(sdf.format(today));
DateFormat formatter = new SimpleDateFormat("MM/dd");
Calendar cal = Calendar.getInstance();
String calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
// Now for tomorrow's date:
int offset = 1;
cal.add(Calendar.DATE, offset);
calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
Use the Calendar to add a value to the day:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,1)
I want a javascript or java program should always give date 1st of current month.
Is there any tech?
You can use Calendar for Java
Date date = new Date(System.currentTimeMillis());
cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
Now you do what every you want to do with this Calendar object like to get the Day of the Week (Sat, Sun, .... )
int weekday = cal.get(Calendar.DAY_OF_WEEK);
And for JavaScript you can use:
var theFirst = new Date();
theFirst.setDate(1);
setDate sets the day of the month for the Date object (from 1 to 31). Then you can do whatever you want with theFirst, like get the day of the week.
Calendar ans = Calendar.getInstance();
ans.set(ans.get(Calendar.YEAR),
ans.get(Calendar.MONTH),
1,
0,
0,
0
);
System.out.println(ans.getTime());