生成Java Calendar对象时会生成什么? [英] What am I generating when I generate a java Calendar object?

查看:58
本文介绍了生成Java Calendar对象时会生成什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何正确使用Java Calendar对象(必须使用此类来完成我的任务,因此我很想使用一些更好的选项-它们不是选项)

我在这里阅读了文档:

我需要代表几趟火车的到达和离开时间站.我是否应该为每次到达使用单独的Calendar对象时间和出发时间分别?我可以同时将两者包括对象?

如果只有几个日期时间信息实例,请使用上述不同的变量.如果要存储多个实例,则可以使用 List (如果事先知道实例数,则可以使用数组),例如

  import java.time.LocalDate;导入java.time.LocalDateTime;导入java.time.temporal.TemporalAdjusters;导入java.util.ArrayList;导入java.util.List;公共班级主要{公共静态void main(String [] args){列表< 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 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) 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<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]

这篇关于生成Java Calendar对象时会生成什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆