Java 日历未更改值

Java Calendar Not Changing Values

我正在编写一个应用程序来接收 java 中过去的股票数据。 在计算六天平均值时,我使用日历来选择开始和结束日期,但这不起作用,因为尽管在后者中减去天数,但由于结束日历与开始日历相同...

    private BigDecimal calcMovingAvg(int days, Calendar start){

    Calendar to = start;

    int temp = Functions.weekdays(start.getTime(), days);
    temp = temp - (2 * temp);

    start.add(start.DAY_OF_MONTH, temp);

    BigDecimal d = new BigDecimal(String.valueOf(days));

    List<HistoricalQuote> histQuotes = null;
    try {
        //Calling a method to get stock history between start and to date
        histQuotes = stk.getHistory(start, to, Interval.DAILY);
        System.out.println(histQuotes);
    } catch (IOException e) {
        e.printStackTrace();
    }

Start 已定义且有效

我保存到=开始,以便在取前 6 天的平均值时有一个结束日期

Functions.weekdays 计算这 6 天中有多少天实际上是工作日(工作日)并相应地调整数字(temp 是获得 6 个工作日所需的天数)。

比较时,我开始==,为什么开始没有改变?

    Calendar to = (Calendar) start.clone();

    int temp = Functions.weekdays(start.getTime(), days);
    temp = temp - (2 * temp);

    start.add(start.DAY_OF_MONTH, temp);

日历使用的是同一个实例。克隆日历解决了这个问题,因为它创建了两个独立的实体。

java.time

使用 Java 8 及更高版本中内置的 java.time 框架可以更轻松地完成这项工作。避免使用旧的日期时间 类,例如 java.util.Calendar,因为它们已被证明设计不佳且很麻烦。

不可变对象

java.time 类 使用 immutable objects,根据旧对象的值生成新对象,而不是更改(“变异”)原始对象。这种做法避免了Question中遇到的问题。

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( zoneId );
ZonedDateTime later = now.plusWeeks( 1 );  // Generates a second object without modifying the first.

转换

最好完全避免使用旧的 类,包括 java.util.Date 和 java.util.Calendar。但如果你必须,你可以转换。

已将新方法添加到旧方法 类 中以方便转换。

呼叫GregorianCalendar::toZonedDateTime to generate an equivalent ZonedDateTime object. I your Calendar object is indeed a GregorianCalendar, downcast.

if( cal instanceof GregorianCalendar ) {
    GregorianCalendar gCal = ( GregorianCalendar ) cal;  // Cast. Down-casting from interface to concrete class.
    ZonedDateTime zdt = gCal.toZonedDateTime();
}

ZonedDateTime.

调用静态方法 GregorianCalendar.from( ZonedDateTime) to get a GregorianCalendar object (which implements the Calendar 接口)
java.util.Calendar cal = java.util.GregorianCalendar.from( later );

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类.

java.time类在哪里获取?

  • Java SE 8, Java SE 9, Java SE 10,及以后
    • 内置。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and Java SE 7
  • Android
    • Android java.time 类.
    • 捆绑实施的更高版本
    • 对于较早的 Android (<26),ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See .

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.