2

我有两次。开始和结束。现在我想以 1 小时的间隔显示它们之间的所有时间。

例如:

我有00:00-23:55

输出:-

12:00 AM - 01:00 AM
01:00 AM - 02:00 AM 等等。

请帮我找到这个。

提前致谢。

4

2 回答 2

8
String date1 = "26/02/2011";
String time1 = "00:00 AM";
String date2 = "26/02/2011";
String time2 = "12:00 PM";

String format = "dd/MM/yyyy hh:mm a";

SimpleDateFormat sdf = new SimpleDateFormat(format);

Date dateObj1 = sdf.parse(date1 + " " + time1);
Date dateObj2 = sdf.parse(date2 + " " + time2);
System.out.println("Date Start: "+dateObj1);
System.out.println("Date End: "+dateObj2);

//Date d = new Date(dateObj1.getTime() + 3600000);


long dif = dateObj1.getTime();
while (dif < dateObj2.getTime()) {
    Date slot = new Date(dif);
    System.out.println("Hour Slot --->" + slot);
    dif += 3600000;
}

输出 :

Date Start: Sat Feb 26 00:00:00 IST 2011
Date End: Sat Feb 26 12:00:00 IST 2011
Hour Slot --->Sat Feb 26 00:00:00 IST 2011
Hour Slot --->Sat Feb 26 01:00:00 IST 2011
Hour Slot --->Sat Feb 26 02:00:00 IST 2011
Hour Slot --->Sat Feb 26 03:00:00 IST 2011
Hour Slot --->Sat Feb 26 04:00:00 IST 2011
Hour Slot --->Sat Feb 26 05:00:00 IST 2011
Hour Slot --->Sat Feb 26 06:00:00 IST 2011
Hour Slot --->Sat Feb 26 07:00:00 IST 2011
Hour Slot --->Sat Feb 26 08:00:00 IST 2011
Hour Slot --->Sat Feb 26 09:00:00 IST 2011
Hour Slot --->Sat Feb 26 10:00:00 IST 2011
Hour Slot --->Sat Feb 26 11:00:00 IST 2011
于 2012-09-11T09:09:04.213 回答
1

命名可能不正确,也可以更好地形成,但现在尝试和错误此代码有效。

关于00:00-23:55,使用'-'解析,第一部分可以是'startTime',第二部分可以是'endTime'

package demo.orientaion.change.run_cmd;

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

public class TimeDiffCalc {

public TimeDiffCalc() {

}

public Calendar addHr(String startTime) {
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    Date d;

    Calendar gc = null ;
    try {
        d = df.parse(startTime);

        gc = new GregorianCalendar();
        gc.setTime(d);
        gc.add(Calendar.HOUR, 1);
    } catch (ParseException e) {
        e.printStackTrace();
    } 
    return gc;
}

public void diffCalc(String time1, String time2) {

    long diff=0;
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    Date date1 = null;
    try {
        date1 = format.parse(time1);
        Date date2 = format.parse(time2);
        diff = date2.getTime() - date1.getTime();
        Date date3 ;
        String string = time1;
        while(true)
        {
            Calendar gc = addHr(string) ;
            date3 = gc.getTime();
            string = getHrMin(gc.get(Calendar.HOUR_OF_DAY))+":"+getHrMin(gc.get(Calendar.MINUTE));
            if(date2.getTime()-date3.getTime()>=0)
            {
                System.out.println("diff Hours :: "+string);
            }
            else
            {

                break;
            }

        }

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Difference in Houres:"+getDate(diff));

}

private String getDate(long diff) {
    long hours=diff/(60*60*1000);
    long minutes=(diff%(60*60*1000))/(60*1000);

    long sec=((diff%(60*60*1000))%(60*1000))/1000;
    String formateddate="";
    if(hours<10)
    {
        formateddate="0"+hours;
    }else{
        formateddate=""+hours;
    }
    if(minutes<10)
    {
        formateddate=formateddate+":0"+minutes;
    }else{
        formateddate=formateddate+":"+minutes;
    }
    if(sec<10)
    {
        formateddate=formateddate+":0"+sec;
    }else{
        formateddate=formateddate+":"+sec;
    }

    return formateddate;
}

private String getHrMin(int time) {
    String formatedTime;
    if(time<10)
    {
        formatedTime="0"+time;
    }else{
        formatedTime=""+time;
    }
    return formatedTime;
}

/**
 * @param args
 */
public static void main(String[] args) {
    TimeDiffCalc calc = new TimeDiffCalc();

    String startTime = "00:00:00";
    String endTime = "23:53:00";
    calc.diffCalc(startTime, endTime);
}

}
于 2012-09-11T08:59:04.427 回答