[英]Get the day like 1st Sunday, 2nd Tuesday
I want to get the day (eg 1st Sunday, 2nd Tuesday) of this month as Calendar.我想将本月的一天(例如第一个星期日,第二个星期二)作为日历。 How can I achieve this with class Calendar in Android?
如何使用 Android 中的 Calendar 类实现此目的? Thanks.
谢谢。
Edit: The elegant solution uses a DateTimeFormatter
:编辑:优雅的解决方案使用
DateTimeFormatter
:
Locale language = Locale.ENGLISH;
Map<Long, String> ordinalNumber = new HashMap<>(8);
ordinalNumber.put(1L, "1st");
ordinalNumber.put(2L, "2nd");
ordinalNumber.put(3L, "3rd");
ordinalNumber.put(4L, "4th");
ordinalNumber.put(5L, "5th");
DateTimeFormatter dayFormatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.ALIGNED_WEEK_OF_MONTH, ordinalNumber)
.appendPattern(" EEEE 'of the month'")
.toFormatter(language);
LocalDate date = LocalDate.now(ZoneId.of("Pacific/Auckland"));
String dayString = date.format(dayFormatter);
System.out.println("" + date + " is the " + dayString);
When I ran this snippet just now, it output:当我刚刚运行这个片段时,它输出:
2019-06-05 is the 1st Wednesday of the month
2019-06-05 是每个月的第一个星期三
Obviously you can put in any date you wish other than today's date in New Zealand.显然,除了今天在新西兰的日期之外,您可以输入任何您希望的日期。
The concept of aligned week of month defines the first week of the month as days 1 through 7, the 2nd week is 8 through 14, etc. So when a Wednesday is in week 1 according to this scheme, we also know that it must be the 1st Wednesday of the month.月份对齐周的概念将一个月的第一周定义为第 1 到 7 天,第 2 周是第 8 到 14 天,依此类推。因此,根据此方案,当星期三在第 1 周时,我们也知道它必须是每月的第一个星期三。 Same argument for other numbers.
其他数字的相同论点。
I am not using the Calendar
class you mentioned since it is poorly designed and long outdated.我没有使用您提到的
Calendar
类,因为它的设计很差并且已经过时了。 Instead I am using java.time, the modern Java date and time API.相反,我使用的是 java.time,现代 Java 日期和时间 API。
Original code (a bit shorter, but nevertheless does a little more hand work):原始代码(有点短,但仍然做了更多的手工工作):
String[] ordinalNumber = { "0th", "1st", "2nd", "3rd", "4th", "5th" };
LocalDate date = LocalDate.now(ZoneId.of("Pacific/Auckland"));
DayOfWeek day = date.getDayOfWeek();
int numberDowOfMonth = date.get(ChronoField.ALIGNED_WEEK_OF_MONTH);
String dayString = String.format(language, "%s %s of the month",
ordinalNumber[numberDowOfMonth],
day.getDisplayName(TextStyle.FULL_STANDALONE, language));
System.out.println("" + date + " is the " + dayString);
Output is the same as above.输出和上面一样。
There's an unused 0th element of my ordinalNumber
array.我的
ordinalNumber
数组有一个未使用的第 0 个元素。 I only put it there because arrays are 0-based in Java and I wanted the other strings to be aligned with the numbers.我把它放在那里是因为数组在 Java 中是基于 0 的,我希望其他字符串与数字对齐。
Yes, java.time works nicely on older and newer Android devices.是的,java.time 在较旧和较新的 Android 设备上都能很好地工作。 It just requires at least Java 6 .
它只需要至少Java 6 。
org.threeten.bp
with subpackages.org.threeten.bp
导入日期和时间类。java.time
was first described. java.time
。java.time
to Java 6 and 7 (ThreeTen for JSR-310). java.time
到 Java 6 和 7 的 backport(ThreeTen for JSR-310)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.