I am using Calendar Class in my android app to calulate day of year of a date and then do some comparison with current day of year. Here is the code I use :
Date now=new Date();
Calendar ca1 = Calendar.getInstance();
ca1.set(now.getYear(), now.getMonth(), now.getDate());
int nowC=ca1.get(Calendar.DAY_OF_YEAR);
//Date arg0=say,get from user
Calendar ca2 = Calendar.getInstance();
ca2.set(arg0.GetBirthDay().getYear(),arg0.GetBirthDay().getMonth(),arg0.GetBirthDay().getDate());
int d1C=ca2.get(Calendar.DAY_OF_YEAR);
I debug my application and I see the following value for current day :
Fri Mar 02 14:18:33 Asia/Tehran 2012
and for arg0:
Fri Mar 02 00:00:00 Asia/Tehran 1979
And 'nowC' got 62, and 'd1C' got 61.
I expect them to be equal cause both of them has same month and day, also If I use DateTime class of joda package, as below, I get the same results:
int ndy=dtnow.getDayOfYear();
int d1dy=dt1.getDayOfYear();
Why it is happening ?
2012 was a leap year, 1979 was not. There is an extra day before March 2nd this year - so both APIs are giving you the right answer!
I suspect you want to compare both the month and day-of-month, to get the semantics you are expecting.
Related
I want to get the two calender time difference
my calender is
val calendar1 = Calendar.getInstance()
calendar1[Calendar.HOUR+1] = hour.toString().toInt()
calendar1[Calendar.MINUTE] = minute.toString().toInt()
this is giving in 12 hours format Wed Nov 17 06:30:31 GMT+05:30 2021
current calender is:-
val calendar = Calendar.getInstance()
calendar[Calendar.HOUR]=calendar.get(Calendar.HOUR)
calendar[Calendar.MINUTE]=calendar.get(Calendar.MINUTE)
this is giving in 24 hours format Wed Nov 17 18:01:32 GMT+05:30 2021
how to get this calender in 12 hours format
thats why im having difficult in finding two calender difference
can anyone help?
In first calender you are using
calendar1[Calendar.HOUR+1] -> which will be equivalent to calendar1[Calendar.HOUR_OF_DAY]
Here are the constants from Calendar class
public static final int HOUR = 10;
public static final int HOUR_OF_DAY = 11;
use calendar1[Calendar.HOUR] for 24 hour format .
Better way is to get millis from both calenders and find difference between 2 millis .
To get millis use calendar.time.time
for getting difference between millis see this answer
I'am using GregorianCalendar class to manipulate with date and time.
I need to get only a current date without time.
My code:
Calendar today = new GregorianCalendar();
today.set(GregorianCalendar.HOUR,0);
today.set(GregorianCalendar.MINUTE,0);
today.set(GregorianCalendar.SECOND,0);
today.set(GregorianCalendar.MILLISECOND,0);
Date todayDate = new Date();
todayDate.setTime(today.getTime().getTime());
I expect todayDate will be like this "Wed Dec 07 00:00:00 EET 2016"
But actually todayDate is "Wed Dec 07 12:00:00 EET 2016".
Which is the correct way to do it?
Ia understend difference between fields HOUR and HOUR_OF_DAY when I get value, but why when I set value of HOUR to "0" the HOUR_OF_DAY is not seting to "0" automatically. Zero is always zero...
Question is about Data integrity...
It is a mistake to thought zero is always zero... It is not true for hours after midday. Zero hours after midday in short form (am/pm) is 12 hours in 24 form.
Thanks to #selvin
In my application i have to show dates 5 years back dates from current date like example today is 4th July 2016 but in my date picker it will show as 4th July 2011 and user can not choose date after 4th July 2011 but can choose before 4th July 2011 and backwards.
I have seen some examples like Click here
DatePickerDialog mDate = new DatePickerDialog(DatePicker.this, date, year, month, day);
maxDate = Calendar.getInstance();
maxDate.add(Calendar.YEAR, -5);
mDate.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
mDate.show();
I have tried this and its working perfectly from January 1900 to July 4 2011 but one thing i want that is when i click the date in calendar it shows January 1900 all i want is when i click it it should show from July 2011 and then backward dates.
Solution-1
You can just set :
long years4 = 126144000; //4 years in seconds
long years_millis4 = years4*1000; //4 years in milliseconds, which is required format in date picker
dateDialog.getDatePicker().setMaxDate((new Date().getTime())-years_millis4);
where dateDialog is a
new DatePickerDialog()
and the param type to set for MaxDate and MinDate is a long
Hope this works for your case.
Apart from this you can add listeners to your date picker using which you can prompt user in case he chooses any date other than the required ones.
Solution 2
You can implement your custom date picker using spinners and custom dates as the list in the spinners. Apply proper conversions while getting the input.
Make sure that java.util.Date.getTime() method returns how many milliseconds have passed since January 1, 1970, 00:00:00 GMT.
GregorianCalendar gd1 = new GregorianCalendar(2013,12,12);
Date d = gd1.getTime();
Sun Jan 12 00:00:00 EST 2014 <===== what's wrong?
Is there a simpler way to construct Date for certain date (the Date(year, month, day) is deprecated).
Thanks!
The GregorianCalendar class' months are zero-based (many people see this as a mistake, but that's not the point).
If you actually want to put the date as December 12, 2013, you would have to use the following line:
GregorianCalendar gd1 = new GregorianCalendar(2013,11,12);
When trying to get a string for the current date using
DateFormat.getDateInstance().format(calendar.getTime())
it keeps returning the wrong day. For example, it is saying today, July 25th., is July 26th. Also when I use it to sat a date picker, I get the day value by using
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
When the date picker is set, it also shows the day ahead by 1.
To get the calendar I'm using
Calendar calendar = Calendar.getInstance();
Is there something I'm missing?
Thanks
I would imagine this is because you havent set the timezone to your timezone, and rather than the day being off randomly, the time zone you are in is diferent than GMT (Greenwich Median? Time). Try looking at this example How to handle calendar TimeZones using Java?