使用Java Calendar类获取一周的开始日期和结束日期

使用Java Calendar类获取一周的开始日期和结束日期,第1张

使用Java Calendar类获取一周的开始日期和结束日期

一些代码如何处理

Calendar
对象。我还应该提到joda时间库,因为它可以帮助您解决许多
Date/Calendar
问题。

public static void main(String[] args) {    // set the date    Calendar cal = Calendar.getInstance();    cal.set(2011, 10 - 1, 12);    // "calculate" the start date of the week    Calendar first = (Calendar) cal.clone();    first.add(Calendar.DAY_OF_WEEK,    first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK));    // and add six days to the end date    Calendar last = (Calendar) first.clone();    last.add(Calendar.DAY_OF_YEAR, 6);    // print the result    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");    System.out.println(df.format(first.getTime()) + " -> " +  df.format(last.getTime()));}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5498530.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存