英文:
How to get the third wednesday of given year from edittext?
问题
我想制作一个应用,在应用中,用户在编辑框中输入一个数字(年份),当点击按钮时,用户会在文本视图中得到该年份四月份的第三个星期三的日期。例如,他们输入2020年,会得到日期2020年4月15日。这就是2020年四月份的第三个星期三。我的界面有一个编辑框、一个按钮和一个文本视图。谢谢你的帮助。
问题已解决,谢谢!
英文:
I want to make a app where the user puts a number(year)in edittext and when you click the button the user gets in a textview the third wednesday of april from that year. For example they put 2020 and get as a result the date 15.04.2020. that was the third wednesday of april in 2020. I have an edittext, a button and a textview. Thank you for helping.
Problem solved thank you:
答案1
得分: 1
LocalDate与TemporalAdjuster
使用TemporalAdjuster实现TemporalAdjusters.dayOfWeekInMonth。
将您的年份文本输入解析为int,使用Integer类。
获取指定年份的第一天作为LocalDate对象。
Year y = Year.of(2021);
LocalDate startOfYear = y.atDay(1); // 年份的第一天。
移动到四月份。使用不可变对象,这样我们可以得到一个新的对象,而不是改变("变异")原始对象。
LocalDate ld = startOfYear.with(Month.APRIL); // 移动到四月的第一天,我们所需的月份。
获取一个实现月份第三个星期三的时间调整器。
int ordinal = 3; // 月份的第三个这样的日子。
DayOfWeek dow = DayOfWeek.WEDNESDAY;
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth(ordinal, dow);
应用这个调整器以获得另一个LocalDate对象,我们所需的结果,即那个月份的第三个星期三。
LocalDate thirdWednesdayOfApril = ld.with(ta); // 移动到指定年份四月的第三个星期三。
要以特定格式生成文本以供用户查看,可以使用DateTimeFormatter.ofLocalizedDate。搜索了解更多信息,因为这在Stack Overflow上已经多次讨论过。
关于java.time
java.time框架内置于Java 8及更高版本。这些类取代了麻烦的旧的传统日期时间类,如java.util.Date,Calendar和SimpleDateFormat。
要了解更多信息,请参阅Oracle教程。并在Stack Overflow上搜索许多示例和解释。规范是JSR 310。
Joda-Time项目现在处于维护模式,建议迁移到java.time类。
您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC驱动程序。不需要字符串,不需要java.sql.*类。Hibernate 5和JPA 2.2支持java.time。
从哪里获取java.time类?
- Java SE 8、Java SE 9、Java SE 10、Java SE 11以及之后 - 部分标准Java API与捆绑的实现。
- Java 9添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 大部分java.time功能在Java 6和7中进行了后移,使用ThreeTen-Backport。
- Android
- Android的较新版本捆绑了java.time类的实现。
- 对于较早的Android(<26),ThreeTenABP项目适配了上述的ThreeTen-Backport。参见如何使用ThreeTenABP……。
英文:
LocalDate with TemporalAdjuster
Use TemporalAdjuster implementation TemporalAdjusters.dayOfWeekInMonth.
Parse your textual input of year number to an int using Integer class.
Get the first day of your specified year as a LocalDate object.
Year y = Year.of( 2021 ) ;
LocalDate startOfYear = y.atDay( 1 ) ; // First day of the year.
Move to April. Using immutable objects, so we get a new fresh object rather than alter ("mutate") the original.
LocalDate ld = startOfYear.with( Month.APRIL ) ; // Move to the first of April, our desired month.
Get a temporal adjuster implementation for the 3rd Wednesday of the month.
int ordinal = 3 ; // Third such day of the month.
DayOfWeek dow = DayOfWeek.WEDNESDAY ;
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth( ordinal , dow ) ;
Apply the adjuster to get another LocalDate object, our desired result, the third Wednesday of that month.
LocalDate thirdWednesdayOfApril = ld.with( ta ) ; // Move to the 3rd Wednesday of April of the specified year.
To generate text in a certain format for presentation to the user, use DateTimeFormatter.ofLocalizedDate. Search to learn more, as this has been addressed many times already on Stack Overflow.
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.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
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. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 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….
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论