获取Java中的最后一天和第一天。

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

Get last and first day using Java

问题

我有一个需求,如下所示:例如,如果今天是星期日,那么我需要获取下个月的第一个星期日和最后一个星期日,对于本周的所有天也是如此。我需要获取下个月的第一天和最后一天。

对于如何实现这个需求,你有什么想法吗?

是否有一种通用的方法来做到这一点,还是我需要分别处理每一天?我已经尝试了许多方法,但我发现每一天都有一个特殊的公式。

我正在使用的技术是Java。

英文:

I have a requirement which is as follows: for example if today is Sunday then I need to get first Sunday and last Sunday in next month, and so on for all days in the week. I need to get the first and last day in next month.

Any idea on how I can do it

Is there a generic way to do it, or do I need to deal with each day separately? I have tried many ways, but all I found is each day has a special formula.

The technology I am using is Java.

答案1

得分: 3

使用Java 8的时间API,您需要使用TemporalAdjusters,例如以下方式:

LocalDate today = LocalDate.now();
DayOfWeek dayOfWeek = today.getDayOfWeek();
LocalDate nextMonth = today.plusMonths(1);
LocalDate first = nextMonth.with(TemporalAdjusters.firstInMonth(dayOfWeek));
LocalDate last = first.with(TemporalAdjusters.lastInMonth(dayOfWeek));

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE, MMM d, uuuu");
System.out.println(today.format(fmt));
System.out.println(first.format(fmt));
System.out.println(last.format(fmt));

输出

Sun, May 3, 2020
Sun, Jun 7, 2020
Sun, Jun 28, 2020
英文:

Using the Java 8 Time API, you need to use TemporalAdjusters, e.g. like this:

LocalDate today = LocalDate.now();
DayOfWeek dayOfWeek = today.getDayOfWeek();
LocalDate nextMonth = today.plusMonths(1);
LocalDate first = nextMonth.with(TemporalAdjusters.firstInMonth(dayOfWeek));
LocalDate last = first.with(TemporalAdjusters.lastInMonth(dayOfWeek));

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE, MMM d, uuuu");
System.out.println(today.format(fmt));
System.out.println(first.format(fmt));
System.out.println(last.format(fmt));

Output

Sun, May 3, 2020
Sun, Jun 7, 2020
Sun, Jun 28, 2020

答案2

得分: 0

你可以尝试这样做:

public static void main(String... args) {
    LocalDate now = LocalDate.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE: dd-MM-yyyy");

    System.out.println("现在 " + now.format(dtf));
    System.out.println("下个月第一天 " + getFirstDayOfNextMonth(now).format(dtf));
    System.out.println("下个月最后一天 " + getLastDayOfNextMonth(now).format(dtf));
}

public static LocalDate getLastDayOfNextMonth(LocalDate localDate) {
    return localDate
            .plusMonths(1)
            .with(TemporalAdjusters.lastInMonth(localDate.getDayOfWeek()));
}

public static LocalDate getFirstDayOfNextMonth(LocalDate localDate) {
    return localDate
            .plusMonths(1)
            .with(TemporalAdjusters.firstInMonth(localDate.getDayOfWeek()));
}

输出:

现在 星期日: 03-05-2020
下个月第一天 星期日: 07-06-2020
下个月最后一天 星期日: 28-06-2020
英文:

You could try this

public static void main(String... args) {
    LocalDate now = LocalDate.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE: dd-MM-yyyy");

    System.out.println("Now " + now.format(dtf));
    System.out.println("First " + getFirstDayOfNextMonth(now).format(dtf));
    System.out.println("Last " + getLastDayOfNextMonth(now).format(dtf));
}

public static LocalDate getLastDayOfNextMonth(LocalDate localDate) {
    return localDate
            .plusMonths(1)
            .with(TemporalAdjusters.lastInMonth(localDate.getDayOfWeek()));
}


public static LocalDate getFirstDayOfNextMonth(LocalDate localDate) {
    return localDate
            .plusMonths(1)
            .with(TemporalAdjusters.firstInMonth(localDate.getDayOfWeek()));
}

Output:

Now Sun: 03-05-2020
First Sun: 07-06-2020
Last Sun: 28-06-2020

答案3

得分: -1

我认为,你可以继续使用今天的日期,再加上7天,然后继续进行下去。
这将是通用的。

例如:

获取日期:2020年5月3日,星期日

加上7天,变为2020年5月10日(是否是下个月?不是的话,继续加7天)

2020年5月17日(是否是下个月?不是的话,继续加7天)

依此类推。

你可以很容易地通过日期接口来进行这些加法操作。

英文:

i think, you can just continue with adding today's date with 7 day and go on.
İt would be generic.

for example:

get date : 03.05.2020 sunday

add 7 day to it, 10.05.2020 (is it on next month? no, son add another 7)

17.05.2020 (is it on next month? no, son add another 7)

etc.

You can easily do this adding things with Date interface

huangapple
  • 本文由 发表于 2020年5月3日 14:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61570689.html
匿名

发表评论

匿名网友

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

确定