简体   繁体   中英

What am I generating when I generate a java Calendar object?

I am wondering how I should be using java Calendar objects in order to use them properly (use of this class is mandatory for my assignment so while I'd love to use some of the better options - they aren't options).

I've read the documentation here: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#:~:text=A%20Calendar%20object%20can%20produce,as%20well%20as%20their%20meaning .

And I still don't understand how to use a calendar object correctly.

I need to represent arrival and departure times for several train stations. Should I use a separate Calendar object for each arrival time and departure time separately? Can I include both in the same object?

What does a single Calendar object represent? Is it a single point in time (ie Year, Month, Day, Hour, Minute)? Right now I'm using separate objects for each station's arrival and departure times. That means I have a large number of Calendar objects. Am I using them correctly?

My code snipet is:

    Calendar TimeArrival = Calendar.getInstance();
    Calendar TimeDeparture = Calendar.getInstance();
    TimeArrival.set  (2020,8,20,00,01);
    TimeDeparture.set(2020,8,20,20,30);

I am wondering how I should be using java Calendar objects

Do not use the outdated error-prone date/time API from java.util package. Use the modern date/time API from java.time package. Learn more about it from Trail: Date Time

An example:

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime dateTimeArrival = LocalDateTime.of(2020, 8, 20, 00, 01);
        LocalDateTime dateTimeDeparture = LocalDateTime.of(2020, 8, 20, 20, 30);
        System.out.println(dateTimeArrival);
        System.out.println(dateTimeDeparture);
    }
}

Output:

2020-08-20T00:01
2020-08-20T20:30

If you want to store timezone information, use ZonedDateTime or OffsetDateTime . Choose the class as per your requirement from the table given below:

在此处输入图像描述

I need to represent arrival and departure times for several train stations. Should I use a separate Calendar object for each arrival time and departure time separately? Can I include both in the same object?

If you have just a few instances of date-time information, use different variables as mentioned above. If you have several instances to store, you can use List (or array if you know the number of instances beforehand) eg

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<LocalDateTime> dateTimes = new ArrayList<>();
        dateTimes.add(LocalDateTime.now());
        dateTimes.add(LocalDateTime.now().plusHours(2));
        dateTimes.add(LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth()));
        dateTimes.add(LocalDate.of(2020, 8, 10).atStartOfDay());
        // etc.

        System.out.println(dateTimes);
    }
}

Output:

[2020-08-14T21:37:14.427085, 2020-08-14T23:37:14.429504, 2020-08-31T21:37:14.429518, 2020-08-10T00:00]

I understand that storing your dates and times in objects of the modern java.time classes is not an option; use of the poorly designed and long-outdated Calendar class is mandatory. The modern ZonedDateTime would probably have been the best choice.

… Should I use a separate Calendar object for each arrival time and departure time separately? Can I include both in the same object?

Yes, you need separate objects for arrival and departure time when you need to keep track of both. No, a Calendar object can hold only one time, so both won't fit.

As a supplement allow me to show two other ways of initializing your Calendar objects.

Modern: Use java.time anyway!

    ZoneId zone = ZoneId.of("Asia/Tokyo");
    ZonedDateTime arrivalZdt = ZonedDateTime.of(2020, 8, 20, 0, 1, 0, 0, zone);
    Calendar timeArrival = GregorianCalendar.from(arrivalZdt);

With this code, you are well prepared for the day when your teacher realizes that using Calendar was a very foolish idea, or you get another teacher.

Old-fashioned: Use the GregorianCalendar constructor

    Calendar timeArrival = new GregorianCalendar(2020, Calendar.AUGUST, 20, 0, 1);

Under no circumstances prefix your numbers with zeroes. Coincidentally 00 and 01 work for a time of 0:01 , but when you get around to 08 or 09 , your code will no longer compile. In Java (and many other languages) numbers that begin with 0 are taken to be octal numbers.

To answer your title question: What are you generating?

Computers represent time as a long number of milliseconds past 00:00:00 Jan 1, 1970 UTC. Your Calendar may also include a copy of the local time zone.

Finally, the call to Calendar.getInstance is likely to return a GregorianCalendar object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM