JAVA - Is this a bug in the java.util.Calendar class or what?

I have encountered the same problem many times.

The same problems were with this question and got a solution, like the same one. How to compare the known clock of the current hour in android?

Problem:

When I use Calendar calCurr = Calendar.getInstance(); to get the Calendar object of the current date and time, it always returns me incorrectly.

I set up the logs and checked them, and in order for it to correct, I had to add in years and months, and then I got the correct object for the current date and time.

See my example:

  Calendar calCurr = Calendar.getInstance(); Log.i("Time in mili of Current - Normal", ""+calCurr.getTimeInMillis()); // see what it gives? dont know why? Date date = new Date(); calCurr.set(date.getYear()+1900, date.getMonth()+1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()); // so added one month to it Log.i("Time in mili of Current - after update", ""+calCurr.getTimeInMillis()); // now get correct 

Question:

  • Why does this give the wrong conclusion?
  • Is this a mistake, or is my Calendar class concept wrong?
  • tell me what was to be done for this?
+6
source share
8 answers

It works fine, as expected, if you go to getDate() , it will output:

Time in mili of Current - Normal Wed Apr 04 11:34:34 BST 2012

Time in Miles Current - after the update Fri May 04 11:34:34 BST 2012

What do you expect? And in milliseconds this is also equal to 30 days:

Time in miles Current - Normal 1333535834557

Time in miles of the current - after update 1336127834557

and calculation (difference divided by milliseconds per day):

 1336127834557 - 1333535834557 = 2 592 000 000 2592000000 / 86400000 = 30 

And today's date in milliseconds after 1970 is 1333536754 ... which fits, I don't see a problem.

EDIT

Your problem is that you set the month as 3 for the march ... there you need to set 2..cause months are indexed from 0 to 11.

+5
source

Do not use date.getXXX() . Do not use a setter or getter other than Date.getTime() . They are all out of date. Using them will lead to unexpected results.

If you call Calendar.getInstance() , it is already set to the current date. If you want to set or add days, months, whatever, set them in the calendar.

eg. calCurr.set(Calendar.MONTH,2) or calCurr.add(Calendar.DAY,1) .

+5
source

Calendar months are indexed with a zero mark. Therefore, when you want to set for March its 2 not 3

Also, do not set the year, month, and date from the Date object. If you must initialize the calendar by date, do the following:

  Calendar cal = Calendar.getInstance(); cal.setTime(date); 

remember that your Date object may be different from what you think.

+1
source

The Java Date Based API is not designed properly. in future versions, I think some API issues will be addressed.

I would recommend using JodaTime .

+1
source

This is NOT a mistake, the Calendar returns what it should (at least here).

 Calendar calCurr = Calendar.getInstance(); Log.i("Time in mili of Current - Normal", ""+calCurr.getTimeInMillis()); // see what it gives? dont know why? 

I got 1333546375707 milliseconds, which is the correct value (also calculated manually).
What value do you expect here? How do you know this is wrong?


 Date date = new Date(); calCurr.set(date.getYear()+1900, date.getMonth()+1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()); // so added one month to it 

Why add ONE per month? The month of the date and calendar is independent of zero - no need to add 1.


EDIT
Manual calculation (approximate):

 2012 - 42 years * 365.24 days/year * 86400 seconds/day April - (31 + 29 + 31) days * 86400 4th - 3 days * 86400 13:30 - 13.5 hours * 3600 seconds/hour ==================== 1333553112 seconds 
+1
source

This is a weird implementation of Calendar . For some reason, January is month 0, and the years are also not very logical.

I recommend the Joda time library.

0
source

We use the following lines of code to find the current date and time. He works great on our side.

  java.util.Calendar calc = java.util.Calendar.getInstance(); int day = calc.get(java.util.Calendar.DATE); int month = calc.get(java.util.Calendar.MONTH)+1; int year = calc.get(java.util.Calendar.YEAR); String dayStr,monthStr; if(day<10){ dayStr = "0"+day; }else{ dayStr = ""+day; } if(month<10){ monthStr = "0"+month; }else{ monthStr = ""+month; } /*String currentdate = monthStr+"/"+dayStr+"/"+year+" ";*/ String currentdate = dayStr+"/"+monthStr+"/"+year+" "; /*String currenttime = currentdate + String.valueOf(calc.get(java.util.Calendar.HOUR_OF_DAY))+ ":"+ String.valueOf(calc.get(java.util.Calendar.MINUTE))+":"+String.valueOf(calc.get(java.util.Calendar.SECOND));*/ return currentdate; 
0
source

When a Calendar object is created using Calendar.getInstance (), the time instance variable in the Calendar object is set, and this value will only be changed if you use the Calendar.setTimeInMillis () function.
Code snippet from Calendar object:

 public long getTimeInMillis() { if (!isTimeSet) { updateTime(); } return time; } 

Here, "isTimeSet" will become "true" when Calendar.getInstance () is called, and it returns a "time" each time without updating the time.

It is for this reason that you get the same time value every time you call calCurr.getTimeInMillis ();

Hope this helps.

0
source

Source: https://habr.com/ru/post/912374/


All Articles