使用2个字符串计算日期而不使用任何日期或日历类

时间:2013-12-13 17:12:19

标签: java date

该方法接收2个字符串,例如mon 2013-12-31 12:30:30 am,从这两个字符串中我必须计算它们之间的时间。时钟之间的时间12:30:30 am很容易,但我无法找到如何在日期之间获得正确的时间。它必须能够用双轴年或“闰年”来计算它。我遇到的问题主要是因为日期可能是这样的:

mon 2012-12-30 12:30:30 am, mon 2013-02-13 12:30:30 am.

代码之间存在一些差异,因为我一直在测试它并试图让它工作

public float calculateTime(String a, String b) {
    float time = 0;
    String indicatorA = a.substring(20, 22); //am and pm
    String indicatorB = b.substring(20, 22);
    int yearA = Integer.parseInt(a.substring(0, 4));
    int monthA = Integer.parseInt(a.substring(5, 7));
    int dayA = Integer.parseInt(a.substring(8, 10));
    int hourA = Integer.parseInt(a.substring(11, 13));
    int minA = Integer.parseInt(a.substring(14, 16));
    int secA = Integer.parseInt(a.substring(17, 19));
    int yearB = Integer.parseInt(b.substring(0, 4));
    int monthB = Integer.parseInt(b.substring(5, 7));
    int dayB = Integer.parseInt(b.substring(8, 10));
    int hourB = Integer.parseInt(b.substring(11, 13));
    int minB = Integer.parseInt(b.substring(14, 16));
    int secB = Integer.parseInt(b.substring(17, 19));
    //get hours based on indicator am/pm
    int hourAI = hourIndicator(hourA, indicatorA);
    int hourBI = hourIndicator(hourB, indicatorB);
    //calculation
    if (yearA < (yearB-1)) {
        int days = 0;
        do {
            days += getDaysYear(yearA); //returns 366/365 days if it's a leap year
            yearA++;
        } while (yearA < (yearB - 1));
        time += 86400 * days; //turns time into seconds
        System.out.println(time/3600);
    }do{
    if(yearA == (yearB-1)){//if theere's only 1 year of difference
        int days = 0;
        if(monthA == monthB+11){//days when there's a month of diference //
            int daysA = getDaysMonth(monthA, yearA)- dayA;
            int daysBRest = getDaysMonth(monthB, yearB) -dayB;
            int daysB = getDaysMonth(monthB,yearB) -daysBRest;
            days += daysA + daysB;
        }
        if(monthA < monthB+11){//number of days between month A and B //
            do{
                days += getDaysMonth(monthA,yearA);
                monthA++;
            }while(monthA < monthB+11);
        }
        System.out.println(days);
        time += 86400 * days;
    }yearA++;
    }while(yearA <= (yearB-1));
    System.out.println(time/3600);     


    if(yearA == yearB){
    if (monthA < (monthB-1)) {
        int days = 0;
        do {
            days += getDaysMonth(monthA, yearA);
            monthA++;
        } while (monthA < (monthB - 1));
        time += 86400 * days;
    }}

    System.out.println(time/3600+"after month");


    if (dayA < dayB) {
        int days = 0;
        do {
            days += 1;
            dayA++;
        } while (dayA < (dayB - 1));
        time += days * 86400;
    }System.out.println(time/3600+"after day");

    if (hourAIndicator < hourBIndicator) {
        int hourD = hourBIndicator - hourAIndicator;
        time += hourD * 3600;
    }
    if (minA < minB) {
        int minD = minB - minA;
        time += minD * 60;
    }
    if (secA < secB) {
        int secD = secB - secA;
        time += secD;
    }
    System.out.println(time / 3600);
    return time;
}


private int hourIndicator(int hour, String indicator) {
    if ("am".equalsIgnoreCase(indicator)) {
        return hour;
    } else {
        return hour + 12;
    }
}

0 个答案:

没有答案