Questions tagged [java.util.calendar]

The Java class for converting a moment in time (java.util.Date) to fields that represent the date (day, hour, minute, etc.).

The java.util.Calendar class has existed since Java 1.1. It allows conversion between the Date interpretation of "number of milliseconds" since midnight, January 1, 1970 UTC to fields such as year, month, day, hour, minutes, seconds, and milliseconds. These fields are mutable.

147 questions
145
votes
14 answers

Set time to 00:00:00

I have a problem resetting hours in Java. For a given date I want to set the hours to 00:00:00. This is my code : /** * Resets milliseconds, seconds, minutes and hours from the provided date * * @param date * @return */ …
Adelin
  • 18,144
  • 26
  • 115
  • 175
13
votes
5 answers

Setting values of Java Calendar does not give expected date-time

I have an hour, minute, date and millisecond timestamp, and am trying to create a Date object representing the time. The timestamp is provided in Eastern Daylight Time. In dissecting the problem, I created some simple test code to see what was…
xirt
  • 514
  • 1
  • 6
  • 18
9
votes
2 answers

What does Calendar.UNDECIMBER do?

There is a constant in the Calendar class called: UNDECIMBER. It describes the 13th month. Is there a useful purpose for this constant? In Wikipedia it is written that it is for the lunar calendar. But there is no implementation for such…
Norbert Koch
  • 533
  • 6
  • 17
7
votes
8 answers

getDay() method to return day of the week for a given date not works

I'm trying to complete the task named Java Date and Time on HackerRank. Task You are given a date. You just need to write the method, getDay, which returns the day on that date.For example, if you are given the date, August 14th 2017, the…
Said
  • 187
  • 1
  • 2
  • 11
7
votes
5 answers

How to get all the dates in a month using calender class?

Here I want to display dates like 2013-01-01, 2013-01-02, 2013-01-03, . . ...etc I can get total days in a month private int getDaysInMonth(int month, int year) { Calendar cal = Calendar.getInstance(); // or pick another time zone if necessary …
Android_dev
  • 117
  • 1
  • 1
  • 7
5
votes
0 answers

Is there anything in the java.time package that does the same thing as the roll method in java.util.Calendar?

I am currently ditching all usages of java.util Date implementations from my project and want to change everything to Classes from the java.time package. Now I found a usage of the roll method of a java.util.Calendar object and I am wondering how I…
zingi
  • 1,141
  • 13
  • 23
4
votes
1 answer

Question about java.util.Calendar

I'm trying to understand the behavior with the following code. My local time zone is UTC -7 (Arizona). Calendar cal = Calendar.getInstance(); cal.set(Calendar.MINUTE,40); cal.set(Calendar.AM_PM,Calendar.PM); System.out.println("1 UTC -4 Hour:" +…
opike
  • 7,053
  • 14
  • 68
  • 95
4
votes
2 answers

Java Calendar clear() changes DST

First, I want to state that I know the Java Calendar class is being supplanted by other libraries that are arguably better. Perhaps I've stumbled upon one of the reasons Calendar has fallen out of favor. I ran into frustrating behavior in Calendar…
4
votes
3 answers

How to Pick Timezone from ISO 8601 format String into a Calendar instace

As an input I have a string which is a String in ISO 8601 to represent date. For example: "2017-04-04T09:00:00-08:00" The last part of String, which is "-08:00" denotes TimeZone Offset. I convert this string into a Calendar instance as shown…
Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
3
votes
1 answer

GregorianCalendar with a custom cutover date sets unexpected date before cutover

TL;DR: I'm getting a strange result when setting a GregorianCalendar with a custom Julian->Gregorian cutover date. 1 Jan 1800 becomes 12 Jan 1800, where 1800 is before the custom cutover date (31 Jan 1918) but after the default cutover date (15 Oct…
frIT
  • 3,213
  • 1
  • 18
  • 22
3
votes
1 answer

Is there any different behavior between Calendar#add(Calendar.MONTH, months) and LocalDate#plusMonth(months)?

I'm working on some legacy code where java.util.Calendar is used for date related calculations (basically adding months). Now I want to replace java.util.Calendar by java.time.LocalDate in the code but I don't want to change the behavior. So, I've…
Kohei Nozaki
  • 1,154
  • 1
  • 13
  • 36
3
votes
2 answers

What is the equivalent of Calendar.roll in java.time?

I was studying the old Calendar API to see how bad it was, and I found out that Calendar has a roll method. Unlike the add method, roll does not change the values of bigger calendar fields. For example, the calendar instance c represents the date…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
3
votes
1 answer

How does Calender.set(Calender.Month, ?) Work?

I am writing a method that can advance the date by a given number of weeks. Here is my code: public class Date { int year; int month; int day; public Date (int year, int month, int day){ this.year = year; this.month = month; this.day =…
Saad Hassan
  • 111
  • 1
  • 5
3
votes
3 answers

Adding a month to a date in Java two ways gives two different results (leap year philosophy?)

Assuming Jan 31, 2000, a leap year, the following two ways of adding a month give me different results. Am I doing something wrong or are these two different philosophies of handling leap-year months? And, if these two approaches are just a…
Morkus
  • 517
  • 7
  • 21
3
votes
1 answer

Calendar.AM must be one of: Calendar.(Weekdays)

I was checking the code I wrote a few years ago. Then I realised that Android Studio is giving an annotation at Calendar.AM, it says it must be one of Calendar.SUNDAY, Calendar.MONDAY and so on... Calendar c =…
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
1
2 3
9 10