【问题标题】:how to check whether the current time is between a preset time frame in android如何检查当前时间是否在android中的预设时间范围内
【发布时间】:2014-06-16 12:35:32
【问题描述】:

嘿,我现在有一个问题,我想检查当前时间 say(10;00am) 是否在预设时间之间
框架说(晚上 10:00 - 凌晨 4:00)我该怎么做。 我尝试了一些并不总是满足条件的东西

public class Test {

    static int flag;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // final Timer timer = new Timer();
        // Timer timer2 = new Timer();

        SimpleDateFormat parser = new SimpleDateFormat("HH.mm");
        Date start = null;
        Date end = null;
        try {
            start = parser.parse("22.00");
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            end = parser.parse("8.00");
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            Date userDate = parser.parse("23.06");
            if (userDate.after(start) && userDate.before(end)) {
                System.out.println("innnnnnnnnnnn");
            } else {
                System.out.println("outttttttttt");
            }
        } catch (ParseException e) {
            // Invalid date was entered
        }



    }
}

【问题讨论】:

    标签: java android date time


    【解决方案1】:

    避免 j.u.Date

    通常,Java 捆绑的 java.util.Date 和 .Calendar 和 SimpleDateFormat 类是出了名的麻烦,应该避免使用。专门针对您的需求,它们结合了日期时间,而您似乎只需要时间。

    改用一个不错的日期时间库。

    当地时间

    Joda-Time(和 java.time)提供了一个 LocalTime 类来表示没有任何时区的时间。获取当前时间时,您应该传递一个 DateTimeZone 以将您的计算机/JVM 的日期时间调整为所需的时区,而不是默认时区。

    半开

    通常在日期时间工作中,比较时间跨度的最佳方法是使用“半开”方法。半开的开头是inclusive,结尾是exclusive。请注意此示例代码中使用 Joda-Time 2.3 进行的比较。

    Joda-Time 示例

    DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
    LocalTime now = LocalTime.now( timeZone ); // Adjust computer/JVM time zone's current time to desired time zone's current time.
    LocalTime start = new LocalTime( "10:00:00" );
    LocalTime stop = new LocalTime( "16:00:00" );
    boolean isNotBeforeStart = ( ! now.isBefore( start ) );  // Is now equal to or after start?
    boolean isBeforeEnd = now.isBefore( stop ); // Is now any time up to, but not including, stop?
    boolean isWithinRange = ( isNotBeforeStart && isBeforeEnd );
    

    【讨论】:

      【解决方案2】:

      我理解您的方法,但您可能忘记了 java.util.Date 实例代表特定的时间点。换句话说,它总是描述特定日期的特定时间。但是,您希望它描述任何天的时间 23:06。

      您可以使用日历而不是日期来获取各个字段(日、月、年、小时等):

          Date yourDateObject = ...;
          GregorianCalendar timeToCheck = new GregorianCalendar();
          timeToCheck.setTime(yourDateObject);
          int hour = timeToCheck.get(GregorianCalendar.HOUR_OF_DAY);
      
          if (hour >= 22 || hour < 8) {
              System.out.println("It's between 10 PM and 8 AM");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        • 1970-01-01
        • 2023-04-05
        • 2016-07-19
        相关资源
        最近更新 更多