简体   繁体   English

获得第一个星期日,第二个星期二这样的日子

[英]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.谢谢。

java.time and ThreeTenABP java.time 和 ThreeTenABP

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 的,我希望其他字符串与数字对齐。

Question: Can I use java.time on Android?问题:我可以在 Android 上使用 java.time 吗?

Yes, java.time works nicely on older and newer Android devices.是的,java.time 在较旧和较新的 Android 设备上都能很好地工作。 It just requires at least Java 6 .它只需要至少Java 6

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.在 Java 8 及更高版本和更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).在 Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
  • On (older) Android use the Android edition of ThreeTen Backport.在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。 It's called ThreeTenABP.它被称为 ThreeTenABP。 And make sure you import the date and time classes from org.threeten.bp with subpackages.并确保使用子包从org.threeten.bp导入日期和时间类。

Links链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 是否可以使用 navigator.pop 刷新或重新加载页面...例如第 1(nav.push=&gt;2)页到第 2 页,然后从第 2(nav.pop,值)返回到第 1 页? - is it possible to refresh or reload page with navigator.pop... like 1st (nav.push=>2) page to 2nd page and back from 2nd (nav.pop, value) to 1st page? 第一项活动第二项活动获得纬度经度地理位置。 如何将信息转移回第一活动? - 1st activity 2nd activity to get the latitude longitude geo-location. How to transfer info back to 1st activity? 如何从第一个和第二个 position 手动从 Kotlin ZE84E30B9390CDB4864DZDB6C - How to get object from 1st and 2nd position manually from LinkedHashMap in Kotlin Android? 如何根据我的1st Spinner选择来选择2nd Spinner? - How to do 2nd Spinner get selected , based on my 1st Spinner selection? 从第一个活动转到第二个活动时出错 - error when going from 1st activity to 2nd activity 检查第一时间选择器是否早于第二时间选择器 - Check if 1st timepicker earlier than 2nd timepicker 在更改第一微调器值时更改第二微调器值 - changing 2nd spinner values on changing 1st spinner value Java继承:第2个实例调用第1个实例方法 - Java Inheritance : 2nd instance calls 1st instance method 仅当在 recyclerView 中编辑第一个时如何编辑第二个 editText - how to edit 2nd editText only if the 1st is edited in recyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM