每年如何计算年龄并显示生日吐司消息

时间:2020-06-07 05:56:54

标签: java android date date-of-birth

我正在学习android开发,我想根据当前位置向今天有生日的用户显示通知。我有一个基本的DatePicker,可以使用以下代码从用户那里获取出生日期:

String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int monthValue = datePicker.getMonth();
String month = MONTHS[monthValue];
String dateofBirth = datePicker.getDayOfMonth() + " " + month + " " + datePicker.getYear();

我得到的用户出生日期是以下字符串格式:

24 Jan 1982

是否可以使用上面的日期字符串格式计算用户的生日,并在每年的这一天显示生日通知?例如:

"Today is your 37th birthday!"

我认为此功能不错,但是我不知道如何计算该功能并将其与基于不同位置的今天的日期进行比较:(

非常感谢您的帮助。预先谢谢你。

2 个答案:

答案 0 :(得分:3)

tl; dr

生成表示日期的文本。

LocalDate                                    // Represent a date-only value, without time-of-day and without time zone, using `java.time.LocalDate` class.
.of( 1982 , 1 , 24 )                         // Specify a date. Notice the sane numbering of both year and month, unlike the legacy classes `Date`/`Calendar`. Returns a `LocalDate` object.
.format(                                     // Generate text to represent the value of the `LocalDate` object.
    DateTimeFormatter
    .ofLocalizedDate( FormatStyle.MEDIUM )   // How long or abbreviated do you want the text.
    .withLocale( Locale.UK )                 // Specify a `Locale` to determine the human language and cultural norms to use in localizing.
)                                            // Returns a `String` object.

1982年1月24日

计算年龄。

Period
.between(
    LocalDate.of( 1982 , 1 , 24 ) ,
    LocalDate.now( ZoneId.systemDefault() ) 
)
.getYears()

生日吐司。

MonthDay
.now( 
    ZoneId.systemDefault()   // Specify time zone. For any given moment, the date varies around the globe by time zone.
)
.equals(
    MonthDay.of( 1 , 24 )
)

java.time

现代方法使用 java.time 类。

从您的DatePicker中提取年,月和日,并将这三个都传递给LocalDate的工厂方法。

DatePicker dp = … ;
…
LocalDate localDate = LocalDate.of( dp.getYear() , dp.getMonth() , dp.getDayOfMonth() ) ;

生成文本以呈现给用户时,您可以定义自己的格式设置模式。但是通常让 java.time 自动本地化会更好。

LocalDate localDate = LocalDate.of( 1982 , 1 , 24 ) ;
Locale locale = Locale.UK ; 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = localDate.format( f ) ;

请参阅此code run live at IdeOne.com

1982年1月24日

要计算年龄,请在以下问题上寻找与LocalDate相关的答案:How do I calculate someone's age in Java?

要查看今天是否是他们的生日,请使用MonthDay类。

MonthDay birthday = MonthDay.from( localDate ) ;
ZoneId z = ZoneId.of( "America/Montreal" ) ;  // The time zone through which we want to see today's date.
MonthDay monthDay = MonthDay.now( z ) ;
if( monthDay.equals( birthday ) ) {  … Happy Birthday … }

关于 java.time

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

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

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

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

在哪里获取java.time类?

Table of which java.time library to use with which version of Java or Android

答案 1 :(得分:-1)

使用Date类。永远不要自己处理约会。它很复杂。

https://docs.oracle.com/javase/7/docs/api/java/util/Date.html

这是一个简单的教程

https://www.javatpoint.com/java-string-to-date

因此,您可以检查今天的日期和月份是否相同,然后检查您的生日

编辑:根据之前的评论,似乎Date类不再很酷。请看一下Calendar类

Java: Why is the Date constructor deprecated, and what do I use instead?

甚至更好,如Basil提到的那样,请检查java.time类。尽管我以前从未使用过它们。