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.

136 questions
130
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
  • 15,139
  • 20
  • 96
  • 143
12
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
  • 484
  • 1
  • 5
  • 17
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
  • 483
  • 2
  • 15
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
  • 482
  • 4
  • 13
5
votes
7 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
  • 157
  • 1
  • 2
  • 11
5
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
  • 107
  • 1
  • 1
  • 7
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
  • 5,655
  • 13
  • 56
  • 87
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
  • 5,972
  • 4
  • 32
  • 52
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…
fr13d
  • 2,871
  • 1
  • 14
  • 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,019
  • 9
  • 30
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
  • 145,870
  • 17
  • 129
  • 225
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
  • 381
  • 3
  • 13
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,000
  • 10
  • 38
  • 76
3
votes
2 answers

Java Calendar bug?

After a lot of debugging I narrowed my problem to the following snippet: public static void calendarBug() { for (int i=0 ; i<6 ; i++) { Calendar c = Calendar.getInstance(); c.clear(); c.set(2015, 2, 27, i, 0); …
BioRoy
  • 335
  • 1
  • 3
  • 10
3
votes
3 answers

SimpleDateFormat incorrectly parsing string

String s = 19.17.38.008000; DateFormat f = new SimpleDateFormat("HH.mm.ss.SSSSSS"); Date d = f.parse(s); system.out.println(d); this is the code I am running it runs fine except when it prints it prints the time 19:17:46. Please someone explain…
1
2 3
9 10