简体   繁体   English

当试图在 java 中获取下个月的最后一个日期时

[英]When trying to get last date of next month getting last date of next month of next year in java

Trying to run this code it was working perfectly fine till OCT but in NOV it is like firstdate 2019-12-01 & lastdate 2020-12-31尝试运行此代码在OCT之前一直运行良好,但在NOV月它就像firstdate 2019-12-01 & lastdate 2020-12-31

public class Test1 {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();         
        calendar.add(Calendar.MONTH, 1);
        String date;    
        calendar.set(Calendar.DATE,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date nextMonthFirstDay = calendar.getTime();
        date=new SimpleDateFormat("YYYY-MM-dd").format(nextMonthFirstDay).toLowerCase();
        System.out.println("firstdate "+ date);

        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date nextMonthLastDay = calendar.getTime();
        date=new SimpleDateFormat("YYYY-MM-dd").format(nextMonthLastDay).toLowerCase();
        System.out.println("lastdate  "+date);

    }
}

I don't know why it is showing like this.. Is it a fault or bug in java?我不知道为什么会这样显示。它是 java 中的故障还是错误?

Change your date format to yyyy-MM-dd (notice lowercase for year)将日期格式更改为yyyy-MM-dd (注意年份小写)

They both represent a year but yyyy represents the calendar year while YYYY represents the year of the week.它们都代表一年,但yyyy代表日历年,而YYYY代表一周中的年份。

So something like...所以像...

date=new SimpleDateFormat("yyyy-MM-dd").format(nextMonthLastDay).toLowerCase();

Hope it helps!希望能帮助到你!

You seem to already have got an answer that works, however, here is one that uses the modern datetime API java.time and is s little more readable than the way you are calculating the first and last day of the next month based on today:您似乎已经得到了一个有效的答案,但是,这是一个使用现代日期时间 API java.time的答案,并且您根据今天计算下个月的第一天和最后一天的方式更具可读性:

public static void main(String[] args) {
    // base is today
    LocalDate today = LocalDate.now();
    /*
     * create a LocalDate from 
     * - the year of next month (may be different)
     * - the current month plus 1 and 
     * - the first day
     * ——> first day of next month
     */
    LocalDate firstDayOfNextMonth = LocalDate.of(
            today.plusMonths(1).getYear(),
            today.getMonth().plus(1),
            1);
    /*
     * create a LocalDate from 
     * - the first day of next month (just created above)
     * - add a month and
     * - subtract one day
     * ——> last day of next month
     */
    LocalDate lastDayOfNextMonth = firstDayOfNextMonth.plusMonths(1).minusDays(1);

    // print the results
    System.out.println("first date of upcoming month:\t"
                        + firstDayOfNextMonth.format(DateTimeFormatter.ISO_DATE));
    System.out.println("last date of upcoming month:\t"
                        + lastDayOfNextMonth.format(DateTimeFormatter.ISO_DATE));
}

Don't be misled by the formatting, there are significantly fewer lines of code and their output is不要被格式误导,代码行数要少得多,它们的 output 是

first date of upcoming month:   2019-12-01
last date of upcoming month:    2019-12-31

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM