Reputation: 167
I have four dates and I wanted to get the total...
EXAMPLE
timeInAM=9:00
timeOutAM=12:00
timeInPM=13:00
timeOutPM=18:00
I wanted to make total=(timeOutAM-timeInAM)+(timeOutPM-timeInPM)
that would result in total=8:00
but it gives me '16:00:00'
Here's what I did: DATE
SimpleDateFormat tf24=new SimpleDateFormat("HH:mm");
Date timeInAM=new Date();
Date timeOutAM=new Date();
Date timeInPM=new Date();
Date timeOutPM=new Date();
long total;
timeInAM=tf24.parse(tblWorkPeriod.getValueAt(i, 1).toString());
timeOutAM=tf24.parse(tblWorkPeriod.getValueAt(i, 2).toString());
timeInPM=tf24.parse(tblWorkPeriod.getValueAt(i, 3).toString());
timeOutPM=tf24.parse(tblWorkPeriod.getValueAt(i, 4).toString());
total=(timeOutAM.getTime()-timeInAM.getTime())+(timeOutPM.getTime()-timeInPM.getTime());
System.out.println(tf24.format(new Date(total)));
CALENDAR
Calendar timeInAM=Calendar.getInstance();
Calendar timeOutAM=Calendar.getInstance();
Calendar timeInPM=Calendar.getInstance();
Calendar timeOutPM=Calendar.getInstance();
Calendar total=Calendar.getInstance();
SimpleDateFormat tf24=new SimpleDateFormat("HH:mm");
timeInAM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 1).toString()));
timeOutAM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 2).toString()));
timeInPM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 3).toString()));
timeOutPM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 4).toString()));
long sum=(timeOutAM.getTimeInMillis()-timeInAM.getTimeInMillis())+(timeOutPM.getTimeInMillis()-timeInPM.getTimeInMillis());
total.setTimeInMillis(sum);
System.out.println("total : "+tf24.format(total.getTime()));
Upvotes: 0
Views: 87
Reputation: 325
I'm not sure if I understand your question, but here:
You can try to use:
double timeInAMint = Double.parseDouble(timeInAM);
double timeOutAMint = Double.parseDouble(timeoutAM);
double timeInPMint = Double.parseDouble(timeInPM);
double timeOutPMint = Double.parseDouble(timeOutPM);
double sum = timeInAMint + timeOutAMint + timeInPMint + timeOutPMint;
System.out.println("Sum: " + sum);
Upvotes: 0
Reputation: 496
You can give a try to JodaTime library (if you can use other libraries). With the following you can achieve what you need by calling LocalTime::minusHours
and similar commands:
LocalTime timeInAM=new LocalTime(hourOfDay, minuteOfHour);
LocalTime timeOutAM=new LocalTime(hourOfDay, minuteOfHour);
LocalTime timeInPM=new LocalTime(hourOfDay, minuteOfHour);
LocalTime timeOutPM=new LocalTime(hourOfDay, minuteOfHour);
LocalTime amInterval = timeOutAM.minusHours(timeInAM.getHourOfDay()).minusMinutes(timeInAM.getMinuteOfHour());
LocalTime pmInterval = timeOutPM.minusHours(timeInPM.getHourOfDay()).minusMinutes(timeInPM.getMinuteOfHour());
LocalTime total = pmInterval.plusHours(amInterval.getHourOfDay()).plusMinutes(amInterval.getMinuteOfHour());
Use a proper DateTimeFormatter to parse/print the dates in LocalTime.
Upvotes: 2
Reputation: 2608
Try using a Calendar instance.
Edit : Sample implementation.
Calendar timeInBeforeBreak = Calendar.getInstance();
timeInBeforeBreak.clear();
Calendar timeOutBeforeBreak = Calendar.getInstance();
timeOutBeforeBreak.clear();
timeInBeforeBreak.add(Calendar.HOUR_OF_DAY, 9);
timeInBeforeBreak.add(Calendar.MINUTE, 30);
timeOutBeforeBreak.add(Calendar.HOUR_OF_DAY, 11);
timeOutBeforeBreak.add(Calendar.MINUTE, 30);
long timeMillis = timeOutBeforeBreak.getTimeInMillis() - timeInBeforeBreak.getTimeInMillis();
System.out.println("Hours :"+timeMillis/1000/60/60);
Upvotes: 0