根据传入的日期和会员天数计算时间截止日期

代码语言:javascript
复制
package controller;

/**

  • Created by Lenovo on 2023/7/22.
    */
    public class ExtentionAlgorithm {

    public static void main(String[] args) {
    System.out.println("Hello World!");
    System.out.println(isLeapYear(2000));
    System.out.println(isLeapYear(2023));
    System.out.println(isLeapYear(5001));
    }

    /**

    • vipStartTime: 2023/07/22 20:05:00

    • vipDays: 21Days

    • vipEndTime: 2023/10/22 20:05:00

    • @param startTime

    • @param vipDays

    • @return
      */
      public static String getVipExpriedDate(String startTime, int vipDays){
      if(startTime==null){
      return null;
      }
      if(startTime.isEmpty()){
      return null;
      }
      if(vipDays<=0){
      return startTime;
      }

      String substringYearDate = startTime.substring(0, 4);
      Integer integerYearDate = Integer.valueOf(substringYearDate);
      int yearDate=integerYearDate;
      int yearCount = vipDays / 365;
      if (yearCount>0){
      while (true){
      if (isLeapYear(yearDate)){
      yearDate+=1;
      vipDays-=366;
      }else {
      yearDate+=1;
      vipDays-=365;
      }

           yearCount--;
           if (yearCount&lt;0){
               break;
           }
       }
      

      }else {
      int intMonthCount = vipDays / 28;
      if (intMonthCount>0){
      String monthDateStr=startTime.substring(5,7);
      Integer integerMonth = Integer.valueOf(monthDateStr);
      while (true){
      switch (integerMonth){
      // case CustMonthName.getKey("JAN"):
      // break;
      default:
      break;
      }
      integerMonth--;
      if (integerMonth<0){
      break;
      }
      }
      }
      }

      String vipEndTime=null;
      return vipEndTime;
      }

    /**

    • 2023

    • 2000

    • 判断年份是平年还是闰年

    • @param yearDate

    • @return
      */
      public static Boolean isLeapYear(int yearDate){
      if (yearDate<0){
      return null;
      }
      String yearDateStr = String.valueOf(yearDate);
      if (yearDateStr==null){
      return null;
      }
      if (yearDateStr.isEmpty()){
      return null;
      }
      if (yearDateStr.length()!=4){
      return null;
      }
      if (yearDate>=5000){
      return null;
      }

      if(yearDate%4==0 && yearDate%100!=0){
      return true;
      }
      if (yearDate%400==0){
      return true;
      }
      return false;
      }
      }

enum CustMonthName{
JAN(1,"JAN"),
FEI(2,"FEI"),
MARCH(3,"MARCH"),
APERIL(4,"APERIL"),
MAY(5,"MAY"),
JUN(6,"JUN"),
JULY(7,"JULY"),
AUGOST(8,"AUGOST"),
SEPTEMBER(9,"SEPTEMBER"),
OCTOBER(10,"OCTOBER"),
NOMEMBER(11,"NOMEMBER"),
DESEMBER(12,"DESEMBER");

private Integer key;
private String desc;

private CustMonthName(Integer key,String desc){
    this.key=key;
    this.desc=desc;
}

public static String getDesc(Integer index){
    for (CustMonthName custMonthName: CustMonthName.values()){
        if(custMonthName.getKey()==index){
            return custMonthName.getDesc();
        }
    }
    return null;
}

public static Integer getKey(String desc){
    for (CustMonthName custMonthName : CustMonthName.values()){
        if (custMonthName.getDesc().equals(desc)){
            return custMonthName.getKey();
        }
    }
    return null;
}

public Integer getKey() {
    return key;
}

public void setKey(Integer key) {
    this.key = key;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

}

程序设计的实现思路在上面,编程代码的过程是需要使用数学知识和计算机的应用编程知识。代码质量的提高,数据返回正确性。魔鬼数字需要尽量避免。