【问题标题】:Return time to another class将时间返回到另一个班级
【发布时间】:2016-10-01 13:55:16
【问题描述】:

我正在尝试收集小时的分钟。这似乎在这个类中工作。现在我想在其他类中使用 intTime 进行一些计算。我如何返回 intTime。 在返回实例的属性时,我尝试使用相同的原则,但时间与我使用的任何对象都无关。 getIntTime 可行吗?

import java.text.SimpleDateFormat;
import java.util.*;

public class Time extends Database{
    public Time(){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm:ss");
        String stringTime = sdf.format (cal.getTime());

        int intTime = 0;

        stringTime = stringTime.substring(3,5); // retrieve the minutes (is recorded as string)
        intTime = Integer.parseInt(stringTime);
    }

    public String getStringTime() {
        return intTime;
    }

    public static void main (String[] args) {
    }
}

【问题讨论】:

    标签: java date get return


    【解决方案1】:

    您需要将 intTime 定义为类成员。在您的代码中,intTime 仅在构造函数中“存在”。

    import java.text.SimpleDateFormat;
    import java.util.*;
    
    public class Time extends Database{
        // class member defined in the class but not inside a method.
        private int intTime = 0;
        public Time(){
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm:ss");
            String stringTime = sdf.format (cal.getTime());
    
            // vars defined here, will be gone when method execution is done.
    
            stringTime = stringTime.substring(3,5); // retrieve the minutes (is recorded as string)
    
            // setting the intTime of the instance. it will be available even when method execution is done.
            intTime = Integer.parseInt(stringTime);
        }
    
        public String getStringTime() {
            return intTime;
        }
    
        public static void main (String[] args) {
            // code here
        }
    }
    

    【讨论】:

      【解决方案2】:

      tl;博士

      ZonedDateTime.now( ZoneId.of( "America/Montreal" ) )
                   .get( ChronoUnit.MINUTE_OF_HOUR )
      

      详情

      Chenchuk 的答案是正确的,应该被接受。

      这里涉及的其他一些问题。

      返回一个整数

      您可以将分钟作为 int 原语或 Integer 对象返回,而不是问题中看到的字符串。

      顺便说一句,请避免使用“时间”之类的模棱两可的名称。如果您的意思是分钟,请说出来。

      public int getMinuteOfHour() {
          int m = Integer.parseInt( yourStringGoesHere ) ;
          return m ;
      }
      

      java.time

      所有这些都是不必要的。您正在使用麻烦的旧旧日期时间类,现在已被 java.time 类所取代。 java.time 类已经提供了您的功能。

      您的代码忽略了时区的关键问题。如果省略,则隐式应用 JVM 当前的默认时区。最好具体一点。

      我们将时区定义为ZoneId。使用它来获取ZonedDateTime 作为当前时刻。询问分钟。

      ZoneId z = ZoneId.of( "America/Montreal" );
      ZonedDateTime zdt = ZonedDateTime.now( z );
      int minuteOfHour = zdt.get( ChronoUnit.MINUTE_OF_HOUR );
      

      关于java.time

      java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧日期时间类,例如 java.util.Date.Calendarjava.text.SimpleDateFormat

      Joda-Time 项目现在位于maintenance mode,建议迁移到 java.time。

      要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。

      大部分 java.time 功能在ThreeTen-Backport 中向后移植到Java 6 和7,并进一步适应ThreeTenABP 中的Android(参见How to use…)。

      ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        • 2020-10-08
        相关资源
        最近更新 更多