I have a birthdate in integers:
我有一个整数的生日:
int year, month, day;
and a timestamp.
和时间戳。
long timestamp;
I'm going to check if the birthdate is n years (for example 2 years) younger than my timestamp. How can I do it?
我要检查生日是否比我的时间戳更年轻(例如2年)。我该怎么做?
The minimum API level is 15.
最低API级别为15。
3 个解决方案
#1
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
Parse your count of milliseconds since the epoch reference of first moment of 1970 in UTC as a Instant
.
从UTC的1970年第一时刻的纪元参考作为瞬间解析您的毫秒计数。
Instant instant = Instant.ofEpochMilli( millis ) ;
Adjust from UTC to the time zone in which you want to interpret a date. Understand that for any given moment, the date and time-of-day both vary around the globe by zone.
从UTC调整到您要解释日期的时区。了解在任何特定时刻,日期和时间都会因地区而异。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
以洲/地区的格式指定适当的时区名称,例如America / Montreal,Africa / Casablanca或Pacific / Auckland。切勿使用3-4字母缩写,例如EST或IST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( “Pacific/Auckland” ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Extract only the date portion, without the time-of-day and without the time zone.
仅提取日期部分,没有时间和没有时区。
LocalDate ld = zdt.toLocalDate() ;
Move backwards in time by your desired amount of time.
按时间向后移动所需的时间。
Period p = Period.ofYears( 2 ) ;
LocalDate twoYearsPrior = ld.minus( p ) ;
Represent the birthday.
代表生日。
LocalDate birthday = LocalDate.of( y , m , d ) ;
Compare.
Boolean x = birthday.isBefore( twoYearsPrior ) ;
About java.time
The 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
.
java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧遗留日期时间类,如java.util.Date,Calendar和SimpleDateFormat。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle教程。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310。
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC驱动程序。不需要字符串,不需要java.sql。*类。
Where to obtain the java.time classes?
从哪里获取java.time类?
-
Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
带有捆绑实现的标准Java API的一部分。
Java 9增加了一些小功能和修复。
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
许多java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7。
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
更高版本的Android捆绑java.time类的实现。
对于早期的Android(<26),ThreeTenABP项目采用ThreeTen-Backport(如上所述)。请参见如何使用ThreeTenABP ....
Java SE 8,Java SE 9,Java SE 10和更高版本内置。带有捆绑实现的标准Java API的一部分。 Java 9增加了一些小功能和修复。
Java SE 6和Java SE 7许多java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7。
Android更新版本的Android捆绑java.time类的实现。对于早期的Android(<26),ThreeTenABP项目采用ThreeTen-Backport(如上所述)。请参见如何使用ThreeTenABP ....
#2
1
Try using joda time for lower versions.
尝试将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;
}
#3
0
This might work for you. Make Calendar objects and check the difference in the ms time between them. Basically just converting the date you had in integers to ms instead.
这可能对你有用。制作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");
}
#1
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
Parse your count of milliseconds since the epoch reference of first moment of 1970 in UTC as a Instant
.
从UTC的1970年第一时刻的纪元参考作为瞬间解析您的毫秒计数。
Instant instant = Instant.ofEpochMilli( millis ) ;
Adjust from UTC to the time zone in which you want to interpret a date. Understand that for any given moment, the date and time-of-day both vary around the globe by zone.
从UTC调整到您要解释日期的时区。了解在任何特定时刻,日期和时间都会因地区而异。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
以洲/地区的格式指定适当的时区名称,例如America / Montreal,Africa / Casablanca或Pacific / Auckland。切勿使用3-4字母缩写,例如EST或IST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( “Pacific/Auckland” ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Extract only the date portion, without the time-of-day and without the time zone.
仅提取日期部分,没有时间和没有时区。
LocalDate ld = zdt.toLocalDate() ;
Move backwards in time by your desired amount of time.
按时间向后移动所需的时间。
Period p = Period.ofYears( 2 ) ;
LocalDate twoYearsPrior = ld.minus( p ) ;
Represent the birthday.
代表生日。
LocalDate birthday = LocalDate.of( y , m , d ) ;
Compare.
Boolean x = birthday.isBefore( twoYearsPrior ) ;
About java.time
The 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
.
java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧遗留日期时间类,如java.util.Date,Calendar和SimpleDateFormat。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle教程。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310。
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC驱动程序。不需要字符串,不需要java.sql。*类。
Where to obtain the java.time classes?
从哪里获取java.time类?
-
Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
带有捆绑实现的标准Java API的一部分。
Java 9增加了一些小功能和修复。
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
许多java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7。
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
更高版本的Android捆绑java.time类的实现。
对于早期的Android(<26),ThreeTenABP项目采用ThreeTen-Backport(如上所述)。请参见如何使用ThreeTenABP ....
Java SE 8,Java SE 9,Java SE 10和更高版本内置。带有捆绑实现的标准Java API的一部分。 Java 9增加了一些小功能和修复。
Java SE 6和Java SE 7许多java.time功能都被反向移植到ThreeTen-Backport中的Java 6和7。
Android更新版本的Android捆绑java.time类的实现。对于早期的Android(<26),ThreeTenABP项目采用ThreeTen-Backport(如上所述)。请参见如何使用ThreeTenABP ....
#2
1
Try using joda time for lower versions.
尝试将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;
}
#3
0
This might work for you. Make Calendar objects and check the difference in the ms time between them. Basically just converting the date you had in integers to ms instead.
这可能对你有用。制作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");
}