如何在java中获取给定月份的最小和最大日期

发布于 2024-10-31 20:39:36 字数 54 浏览 0 评论 0原文

如何使用 java.util.Calendar 在 java 中获取给定月份的最小和最大日期。

how to get minimum and maximum date from given month in java using java.util.Calendar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笛声青案梦长安 2024-11-07 20:39:36

最小值始终是本月 1 日。可以通过将月份加 1 并从日历日字段中减去 1 来确定最大值。

The minimum is always the 1st of this month. The maximum can be determined by adding 1 to month and subtracting 1 from the Calendar day field.

意中人 2024-11-07 20:39:36

可以这样完成:

c = ... // get calendar for month you're interested in     
int numberOfDays = c.getActualMaximum(Calendar.DAY_OF_MONTH)

您可以以相同的方式找到日期的任何组成部分的最小值和最大值。

This could be done this way:

c = ... // get calendar for month you're interested in     
int numberOfDays = c.getActualMaximum(Calendar.DAY_OF_MONTH)

You could find minimum and maximum value the same way for any of components of the date.

人疚 2024-11-07 20:39:36

您尝试过以下方法吗?

将日历对象设置为所需的月份后,
calendar.getActualMaximum(日历.DATE);

对于最小值,我想它总是第一个。

希望有帮助。

Have you tried the following?

After setting your calendar object to your desired month,
calendar.getActualMaximum(Calendar.DATE);

For the minimum, I suppose it's always the first.

Hope that helps.

七婞 2024-11-07 20:39:36

最短日期始终为 1
和最大日期可以计算为

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        int year = 2010;
        int month = Calendar.FEBRUARY;
        int date = 1;
        int maxDay =0;  

        calendar.set(year, month, date);
        System.out.println("First Day: " + formatter.format(calendar.getTime()));

        //Getting Maximum day for Given Month
        maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        calendar.set(year, month, maxDay);  
        System.out.println("Last Day: " + formatter.format(calendar.getTime()));

希望这会有所帮助

Minimum date is always 1
and Maximum date can be calculate as

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        int year = 2010;
        int month = Calendar.FEBRUARY;
        int date = 1;
        int maxDay =0;  

        calendar.set(year, month, date);
        System.out.println("First Day: " + formatter.format(calendar.getTime()));

        //Getting Maximum day for Given Month
        maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        calendar.set(year, month, maxDay);  
        System.out.println("Last Day: " + formatter.format(calendar.getTime()));

Hopefully this will helps

留蓝 2024-11-07 20:39:36

我得到的解决方案如下,

public void ddl_month_valueChange(ValueChangeEvent event) {
 int v_month = Integer.parseInt(event.getNewValue().toString()) - 1; 
 java.util.Calendar c1 = java.util.Calendar.getInstance(); 
 c1.set(2011, v_month, 1); 
 Date d_set_att_from = c1.getTime(); 
 cal_att_from_date.setValue(d_set_att_from);
 c1.add(java.util.Calendar.MONTH, 1);
 c1.add(java.util.Calendar.DATE, -1); 
 Date d_set_att_to = c1.getTime();
 cal_att_to_date.setValue(d_set_att_to);  }

I got solution as below,

public void ddl_month_valueChange(ValueChangeEvent event) {
 int v_month = Integer.parseInt(event.getNewValue().toString()) - 1; 
 java.util.Calendar c1 = java.util.Calendar.getInstance(); 
 c1.set(2011, v_month, 1); 
 Date d_set_att_from = c1.getTime(); 
 cal_att_from_date.setValue(d_set_att_from);
 c1.add(java.util.Calendar.MONTH, 1);
 c1.add(java.util.Calendar.DATE, -1); 
 Date d_set_att_to = c1.getTime();
 cal_att_to_date.setValue(d_set_att_to);  }
记忆里有你的影子 2024-11-07 20:39:36

对于正在使用 Java 8 API 寻找解决方案的人:

给定月份最小日期:

YearMonth.from(clock.instant().atZone(ZoneId.of("UTC")))
            .atDay(1)
            .atStartOfDay()
            .toInstant(ZoneOffset.UTC)

给定月份最大日期:

YearMonth.from(clock.instant().atZone(ZoneId.of("UTC")))
            .atEndOfMonth()
            .atTime(LocalTime.MAX)
            .toInstant(ZoneOffset.UTC)

For people that are looking for solution using Java 8 API:

Given month min date:

YearMonth.from(clock.instant().atZone(ZoneId.of("UTC")))
            .atDay(1)
            .atStartOfDay()
            .toInstant(ZoneOffset.UTC)

Given month max date:

YearMonth.from(clock.instant().atZone(ZoneId.of("UTC")))
            .atEndOfMonth()
            .atTime(LocalTime.MAX)
            .toInstant(ZoneOffset.UTC)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文