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

时间:2018-07-21 15:42:16

标签: java android date timestamp

我的生日是整数:

int year, month, day;

和时间戳。

long timestamp;

我要检查生日是否比我的时间戳小n年(例如2岁)。我该怎么办?

最低API级别为15。

3 个答案:

答案 0 :(得分:2)

tl; dr

Instant                               // `Instant` = moment in UTC. Resolved in nanoseconds, much finer than the milliseconds seen in the Question.
.ofEpochMilli( 1_532_197_770_716L )   // Parse a count-from-epoch as an `Instant` object.
.atZone(                              // Adjust from UTC to a particular time zone.
    ZoneId.of( "America/Montreal" )   // Always use `Contintent/Region` formatted names, never the 3-4 letter pseudo-zones such as `PST` or `IST`.
)                                     // Returns a `ZonedDateTime` object. Think of it conceptually as: ZonedDateTime = ( Instant + ZoneId ). Represents the same moment, the same point on the timeline, but viewed through the lens of the wall-clock time used by the people of a particular region.
.toLocalDate()                        // Returns a `LocalDate`, date-only value without time-of-day and without time zone.
.minus(                               // Subtract a span-of-time.
    Period.ofYears( 2 )               // `Period` = a span of time unattached to the timeline, in granularity of a number of years-months-days.
)                                     // Returns another `LocalDate`. Using immutable objects pattern, producing a new object rather than altering (“mutating”) the original.
.isAfter(                             // Compare one `LocalDate` with another.
    LocalDate.of( yourYear , yourMonth , yourDay )
)                                     // Returns a boolean.

java.time

将您以UTC 1970年第一时刻的纪元参考以来的毫秒数解析为Instant

Instant instant = Instant.ofEpochMilli( millis ) ;

从UTC调整到您想要解释日期的时区。要了解的是,在任何给定时刻,日期和时间在全球范围内都会有所不同。

continent/region的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用3-4个字母的缩写,例如ESTIST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( “Pacific/Auckland” ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

仅提取日期部分,而不提取时间和时区。

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

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

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

在哪里获取java.time类?

答案 1 :(得分:1)

尝试将joda时间用于较低版本。

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;
}

答案 2 :(得分:0)

这可能对您有用。制作Calendar对象并检查它们之间的ms时间差异。基本上只是将整数日期转换为ms。

    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");
    }