java 一年 周数_Java获取一年有多少周、某周的第一天和最后一天,当前时间所在当年的实际周数...

import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;

public class DateTest {

// 获取当前时间所在年的周数 public static int getWeekOfYear(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setMinimalDaysInFirstWeek(7); c.setTime(date);

return c.get(Calendar.WEEK_OF_YEAR); }

// 获取当前时间所在年的最大周数 public static int getMaxWeekNumOfYear(int year) { Calendar c = new GregorianCalendar(); c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

return getWeekOfYear(c.getTime()); }

// 获取某年的第几周的开始日期 public static Date getFirstDayOfWeek(int year, int week) { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1);

Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7);

return getFirstDayOfWeek(cal.getTime()); }

// 获取某年的第几周的结束日期 public static Date getLastDayOfWeek(int year, int week) { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1);

Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7);

return getLastDayOfWeek(cal.getTime()); }

// 获取当前时间所在周的开始日期 public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday return c.getTime(); }

// 获取当前时间所在周的结束日期 public static Date getLastDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday return c.getTime(); }

public static void main(String[] args) { int year = 2011; int week = 1;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date today = new Date(); Calendar c = new GregorianCalendar(); c.setTime(today);

System.out.println("current date = " + sdf.format(today)); System.out.println("getWeekOfYear = " + getWeekOfYear(today)); System.out .println("getMaxWeekNumOfYear = " + getMaxWeekNumOfYear(year)); System.out.println("getFirstDayOfWeek = " + sdf.format(getFirstDayOfWeek(year, week))); System.out.println("getLastDayOfWeek = " + sdf.format(getLastDayOfWeek(year, week))); System.out.println("getFirstDayOfWeek = " + sdf.format(getFirstDayOfWeek(today))); System.out.println("getLastDayOfWeek = " + sdf.format(getLastDayOfWeek(today))); System.out.println("---------------------------------------------------");

/*下边是解决年初第一周的前几天有可能也属于上一年的最后的一周的后几天,导致当前时间获取得到的所在周数和实际不符的问题*/

//获取当前年已经过的周数c.setFirstDayOfWeek(Calendar.MONDAY);c.setMinimalDaysInFirstWeek(7);c.setTime(today);

//获取当前年的当前月份int month = c.get(Calendar.MONTH) + 1;int year2 = c.get(Calendar.YEAR);int weekofYear1 = c.get(Calendar.WEEK_OF_YEAR);System.out.println(weekofYear1);String yearStr = String.valueOf(year2);int yearint = Integer.parseInt(yearStr.substring(yearStr.length() - 1, yearStr.length()));System.out.println(yearint);if(weekofYear1 > 51 && month == 1){if(weekofYear1 > 51){weekofYear1 = 1;}}//当前周数 * 7 是否小于 当日处于一年中的第几天if(weekofYear1 * 7 < c.get(Calendar.DAY_OF_YEAR)){weekofYear1++;}System.out.println(weekofYear1);System.out.println("一年中第多少天"+c.get(Calendar.DAY_OF_YEAR)); }}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
评论

打赏作者

车万白野兔

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值