to get the current date and time
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
to create current date object
Date toDate;
toDate.setYear(mYear);
toDate.setMonth(mMonth);
toDate.setDate(mDay);
Date endDate = toDate;
when printing endDate object I got
Mon Jan 01 13:11:00 GMT+03:00 3912
why ?
From Date.setYear(int) description: Sets the gregorian calendar year since 1900 for this Date object. Thus, 1900 + 2012 = 3912.
But calendar.get(Calendar.YEAR) returns exact year number 2012. So this inconsistency of API causes your issue. But anyway Date.setYear(int) is deprecated, thus, it is better to use Calendar object for date calculations.
Better to use Calendar
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,myear);
(i.e)
cal.set(Calendar.YEAR,2016);
Related
I am using realm to save a date but need to use the day and month of the date for later use so am converting the type Date to Calendar using:
public static Calendar toCalendar(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
Then on onCreate of a fragment:
Date testDate = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", testDate.getTime());
Log.d("Date", String.valueOf(s));
// This prints May 26, 2017
Calendar testCalendar = toCalendar(testDate);
Log.d("CALENDAR DAY TEST", String.valueOf(testCalendar.DAY_OF_MONTH));
// This prints 5, MONTH prints 2. Rather the expected 26 & 5.
The DAY_OF_MONTH and MONTH methods of testCalendar are returning the day as the 5th and month as the 2nd rather than the 26th and 5th.
Those are static ints used for tellling Calendar what type of data you want.
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
Hi I implemented a DatePickerDialog but I want to set range, I found setMinDate and setMaxDate, but I don't know how to put values. I want to set the calendar for a user of min age 18 and max age 100; I think it's about calculations into milliseconds.
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, year, month, day);
dpd.getDatePicker().setMaxDate(max);
return dpd;
I don't know what value I have to put to max to do 2016 - 18.
Use this code, to set maximum date and minimum date from current date.
Calendar minCal = Calendar.getInstance();
minCal.set(Calendar.YEAR, minCal.get(Calendar.YEAR) - 100);
Calendar maxCal = Calendar.getInstance();
maxCal.set(Calendar.YEAR, maxCal.get(Calendar.YEAR) - 18);
dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
dialog.getDatePicker().setMaxDate(maxCal.getTimeInMillis());
I guess this is what you need:
final Calendar maxc = Calendar.getInstance();
int year = maxc.get(Calendar.YEAR);
maxc.set(Calendar.YEAR,year-18);
datePickerDialog.getDatePicker().setMaxDate(maxc.getTimeInMillis());
final Calendar minc = Calendar.getInstance();
int year = minc.get(Calendar.YEAR);
minc.set(Calendar.YEAR,year-100);
datePickerDialog.getDatePicker().setMinDate(minc.getTimeInMillis());
datePickerDialog.show();
I am trying to get only the date from the Date object but currently i get only the following output:
"Thu Nov 24 17:46:14 GMT +02:00 2016"
and i would like to get:
"24/11/2016"
this is my code:
public String getNextYearDate(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1); // to get previous year add -1
Date nextYear = cal.getTime();
return nextYear.toString();
Just use SimpleDateFormat with dd/MM/yyyy
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String output = df.format(nextYear);
Documentation
You can try below code :
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
int mYear = cal.get(Calendar.YEAR);
int mMonth = (cal.get(Calendar.MONTH))+1;
int mDay = cal.get(Calendar.DAY_OF_MONTH);
String result = ""+mDay+"/"+mMonth+"/"+mYear ;
I can get the current date with the following code to show in DatePicker dialog for example.
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
How can I show the actual date minus 18years (more or less 6570days)?
From my "research" I need to use DAY_OF_YEAR
Try using:
c.add(Calendar.YEAR, -18);
You shouldn't have to worry about the "more or less" of 6570 days since a Calendar object handles leap years (and exact number of days in a month itself).
I want to use a calendar in android. Within the calendar, I want to set the minimum year of 1998; T the user should not able to enter a year before that. How can I solve this problem? By default it goes before 1998.
I am using following code:
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
mDatePickerDialog = new DatePickerDialog(WinningNumberPowerBall.this, mDateSetListener, cyear, cmonth,cday);
mDatePickerDialog.show();
By using this my calendar year starts from 1900 but I want to start the year at 1998. How can I do this?
I think you can just do:
...
Calendar minCal = new GregorianCalendar(1998, Calendar.JANUARY, 1);
mDatePickerDialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
mDatePickerDialog.show();