刷题去WLB965 一起来做题(某房地产公司的面试题)

lyyyyyyy · November 03, 2021 · Last by 团长 replied at November 09, 2021 · 3622 hits

判断今天是不是本月的最后一天

java 版本

public void lastDayOfThisMonth(){
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
       Date date = new Date();
       int today = Integer.parseInt(simpleDateFormat.format(date));
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(date);
       int actualMaximum = calendar.getActualMaximum(Calendar.DATE);
       String result = today == actualMaximum ? "today is the last day of this month" : "today is not the last day of this month";
       System.out.println(result);
   }

python 版本

def func():
    year = int(time.strftime("%Y"))
    month = int(time.strftime("%m"))
    today = int(time.strftime("%d"))
    last_day = calendar.monthrange(year, month)[1]
    result = "today is the last day of this month" if (today == last_day) else "today is not the last day of this month"
    print(result)
共收到 7 条回复 时间 点赞

考这个题目 既没有考到算法思维,也没考到什么特别的经验实际用法(只有一个日历相关的),这啥公司这么无聊

java8 一行就行了

System.out.println(
LocalDate.now().equals(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()))
?"最后一天":"不是最后一天");

既不是常用库 也不涉及算法 意义不太明白

Ha_ha 回复

不太清楚,可能只是考察思路。

有种脚趾头缝里硬掰出来的一道题的感觉😂

6Floor has deleted

python 这样写应该简单一点吧

def islastday():
    today=datetime.datetime.now()
    tomorrow =today+datetime.timedelta(days=1)
    return "今天不是最后一天" if tomorrow .month==today.month else "今天是最后一天"
BigDel 回复

这个思路不错

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up