0

This is my programme:

    public class Test {
    public static void main(String []args) {
        Calendar a = Calendar.getInstance();
        a.setTime(new Date(1949-1900,0,24,0,0,0));
        Calendar b=Calendar.getInstance();
        b.setTime(new Date(1951-1900,2,24,0,0,0));
        System.out.println(a.getTime());
        while(!a.equals(b)) {
            a.add(Calendar.DAY_OF_YEAR, 1);
            System.out.println(a.getTime());
        }
        System.out.println(a.getTime());
    }
}

The output are

Fri Nov 23 01:00:00 CST 5285
Sat Nov 24 01:00:00 CST 5285
Sun Nov 25 01:00:00 CST 5285
Mon Nov 26 01:00:00 CST 5285
Tue Nov 27 01:00:00 CST 5285

why hours goes to 1 but not 0? And when I change the date, it may be changed back to 0.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
sss lll
  • 11
  • 4
    Please use the new date time API (LocalDate, LocalDateTime,...) introduced in Java 8. The old API (Date, Calendar,...) has too many flaws. The `Date` constructor you are using is also deprecated. – Axel Apr 19 '20 at 08:04
  • I cannot reproduce. I get Mon Jan 24 00:00:00 CST 1949, Tue Jan 25 00:00:00 CST 1949, etc. up to Sat Mar 24 00:00:00 CST 1951. – Ole V.V. Apr 19 '20 at 10:14
  • Which time zone are you in? Is that CST for Cuba Standard Time, China Standard Time or something else? – Ole V.V. Apr 19 '20 at 10:21
  • ...... In my college my teachers still teach us these apis..... – sss lll Apr 19 '20 at 12:20
  • 2
    Where’s the dislike button? :-) I don’t mean your question or comment, I mean teachers who teach `Calendar` and related classes 6 years after the far better replacement came out deserve to be sacked. – Ole V.V. Apr 19 '20 at 17:55
  • @ssslll - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 16 '20 at 22:53

2 Answers2

2

Why are you still using outdated date/time API? The example given below is not the answer to your question but an example of modern date/time API. I would recommend you not waste time experimenting with outdated API and start using modern date/time API.

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime a = LocalDateTime.now();
        LocalDateTime b = LocalDateTime.now().plusHours(3);
        while (a.getHour() != b.getHour()) {
            System.out.println(a);
            a = a.plusHours(1);
        }
        System.out.println(a);
    }
}

Output:

2020-04-19T09:00:22.444642
2020-04-19T10:00:22.444642
2020-04-19T11:00:22.444642
2020-04-19T12:00:22.444642
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

tl;dr

GregorianCalendar                        // Terrible legacy class that should no longer be used.
.from(                                   // Convert from modern *java.time* object to legacy `Calendar` object.
    LocalDate                            // Represent a date only, without time-of-day and without time zone or offset.
    .of( 1949 , Month.JANUARY , 24 )     // Instantiate a `LocalDate` for a specific date.
    .atStartOfDay(                       // Determine the first moment of the day. Not necessarily 00:00.
        ZoneId.of( "America/Chicago" )   // Specify your desired/expected time zone.
    )                                    // Return a modern `ZonedDateTime` object. 
    .plusDays( 1 )                       // Move to the next day, accounting for any adjustments in the time-of-day because of changes to the offset for that time zone such as Daylight Saving Time (DST). 
)                                        // Return a legacy `Calendar` object, specifically a `GregorianCalendar` object.

Time zone issue likely

I imagine your issue is due to time zones. A java.util.Date is a moment in UTC, while a Calendar object carries a time zone. If you do not specify a time zone, the JVM’s current default time zone is applied implicitly.

java.time

But the issue is moot. You should not be using these terrible date-time classes. They were supplanted years ago by the modern java.time classes defined in JSR 310.

Apparently you want to loop days from 1949-01-24 to 1951-03-24.

LocalDate

For a date-only value, without time-of-day and without time zone, use LocalDate. Notice the sane year numbers, no subtracting 1900.

LocalDate start = LocalDate.of( 1949 , Month.JANUARY , 24 ) ;
LocalDate stop = LocalDate.of( 1951 , Month.MARCH , 24 ) ;

Use Java streams to get a series of LocalDate objects.

Stream < LocalDate > dates = start.datesUntil( stop ) ;  // Generate a series of `LocalDate` objects.
dates.forEach( System.out::println ) ;                   // For each `LocalDate` object, call its `println` method to generate text in standard ISO 8601 format for display on the console.

ZonedDateTime

If you meant to include the first moment of the day as a time-of-day as seen in the wall-clock time used by the people of a certain region, specify a time zone to get a ZonedDateTime.

Do not assume the day starts at 00:00. Some dates in some zones may start at another time such as 01:00. Let java.time determine the first moment by calling LocalDate::atStartOfDay.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdtStart = start.atStartOfDay( z ) ;
ZonedDateTime zdtStop = stop.atStartOfDay( z ) ;

Loop, adding a day at a time.

ZonedDateTime zdt = zdtStart ;
while ( zdt.isBefore( zdtStop ) ) 
{
    System.out.println( zdt ) ;
    zdt = zdt.plusDays( 1 ) ;    // Increment to the next day, taking into account changes in the offset-from-UTC such as Daylight Saving Time (DST). 
}

LocalDateTime cannot represent a moment

If you are trying to track moments, specific points on the timeline, then do not use LocalDateTime.

java.util.Calendar

If you must use java.util.Calendar, to interoperate with old code or crusty instructors not yet updated to java.time, convert. To convert, call the new conversion methods added to the old classes.

A ZonedDateTime object maps to a GregorianCalendar object, a concrete subclass of the abstract Calendar.

ZonedDateTime zdt = zdtStart ;
while ( zdt.isBefore( zdtStop ) ) 
{
    Calendar c = GregorianCalendar.from( zdt ) ;  // Convert to obsolete terrible legacy class.
    zdt = zdt.plusDays( 1 ) ;    // Increment to the next day, taking into account changes in the offset-from-UTC such as Daylight Saving Time (DST). 
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154