如何从编辑框中获取给定年份的第三个星期三?

huangapple go评论61阅读模式
英文:

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

LocalDateTemporalAdjuster

使用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.DateCalendarSimpleDateFormat

要了解更多信息,请参阅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类?

英文:

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?

huangapple
  • 本文由 发表于 2020年5月4日 23:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61595416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定