Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查某个日期是否比android中的时间戳早n年?_Java_Android_Date_Timestamp - Fatal编程技术网

Java 检查某个日期是否比android中的时间戳早n年?

Java 检查某个日期是否比android中的时间戳早n年?,java,android,date,timestamp,Java,Android,Date,Timestamp,我的生日是整数: int year, month, day; 还有时间戳 long timestamp; 我要检查生日是否比我的时间戳年轻n年(例如2年)。我怎么做 最低API级别为15。尝试在较低版本中使用joda time private int checkDateDiff(long timestamp, int bdayYear, int bdayMonth, int bdayDay) { DateTime startDateTime = new DateTime(times

我的生日是整数:

int year, month, day;
还有时间戳

long timestamp;
我要检查生日是否比我的时间戳年轻n年(例如2年)。我怎么做


最低API级别为15。

尝试在较低版本中使用joda time

private int checkDateDiff(long timestamp, int bdayYear, int bdayMonth, int bdayDay) {

    DateTime startDateTime = new DateTime(timestamp);

    DateTime endDateTime = new DateTime(bdayYear, bdayMonth, bdayDay, 0, 0);

    Period period = new Period(endDateTime, startDateTime);

    int yearsDiff = period.getYears();

    return yearsDiff;
}
tl;博士 java.time 将UTC中1970年第一个时刻的历元引用后的毫秒数解析为
瞬间

Instant instant = Instant.ofEpochMilli( millis ) ;
从UTC调整到要解释日期的时区。要知道,在任何一个特定的时刻,一天中的日期和时间在全球各地都因地区而异

大陆/地区
的格式指定,例如,或
太平洋/奥克兰
。切勿使用3-4个字母的缩写,如
EST
IST
,因为它们不是真正的时区,也不是标准化的,甚至不是唯一的(!)

只提取日期部分,不包括一天中的时间和时区

LocalDate ld = zdt.toLocalDate() ;
按你想要的时间向后移动

Period p = Period.ofYears( 2 ) ;
LocalDate twoYearsPrior = ld.minus( p ) ;
代表生日

LocalDate birthday = LocalDate.of( y , m , d ) ;
比较一下

Boolean x = birthday.isBefore( twoYearsPrior ) ;

关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • 然后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(这可能适用于您。创建日历对象并检查它们之间的毫秒时间差。基本上只是将整数中的日期转换为毫秒

          int year, month, day;
          long timestamp;
          Calendar calendar = Calendar.getInstance();
          calendar.set(year, month+1, day);
      
      
          long twoYears = 63113852000L; //Two years in ms
      
          //Check if the difference is more than 2 years.
          if(calendar.getTimeInMillis() - timestamp >= twoYears || calendar.getTimeInMillis() - timestamp <= -twoYears) {
               System.out.println("More than 2 years dif");
          }
      
      int年、月、日;
      长时间戳;
      日历=Calendar.getInstance();
      日历设置(年、月+1、日);
      长两年=63113852000L;//两年毫秒
      //检查差异是否超过2年。
      
      如果(calendar.getTimeInMillis()-时间戳>=twoYears | | calendar.getTimeInMillis())-时间戳这不起作用,因为您只检查年份。您还需要在计算中添加月和日。一个日期可能从1月开始,一个日期可能在12月。仅供参考,项目现在开始,团队建议迁移到类。请参阅.FYI,非常麻烦的旧日期时间类,如,
      java.text.SimpleDateFormat
      现在已被Java 8和更高版本中内置的类所取代。请参阅。回答不错,但您的解决方案需要API级别26,当前的最小值为15。
      Boolean x = birthday.isBefore( twoYearsPrior ) ;
      
          int year, month, day;
          long timestamp;
          Calendar calendar = Calendar.getInstance();
          calendar.set(year, month+1, day);
      
      
          long twoYears = 63113852000L; //Two years in ms
      
          //Check if the difference is more than 2 years.
          if(calendar.getTimeInMillis() - timestamp >= twoYears || calendar.getTimeInMillis() - timestamp <= -twoYears) {
               System.out.println("More than 2 years dif");
          }