簡體   English   中英

java 從 ZonedDateTime 獲取月份中的哪一天

[英]java get which day of month from ZonedDateTime

我想從給定的ZonedDateTime找出月份中的哪一天。 我知道今天是星期四,想知道每月的哪個星期四? 例如:

  • 2021-06-10T13:30:30+03:00 是 2. 星期四;

  • 2021-06-17T13:30:30+03:00 是 3. 星期四;

  • 2021-06-24T13:30:30+03:00 是上周四;

  • 等等

    private static int getWhicDayOfMonth(ZonedDateTime date) { }

這樣做的簡單方法是什么?

 private static int getWhicDayOfMonth(ZonedDateTime date) {
    return (date.getDayOfMonth() - 1) / 7 + 1;
 }

在您的示例中:

10 -> 2
17 -> 3
24 -> 4
31 (in some months) -> 5

或者簡化為date.getDayOfMonth() + 6) / 7 - 但我傾向於覺得這更令人困惑

從當月第一次出現星期幾開始,以 7 天的步長值循環遍歷該月的所有日子,並不斷增加計數器,直到匹配給定的日期。

演示:

import java.time.DayOfWeek;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(whichDowOfTheMonth("2021-06-10T13:30:30+03:00"));
        System.out.println(whichDowOfTheMonth("2021-06-17T13:30:30+03:00"));
        System.out.println(whichDowOfTheMonth("2021-06-24T13:30:30+03:00"));
    }

    static int whichDowOfTheMonth(String strDateTime) {
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);
        DayOfWeek dow = zdt.getDayOfWeek();

        ZonedDateTime zdtFirstDowOfMonth = zdt.with(TemporalAdjusters.firstInMonth(dow));
        ZonedDateTime zdtLastDayOfMonth = zdt.with(TemporalAdjusters.lastDayOfMonth());

        int count = 1;
        for (ZonedDateTime date = zdtFirstDowOfMonth; !date.isAfter(zdtLastDayOfMonth); date = date.plusDays(7)) {
            if (date.equals(zdt))
                break;
            count++;
        }
        return count;
    }
}

Output:

2
3
4

在線演示

Trail: Date Time了解有關現代日期時間 API 的更多信息。

基本上與@ArvindKumarAvinash 提供的答案中的方法相同,但循環有點不同:

/**
 * <p>
 * Calculates which weekday of the month it is
 * in the {@link ZonedDateTime} passed.
 * </p>
 *  
 * @param zonedDateTime the datetime to calculate its "weekday of month"
 * @return the weekday of month
 */
private static int getWeekdayOfMonth(ZonedDateTime zonedDateTime) {
    // extract the values you are interested in from the datetime passed
    int dayOfMonth = zonedDateTime.getDayOfMonth();
    DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
    // provide a counter variable
    int weekdayCount = 0;
    // then iterate until the day of month and compare the days of week
    for (int d = 1; d <= dayOfMonth; d++) {
        if (zonedDateTime.withDayOfMonth(d).getDayOfWeek() == dayOfWeek) {
            // increase counter at every match (including the one passed)
            weekdayCount++;
        }
    }
    
    return weekdayCount;
}

你可以像這樣使用它:

public static void main(String[] args) {
    ZonedDateTime zdt = OffsetDateTime.parse("2021-06-17T13:30:30+03:00")
                                      .atZoneSameInstant(ZoneId.of(
                                                             "Europe/Istanbul"
                                                         )
                                      );
    System.out.println("The date "
                    + zdt.toLocalDate()
                    + " is "
                    + zdt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)
                    + " no. "
                    + getWeekdayOfMonth(zdt)
                    + " in "
                    + zdt.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
}

並收到回復

The date 2021-06-17 is Thursday no. 3 in June

在您的控制台中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM