java 各种日期计算

public class Tests {


    static String[] weeks = { "日", "一", "二", "三", "四", "五", "六"};
    public static void main(String[] args) throws ParseException {
        String dayStr="2024-12-31";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(4);
        calendar.setTime(dateFormat.parse(dayStr));
        int weekOfYear = calendar.get(Calendar.YEAR);
        String monthOfYear = dayStr.substring(5,7);
        int weekNo = calendar.get(Calendar.WEEK_OF_YEAR);
        //Calendar获取月份需要+1
        int nowMonth = calendar.get(Calendar.MONTH) +1;
        //正常12月份肯定都是大月份的周数如53 52 51 50 49 如果周数是1而月份是12,则表示日期被计算在了下一年 年份需要 +1
        if(weekNo==1 && nowMonth==12){
            weekOfYear = weekOfYear + 1;
        }
        //1月份肯定都是小周数如1,2,3,4,5 如果周数是52 或者53 而月份是1 则表示日期被计算在了上一年,年份需要-1
        if((weekNo==53 || weekNo==52)  && nowMonth==1){
            weekOfYear = weekOfYear - 1;
        }
        System.out.println(weekOfYear+"年"+"第"+weekNo+"周");
        System.out.println(weekOfYear+"年"+"第"+monthOfYear+"月");
        System.out.println("FB"+weekOfYear%100+monthOfYear);


//
//        String date ="2023-08-07";
//        //转换为Date类型
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date date1 = sdf.parse(date);
//        //判断日期是星期几
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date1);
//        int weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
//        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
//
//        System.out.println("当前是本月的第 " + weekOfMonth + " 个星期的星期 " + dayOfWeek);

//        String dateString = "2023-09-04";
//        LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);
//
//        int weekOfMonth = date.getDayOfMonth() / 7 + 1; // 计算是当前月的第几个星期
//        int dayOfWeek = date.getDayOfWeek().getValue(); // 获取星期几
//        int monthOfYearInt = date.getMonth().getValue();
//        String monthOfYear;
//        if (monthOfYearInt<10){
//          monthOfYear="0"+String.valueOf(monthOfYearInt);
//        }else {
//             monthOfYear=String.valueOf(monthOfYearInt);
//        }
//        String year = String.valueOf(date.getYear());
//
//        System.out.println("第 " + weekOfMonth + " 个星期的 " + dayOfWeek+",当前是"+monthOfYear+"月"+",当前是"+ year.substring(2)+"年");
//        String FB ="FB"+year.substring(2)+monthOfYear;
//        System.out.println("当前FB是:"+FB);




    }
}

你可能感兴趣的:(java,开发语言)