Oracle tutorial: Date Time explaining how to use java.time.Then use om(ZoendDateTIme) for the conversion to a Calendar object. If you indispensably need a Calendar object for a legacy API that you cannot afford to upgrade to java.time just now, use a ZonedDateTIme from java.time for your time math. All of the date-time classes of java.time that include time of day have the same with method, so the code will be the same. If you need the date too, use ZonedDateTime or another appropriate class. with(ChronoField.AMPM_OF_DAY, 0) // 0 = AM LocalTime t12Midnight = LocalTime.now(ZoneId.systemDefault()) with(ChronoField.AMPM_OF_DAY, 1) // 1 = PM ![]() with(ChronoField.CLOCK_HOUR_OF_AMPM, 12) If you have already got a time and only want to adjust the hour and AM/PM, use with(): LocalTime t12Noon = LocalTime.now(ZoneId.systemDefault()) LocalTime t12Midnight = LocalTime.MIDNIGHT Ī LocalTime is a time of day without a date. If you just want 12 noon or 12 midnight, they are built in as constants: LocalTime t12Noon = LocalTime.NOON I recommend that you use java.time, the modern Java date and time API, for your time work. The case for AM is similar, so I leave the details to the reader. At that point it picks up all the changes from the calls to set() up to that point and combines them to the best of its abilities, discarding some if there are conflicts, using rules that no person in their right mind will want to understand. It calculates the time when you call getTime() (and some designated other methods). Why does it calculate the time twice? Not once and not three times when you do three calls to set()? It’s another confusing trait of Calendar. When you set PM again, Calendar takes off from the time you had already got, which is in AM, and changes it into PM, so you get 12:11:08, still on Jan 13, the following day. By Calendar logic hour 12 is the hour that comes after hour 11. Instead it sets the time to 0 AM the following day Jan 13 when you ran the code on Jan 12. For negative values of n, this function returns all rows except the first n rows, equivalent to df n. It is useful for quickly verifying data, for example, after sorting or appending rows. But no, a Calendar object with default settings doesn’t give you that. This function returns last n rows from the object based on position. When you first set hour to 12 and AM/PM to PM, one should have expected an exception because the hour value is out of range. Midnight are represented by 0, not by 12. HOUR is used for the 12-hour clock (0 - 11). The documentation says about HOUR:įield number for get and set indicating the hour of the morning orĪfternoon. To answer your question as asked, Andi80 is correct in the other answer and the comments to it: HOUR goes from 0 through 11. ![]() Why setting calendar AM or PM multiple times change the result? … Fortunately the class is also long outdated. You are so far from the first being confused about how the Calendar class works.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |