How to add n days to a Date in java without import

2020-03-07 08:07发布

How do I add n days to a date in Java Creating my own java class?

For example, my date is (dd/mm/yyyy) = 26/02/2014

Adding 3 days, the output should be 01/03/2014.

Without importing Calendar or Date from JAVA API

Thank you in Advance. Any Sample code or Pseudo Code or idea will be highly apprecited

标签: java class date
8条回答
冷血范
2楼-- · 2020-03-07 09:08
class DateCalculator{
    private int[] normalYear = {31,28,31,30,31,30,31,31,30,31,30,31};
    private int[] leapYear = {31,29,31,30,31,30,31,31,30,31,30,31};

    public String calculateDate(String date, int numberOfDays){
        String[] date_parts = date.split("/");
        int year = Integer.parseInt(date_parts[2]);
        int month = Integer.parseInt(date_parts[1]);
        int day = Integer.parseInt(date_parts[0]);
        int days = numberOfDays;
        int[] refYear = getRefYear(year);
        while(true){
            int diff = days - (refYear[month - 1] - day);
            if(diff > 0 ){
                days = diff;
                month++;
                day = 0;
                if(month <= 12){
                    continue;
                }
            }else{
                day = day + days;
                break;
            }
            year++;
            month = 1;
            refYear = getRefYear(year);
        }

        StringBuilder finalDate = new StringBuilder();
        finalDate.append(day);
        finalDate.append("/");
        finalDate.append(month);
        finalDate.append("/");
        finalDate.append(year);

        return finalDate.toString();

    }

    private int[] getRefYear(int year){

        return isLeapYear(year)? leapYear : normalYear;
    }

    private boolean isLeapYear(int year){
        if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)  ){
            return true;
        }
        return false;
    }
}
查看更多
时光不老,我们不散
3楼-- · 2020-03-07 09:10

Question: increase or update given date by certain days, without using calendar or date class?

Answer: I verified first ex and that seems like good a reference for storing values in maps (as days for months are fixed).

My solution is below and it is working correctly, when I tested.

Solution code:

import java.util.*;
public class IncreaseDate {

    private static final Map<Integer, Integer> date;

    static {
        date= new HashMap<Integer,Integer>();
        date.put(1,31);
        date.put(2,28);
        date.put(3,31);
        date.put(4,30);
        date.put(5,31);
        date.put(6,30);
        date.put(7,31);
        date.put(8,31);
        date.put(9,30);
        date.put(10,31);
        date.put(11,30);
        date.put(12,31);
    }

    public static void main(String args[])  {
        IncreaseDate id= new IncreaseDate();
        System.out.println("2/5/2018 "+"+"+60+" : ");
        id.updateDate(2,5,2018,60);
    }

    public void updateDate(int m,int d, int y, int days) {
        int temp=date.get(m);
        // for finding leap year
        if(m==2 && (y%4==0 && y%100!=0 && y%400!=0)) {
            temp= date.get(m)+1;
        }
        if(d+days>temp) {
            if(m<=11) {
                updateDate(m+1,d,y,days-temp);
            }
            else {
                updateDate(1,d,y+1,days-temp);
            }
        }
        else {
            System.out.println("Upadate Date: "+m+"/"+(d+days)+"/"+y);
        }
    }
}

Points to remember:

  • We are updating months and years and decreasing days until it becomes less than that month days.
  • Calendar class is abstract class, so we cant create object directly. But we can create using getInstance() factory method.
查看更多
登录 后发表回答