生成Java Calendar对象时,我正在生成什么?

huangapple go评论61阅读模式
英文:

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

问题

以下是您要翻译的内容:

我想知道如何正确使用Java Calendar对象,以便正确使用它们(在我的任务中,必须使用这个类,所以虽然我很想使用一些更好的选项,但它们不是选项)。

我已经阅读了这里的文档:https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#:~:text=A%20Calendar%20object%20can%20produce,以及它们的含义。

但我仍然不明白如何正确使用日历对象。

我需要表示多个火车站的到达和离开时间。我应该为每个到达时间和离开时间分别使用一个单独的Calendar对象吗?我可以在同一个对象中同时包含两者吗?

一个单独的Calendar对象代表什么?它是时间中的一个点(例如年、月、日、小时、分钟)吗?现在我正在为每个站点的到达和离开时间使用单独的对象。这意味着我有大量的Calendar对象。我在正确地使用它们吗?

我的代码片段是:

Calendar TimeArrival = Calendar.getInstance();
Calendar TimeDeparture = Calendar.getInstance();
TimeArrival.set(2020, 8, 20, 0, 1);
TimeDeparture.set(2020, 8, 20, 20, 30);
英文:

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);

答案1

得分: 2

不要使用过时且容易出错的java.util包中的日期/时间API。使用java.time包中的现代日期/时间API。从**教程:日期时间**中了解更多信息。

示例:

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);
    }
}

输出:

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

如果要存储时区信息,请使用ZonedDateTimeOffsetDateTime。根据您的需求从下面的表格中选择适当的类:

生成Java Calendar对象时,我正在生成什么?

如果您有多个火车站的到达和出发时间需要表示,可以为每个到达时间和出发时间分别使用单独的Calendar对象吗?还是可以将它们包含在同一个对象中?

如果只有少数日期时间实例,如上面所述,可以使用不同的变量。如果需要存储多个实例,可以使用List(或者如果事先知道实例数目,可以使用数组),例如:

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());
        // 等等。

        System.out.println(dateTimes);
    }
}

输出:

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

> 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:

生成Java Calendar对象时,我正在生成什么?

> 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) e.g.

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&lt;LocalDateTime&gt; dateTimes = new ArrayList&lt;&gt;();
		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]

答案2

得分: 1

我理解您不希望翻译代码部分,以下是翻译的内容:

我明白您不能将日期和时间存储在现代java.time类的对象中;必须使用设计不佳且过时的Calendar类。现代的ZonedDateTime可能是最佳选择。

> … 我应该为每个到达时间和离开时间单独使用Calendar对象吗?
> 我可以在同一个对象中同时包含两者吗?

是的,在需要同时跟踪两者时,您需要为到达时间和离开时间分别使用不同的对象。不,一个Calendar对象只能容纳一个时间,因此两者都无法放在同一个对象中。

作为补充,让我展示另外两种初始化Calendar对象的方法。

现代方法:无论如何都使用java.time

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

通过这段代码,您已经为当您的老师意识到使用Calendar是一个非常愚蠢的想法,或者您换了一位老师的那一天做好了准备。

老式方法:使用GregorianCalendar构造函数

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

在任何情况下都不要在数字前加零。巧合的是,0001适用于0:01的时间,但当您到达0809时,您的代码将无法再编译通过。在Java(以及许多其他语言中),以0开头的数字被视为八进制数。

英文:

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(&quot;Asia/Tokyo&quot;);
	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.

答案3

得分: 0

回答你的标题问题:你正在生成什么?

计算机将时间表示为从1970年1月1日UTC 00:00:00开始的毫秒数。你的日历还可能包括本地时区的副本。

最后,对Calendar.getInstance的调用可能会返回一个GregorianCalendar对象。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月15日 03:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定