java 日历的getActualMinimum返回错误值

vwkv1x7d  于 2023-01-24  发布在  Java
关注(0)|答案(2)|浏览(129)

我需要将某个日期与当前日期/时间进行比较,以确保它在当前月份的第一天/小时/分钟/秒之前。为了实现此功能,a使用getActualMinimum方法配置了一个Calendar示例,然而,今天(星期四,19/01/2023 - 10:40:18 BRT 2023),它出现了我以前从未遇到过的行为。请考虑以下代码:

Calendar cal = Calendar.getInstance();
    System.out.println("After instantiation:                  " + cal.getTime());
    
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    System.out.println("After configuring the Day of Month:   " + cal.getTime());
    
    cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
    System.out.println("After configuring the Hour of day:    " + cal.getTime());
    
    cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
    System.out.println("After configuring the Minutes:        " + cal.getTime());
    
    cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
    System.out.println("After configuring the Seconds:        " + cal.getTime());
    
    cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
    System.out.println("After configuring the Millis:         " + cal.getTime());

上面的代码,在这个帖子被创建的时候,会打印到控制台:

After instantiation:                  Thu Jan 19 10:40:18 BRT 2023
After configuring the Day of Month:   Sun Jan 01 10:40:18 BRT 2023
After configuring the Hour of day:    Sat Dec 31 23:40:18 BRT 2022
After configuring the Minutes:        Sat Dec 31 23:00:18 BRT 2022
After configuring the Seconds:        Sat Dec 31 23:00:00 BRT 2022
After configuring the Millis:         Sat Dec 31 23:00:00 BRT 2022

有人能解释一下为什么在配置了小时后,该值被设置为23而不是00吗?
编辑:我使用的是Java8,特别是JDK 1.8.0_241
我当前和默认的时区是 *Horário Padrão de布拉西利亚 *(BRT或GMT-3)

w7t8yxp5

w7t8yxp51#

Java.时间

java.util Date-Time API及其格式化API SimpleDateFormat已过时且容易出错。建议完全停止使用它们并切换到modern Date-Time API
java.time是现代的日期-时间API,specialized types可以用于不同的用途,一个非常常见的类型是ZonedDateTime,它包含时区信息以及日期和时间信息。

    • 注意**:与java.util日期时间类型不同,java.time类型是不可变的,即您总是在设置新值时获得新示例;因此,与String类似,如果希望引用指向新值,则需要将新值赋给引用。
    • 演示**:
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        // Replace ZoneId.systemDefault() with the applicable ZoneId e.g.
        // ZoneId.of("America/New_York")
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.systemDefault());
        System.out.println(zdt);

        zdt = zdt.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(zdt);

        zdt = zdt.withHour(LocalTime.MIN.getHour());
        System.out.println(zdt);

        zdt = zdt.withMinute(LocalTime.MIN.getMinute());
        System.out.println(zdt);

        zdt = zdt.withSecond(LocalTime.MIN.getSecond());
        System.out.println(zdt);

        zdt = zdt.with(ChronoField.MILLI_OF_SECOND, LocalTime.MIN.getLong(ChronoField.MILLI_OF_SECOND));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
        System.out.println(zdt.format(formatter));

        // In a single statement
        String output = ZonedDateTime.now(ZoneId.systemDefault())
                .with(TemporalAdjusters.firstDayOfMonth())
                .withHour(LocalTime.MIN.getHour())
                .withMinute(LocalTime.MIN.getMinute())
                .withSecond(LocalTime.MIN.getSecond())
                .with(ChronoField.MILLI_OF_SECOND, LocalTime.MIN.getLong(ChronoField.MILLI_OF_SECOND))
                .format(formatter);
        System.out.println(output);

        // There is a better way if all you want is day-1 with minimum time
        zdt = LocalDate.now(ZoneId.systemDefault())
                .with(TemporalAdjusters.firstDayOfMonth())
                .atStartOfDay()
                .atZone(ZoneId.systemDefault());
        System.out.println(zdt.format(formatter));
    }
}
    • 示例运行的输出**:
2023-01-19T16:50:43.811714Z[GMT]
2023-01-01T16:50:43.811714Z[GMT]
2023-01-01T00:50:43.811714Z[GMT]
2023-01-01T00:00:43.811714Z[GMT]
2023-01-01T00:00:00.811714Z[GMT]
2023-01-01T00:00:00.000Z
2023-01-01T00:00:00.000Z
2023-01-01T00:00:00.000Z

ONLINE DEMO
从**Trail: Date Time**了解有关现代日期-时间API的更多信息。

如果您需要使用旧版API的解决方案:

Calendar#getTime返回java.util.Date的示例,该示例不是一个实时日期-时间对象;相反,它只包含从 * January 1,1970,00:00:00 GMT * 开始的毫秒数。Date#toString应用系统的时区来计算日期-时间,并返回相同的值。
获取带有所需时区的日期-时间字符串的方法是将时区应用于SimpleDateFormat,并使用它来格式化java.util.Date的示例。

    • 演示**:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

class Main {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS z yyyy", Locale.ENGLISH);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        Calendar cal = Calendar.getInstance();
        System.out.println("After instantiation:                  " + sdf.format(cal.getTime()));

        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        System.out.println("After configuring the Day of Month:   " + sdf.format(cal.getTime()));

        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        System.out.println("After configuring the Hour of day:    " + sdf.format(cal.getTime()));

        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        System.out.println("After configuring the Minutes:        " + sdf.format(cal.getTime()));

        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        System.out.println("After configuring the Seconds:        " + sdf.format(cal.getTime()));

        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        System.out.println("After configuring the Millis:         " + sdf.format(cal.getTime()));
    }
}
    • 示例运行的输出**:
After instantiation:                  Thu Jan 19 15:29:38.381 UTC 2023
After configuring the Day of Month:   Sun Jan 01 15:29:38.381 UTC 2023
After configuring the Hour of day:    Sun Jan 01 00:29:38.381 UTC 2023
After configuring the Minutes:        Sun Jan 01 00:00:38.381 UTC 2023
After configuring the Seconds:        Sun Jan 01 00:00:00.381 UTC 2023
After configuring the Millis:         Sun Jan 01 00:00:00.000 UTC 2023

ONLINE DEMO

yfjy0ee7

yfjy0ee72#

这可能是由PC的默认时区引起的,请尝试在声明Calnedar对象后添加此时区

cal.setTimeZone(TimeZone.getTimeZone("UTC"));

相关问题