取日,月,年的int并转换为DD / MM / YYYY [英] Take int of day, month, year and convert to DD/MM/YYYY
问题描述
我正在编写一个方法来获取3个整数的DOB - 日,月,年并返回格式化版本DD / MM / YYYY。
I am writing a method to take a DOB of 3 integers - day, month, year and return the formatted version DD/MM/YYYY.
我目前使用dateFormatter和简单的日期格式。虽然当我运行它时默认为01/01/1970并且我无法更改日期。
I am currently using dateFormatter and simple date format. Although when I run this it defaults to 01/01/1970 and I cannot change the date.
有任何建议吗?
更新
感谢下面的帖子,问题解决了!
Guys thanks for the below posts, problem solved!
推荐答案
tl; dr
tl;dr
LocalDate.of( 2017 , 1 , 23 )
.format( DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) )
23/01/2017
23/01/2017
java.time
现代方法使用java.time类。
java.time
The modern way uses the java.time classes.
避免使用旧的旧日期时间类,例如日期
和日历
因为它们设计不当,容易混淆,麻烦和有缺陷。
Avoid the old legacy date-time classes such as Date
and Calendar
as they are poorly designed, confusing, troublesome, and flawed.
LocalDate
表示没有时间且没有时区的仅限日期的值。请注意,与旧版课程不同,这里的月份在1月至12月期间的编号为1-12。
LocalDate
represents a date-only value without time-of-day and without time zone. Note that unlike the legacy classes, here the months have sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 2017 , 1 , 23 );
DateTimeFormatter
使用formatter对象生成表示该值的String。
DateTimeFormatter
Generate a String representing that value by using a formatter object.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
String output = ld.format( f );
这篇关于取日,月,年的int并转换为DD / MM / YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!