Java 使用 Threeten, LocalDate 获取一个月的第一天和最后一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22223786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:25:19  来源:igfitidea点击:

Get first and last day of month using threeten, LocalDate

javadatejava-time

提问by user1746050

I have a LocalDate which needs to get the first and last day of the month. How do I do that?

我有一个 LocalDate 需要获取该月的第一天和最后一天。我怎么做?

eg. 13/2/2014I need to get 1/2/2014and 28/2/2014in LocalDateformats.

例如。13/2/2014我需要1/2/201428/2/2014LOCALDATE的格式。

Using threeten LocalDate class.

使用三个 LocalDate 类。

回答by Jon Skeet

Just use withDayOfMonth, and lengthOfMonth():

只需使用withDayOfMonth, 和lengthOfMonth()

LocalDate initial = LocalDate.of(2014, 2, 13);
LocalDate start = initial.withDayOfMonth(1);
LocalDate end = initial.withDayOfMonth(initial.lengthOfMonth());

回答by Shekhar Rajput

Try this:

尝试这个:

LocalDate initial = LocalDate.of(2014, 2, 13);
LocalDate start = initial.withDayOfMonth(1);         
LocalDate end = initial.withDayOfMonth(initial.getMonthOfYear().getLastDayOfMonth(false));
System.out.println(start);
System.out.println(end);

you can find the desire output but need to take care of parameter true/falsefor getLastDayOfMonthmethod

你可以找到的愿望输出,但需要照顾参数的真/假getLastDayOfMonth方法

that parameter denotes leap year

该参数表示闰年

回答by Meno Hochschild

Jon Skeets answer is right and has deserved my upvote, just adding this slightly different solution for completeness:

Jon Skeets 的回答是正确的,值得我点赞,只是为了完整性添加了这个略有不同的解决方案:

import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;

LocalDate initial = LocalDate.of(2014, 2, 13);
LocalDate start = initial.withDayOfMonth(1);
LocalDate end = initial.with(lastDayOfMonth());

回答by JodaStephen

The API was designed to support a solution that matches closely to business requirements

API 旨在支持与业务需求紧密匹配的解决方案

import static java.time.temporal.TemporalAdjusters.*;

LocalDate initial = LocalDate.of(2014, 2, 13);
LocalDate start = initial.with(firstDayOfMonth());
LocalDate end = initial.with(lastDayOfMonth());

However, Jon's solutions are also fine.

但是,Jon 的解决方案也很好。

回答by herman

YearMonth

YearMonth

For completeness, and more elegant in my opinion, see this use of YearMonthclass.

为了完整起见,在我看来更优雅,请参阅YearMonthclass 的这种用法。

YearMonth month = YearMonth.from(date);
LocalDate start = month.atDay(1);
LocalDate end   = month.atEndOfMonth();

For the first & last day of the current month, this becomes:

对于当月的第一天和最后一天,这变为:

LocalDate start = YearMonth.now().atDay(1);
LocalDate end   = YearMonth.now().atEndOfMonth();

回答by Anil Bhaskar

If anyone comes looking for first day of previous monthand last day of previous month:

如果有人来寻找前一个月的第一天,上月的最后一天

public static LocalDate firstDayOfPreviousMonth(LocalDate date) {
        return date.minusMonths(1).withDayOfMonth(1);
    }


public static LocalDate lastDayOfPreviousMonth(LocalDate date) {
        return date.withDayOfMonth(1).minusDays(1);
    }

回答by MrSmith42

if you want to do it only with the LocalDate-class:

如果您只想使用 LocalDate 类来执行此操作:

LocalDate initial = LocalDate.of(2014, 2, 13);

LocalDate start = LocalDate.of(initial.getYear(), initial.getMonthValue(),1);

// Idea: the last day is the same as the first day of next month minus one day.
LocalDate end = LocalDate.of(initial.getYear(), initial.getMonthValue(), 1).plusMonths(1).minusDays(1);

回答by Luis Roberto

Just here to show my implementation for @herman solution

只是在这里展示我对@herman 解决方案的实现

ZoneId americaLaPazZone = ZoneId.of("UTC-04:00");

static Date firstDateOfMonth(Date date) {
  LocalDate localDate = convertToLocalDateWithTimezone(date);
  YearMonth baseMonth = YearMonth.from(localDate);
  LocalDateTime initialDate = baseMonth.atDay(firstDayOfMonth).atStartOfDay();
  return Date.from(initialDate.atZone(americaLaPazZone).toInstant());
}

static Date lastDateOfMonth(Date date) {
  LocalDate localDate = convertToLocalDateWithTimezone(date);
  YearMonth baseMonth = YearMonth.from(localDate);
  LocalDateTime lastDate = baseMonth.atEndOfMonth().atTime(23, 59, 59);
  return Date.from(lastDate.atZone(americaLaPazZone).toInstant());
}

static LocalDate convertToLocalDateWithTimezone(Date date) {
  return LocalDateTime.from(date.toInstant().atZone(americaLaPazZone)).toLocalDate();
}

回答by Valery Streltsov

You can try this to avoid indicating custom date and if there is need to display start and end dates of current month:

您可以尝试这样做以避免指示自定义日期以及是否需要显示当月的开始和结束日期:

    LocalDate start = LocalDate.now().minusDays(LocalDate.now().getDayOfMonth()-1);
    LocalDate end = LocalDate.now().minusDays(LocalDate.now().getDayOfMonth()).plusMonths(1);
    System.out.println("Start of month: " + start);
    System.out.println("End of month: " + end);

Result:

结果:

>     Start of month: 2019-12-01
>     End of month: 2019-12-30

回答by dersu

 LocalDate monthstart = LocalDate.of(year,month,1);
 LocalDate monthend = monthstart.plusDays(monthstart.lengthOfMonth()-1);