I have an arraylist arr which contains a series of number, like 07, 52, 25, 10, 19, 55, 15, 18, 41. In this list first item is hour, second is minute and third is second, like 07:52:25. Now I want to create a time array in which i can insert these values and do some arithmetic operation like difference between first index and second index, which gives me the time difference. So how can i do that?
ArrayList arr = new ArrayList();
StringTokenizer st = new StringTokenizer(line, ":Mode set - Out of Service In Service");
while(st.hasMoreTokens()){
arr.add(st.nextToken());
}
Ok i think i understand what you want your code to do. Heres how i would do it.
public class DateHandler
{
public DateHandler(int seconds, int minutes, int hours)
{
this.seconds = seconds;
this.minutes = minutes;
this.hours = hours;
}
public String toString()
{
return "Seconds: "+seconds+" Minutes: "+minutes+" Hours: "+hours;
}
public int seconds;
public int minutes;
public int hours;
}
public class Main
{
public static void main(String args[])
{
int[] data = {07, 52, 25, 10, 19, 55, 15, 18, 41}
int numberOfDates = data.length/3//Divide by 3 because there are 3 numbers per date
ArrayList<DateHandler> dates = new ArrayList<DateHandler>(numberOfDates);
for(int x=0;x<numberOfDates;x++)
{
int index = x*3;
DateHandler date = new DateHandler(data[index],data[index+1],data[index+2]);
System.out.println("added date: "+date.toString());
dates.add(date);
}
//here you can do your calculations.
}
}
I hope this helped!
I would use split to tokenise. You can read any amount of times from multi-line data
BufferedReader br =
String line;
List<Integer> times = new ArrayList<Integer>();
while((line = br.readLine()) != null) {
String[] timesArr = line.split(", ?");
for(int i=0;i<timesArr.length-2;i+=3)
times.add(Integer.parseInt(times[i]) * 3600 +
Integer.parseInt(times[i+1]) * 60 +
Integer.parseInt(times[i+2]));
}
br.close();
System.out.println(times); // prints three times in seconds.
// difference between times
for(int i=0;i<times.size()-1;i++)
System.out.println("Between "+i+" and "+(i+1)+
" was "+(times.get(i+1)-times.get(i))+" seconds.");
If this array (you said "arraylist arr" right?)
07, 52, 25, 10, 19, 55, 15, 18, 41
means
7:52:25, 10:19:55 and 15:18:41
Then you can use SimpleDateformat to "convert" a string to a date.
SimpleDateFormat can parse ("convert") a String
to a Date
, and to format a Date
to a String
(actually a StringBuffer
).
In your case, you loop through your ArrayList
group the hours, minutes and seconds, and use SimpleDateFormat
to parse them:
int index = 0;
String tempTime = "";
ArrayList<Date> dateList = new ArrayList<Date>();
//assuming hour in format 0-23, if 1-24 use k in place of h
SimpleDateFormat dateFormat = new SimpleDateFormat("h-m-s-");
for(String timeeElement : timeArrayList)
{
tempTime += timeeElement;
tempTime += "-"; //To handle situations when there is only one digit.
index++;
if(index % 3 == 0)
{
Date d = dateFormat.parse(tempTime, new ParsePosition(0));
dateList.add(d);
tempTime = "";
}
}
You have a list of dates to play with now
精彩评论