I am trying to take have a time that counts down all the way to 0 from 1 minute.
I have already created my timer. I just need to know how do i convert a int such as 100 to a String to show 1:00 as in a minute and keep counting down like minus 1 secodn from the int and convert it to the String :59?
Any suggestions?
If I understand your question correctly you simply want to divide your number by 60 to get the minutes and then mod your number by 60 to get the seconds and then concat the strings.
int remainingTime = 100;// or whatever number of seconds you have left
String min = (remainingTime / 60) + "";
String sec = (remainingTime % 60) + "";
String remainingTimeStamp = "min" + ":" + "sec";
If you want to get fancy with it, check to see if min and sec are less than 10 and append a leading zero, so that it looks like 01:05 rather than 1:5
I'm not sure what you mean by "I already have my timer". I'll wait for you to update your question, rather than speculate as to what you might mean, before I continue elaborating.
To add to this, check the http://developer.android.com/reference/android/text/format/DateUtils.html class. Note that this is not in 'org.apache' which has another DateUtils class.
Edit:
Since it appears you were looking for formatting 100 seconds here is it formatted:
SimpleDateFormat df = new SimpleDateFormat("mm:ss");
String formatted = df.format(remainingTime * 1000);
Related
I have my server returning a time ago string format like this 2 minutes ago which is fine but I now need to convert it to seconds. I thought of just splitting the string using a space delimiter and getting the first text but then, there are cases of my server returning instances of 5 hour 43 minutes ago etc. My question is this, Is there a neat way of conversion from this time ago format bearing in mind of these instances to just seconds?
I dont think there is a particular library or method that can do the conversion from your "time ago" to seconds. The best option for you would be to parse the "Time ago" string returns by your server and do the conversion yourself. The simplest way would be to split your string on space boundaries then get the values of hours and min.
public static int timeAgoToSeconds(String timeAgo) {
int timeInSec = 0;
String []myStringArray = timeAgo.trim().split(" ");
if(myStringArray.length==3){
timeInSec = Integer.valueOf(myStringArray[0]) *60;
}
if(myStringArray.length==5){
timeInSec = Integer.valueOf(myStringArray[0]) *3600 + Integer.valueOf(myStringArray[2]) * 60;
}
return timeInSec;
}
test in the main() method:
String timeAgoHHmm = "5 hour 43 minutes ago";
String timeAgoMM = "2 minutes ago";
System.out.println(timeAgoToSeconds(timeAgoHHmm));//20580
System.out.println(timeAgoToSeconds(timeAgoMM));//120
Complete timeDifference that takes two different times and returns a
string with the difference in hours and minutes, separated by ":".
The int argument 0 represents midnight, 700 is 7:00 a.m., 1314 is 14
minutes past 1:00pm, and 2200 is 10 pm.
Leading zeros required
I know the problem requires you to convert both times to minutes, however I don't know how to separate the integers that are four characters long so I can differ between hours and minutes.
Sice your question is on the separation part only, this will do the trick:
int timeDifference(int a, int b)
{
int minsA = a % 100); //remainder
int hrsA = (a / 100);
.....
}
Edit: if you want to get the full time just in minutes you can do:
int fullMinsA = minsA + hrsA*60;
I have a string that contain certain hour ex. 14:34, and now I want to calculate the difference between the current hour ex. 21:36-14:34=7 hours 2 minutes (or something like that.) Can someone explain me how can I do that?
It's very easy: You need to separate the string in terms you can add or substract:
String timeString1="12:34";
String timeString2="06:31";
String[] fractions1=timeString1.split(":");
String[] fractions2=timeString2.split(":");
Integer hours1=Integer.parseInt(fractions1[0]);
Integer hours2=Integer.parseInt(fractions2[0]);
Integer minutes1=Integer.parseInt(fractions1[1]);
Integer minutes2=Integer.parseInt(fractions2[1]);
int hourDiff=hours1-hours2;
int minutesDiff=minutes1-minutes2;
if (minutesDiff < 0) {
minutesDiff = 60 + minutesDiff;
hourDiff--;
}
if (hourDiff < 0) {
hourDiff = 24 + hourDiff ;
}
System.out.println("There are " + hourDiff + " and " + minutesDiff + " of difference");
UPDATE:
I'm rereading my answer and I'm surprised is not downvoted. My fault. I wrote it without any IDE check. So, the answer should be minutes1 and 2 for the minutesDiff and obviously and a check to carry the hour difference if the rest of minutes is negative, making minutes (60+minutesDiff). If minutes is negative, rest another hour to the hourDiff. If hours become negative too, make it (24+hourDiff). Now is fixed.
For the sake of fastness, I'm using a custom function. For the sake of scalability, read Nikola Despotoski answer and complete it with this:
System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
System.out.println(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
I would start by using the .split method to get the string into its two components (minutes and hours) then I would convert both times into minutes by mutliplying the hours by 60 and then adding the minutes
String s = "14:34";
String[] sArr = s.split(",");
int time = Integer.parseInt(sArr[0]);
time *= 60;
int time2 = Integer.parseInt(sArr[1]);
time = time + time2;
do this for both strings and then subtract one from the other. You can convert back to normal time by using something like this
int hours = 60/time;
int minutes = 60%time;
The answer labeled as correct will not work. It does not account for if the first time is for example 3:17 and the second is 2:25. You end up with 1 hour and -8 minutes!
i trying to convert time in string format to minutes. i can use substring and find the minutes but there is a problem because there is a chance of getting time in the format 720:00 hours. (as i am calculating time for many days) pls help with me....
Thanks in advance........
Hussain
As far as I understood, you need to calculate how many minutes are in the given hours and minutes. Well, I'd do it fast and simple:
String time = "725:00";
String[] parts = time.split(":",2);
int hours = Integer.valueOf(parts[0]);
int minutes = Integer.valueOf(parts[1]);
return hours*60+minutes;
With Groovy, you could do something like this:
def time = '720:00'
def minutes = time.split(':').with {
(h,m) = it as int[]
h * 60 + m
}
In Java use String.split(":") and take the second element of the array.
Assuming that the time is in "hour:minute" format.
Currently I have a function which can take the start time and end time of one day, and calculate the difference between the two, giving me the hours worked in a day. What I would like to do is be able to get the hours worked for 7 days, and return a grand total, while remaining with the display format (HH:mm).
My function for a single day's total:
Period p = new Period(this.startTime[dayIndex], this.endTime[dayIndex]);
long hours = p.getHours();
long minutes = p.getMinutes();
String format = String.format("%%0%dd", 2);//Ensures that the minutes will always display as two digits.
return Long.toString(hours)+":"+String.format(format, minutes);
this.startTime[] & this.endTime[] are both arrays of DateTime objects.
Any suggestions?
You'll need something to hold a week's worth of days, and call your function once for each day.
But that means you'll want to refactor so that your calculator method doesn't format as a string, but instead returns a numeric value, so you can easily add them together.
Another simple solution:
Here is a method that receives separate the hours and minutes.The parameters are:
Start Hour
Start Minutes
End Hour
End Minutes
first, calculate the difference between hours and minutes separate:
int hours = pEndHour - pStartHour;
int minutes = ((60 - pStartMinutes) + pEndMinutes) - 60;
then, validates if the value of "minutes" variable is negative:
// If so, the "negative" value of minutes is our remnant to the next hour
if (minutes < 0) {
hours--;
minutes = 60 + minutes ;
}
Finally you can print the period of time in the hour format:
String format = String.format("%%0%dd", 2);
System.out.println( "*** " + hours + " : " + minutes);
That's all.
Solution I ended with for those interested
Period[] p=new Period[7];
long hours = 0;
long minutes =0;
for(int x=0; x<=this.daysEntered;x++)
{
p[x] = new Period(this.startTime[x], this.endTime[x]);
hours += p[x].getHours();
minutes += p[x].getMinutes();
}
hours += minutes/60;
minutes=minutes%60;
String format = String.format("%%0%dd", 2);
return Long.toString(hours)+":"+String.format(format, minutes);