I'm getting a data which contains a certain number.
I need to find how many hours, minutes and seconds it stands for.
for example:
I'm getting the number 248 which means:
00 hours : 04 minutes : 08 seconds
Any ideas would be really apprieciated!
Determine hours, minutes and seconds like this:
int hours = (int) (time / 3600);
int minutes = ((int) (time / 60)) % 60;
int seconds = time % 60;
Alternatively, you can determine minutes like this once you know the hours:
int minutes = (int) ((time - hours * 3600) / 60));
You need to multiple it to milisec.
Like 248*1000;
Then Date d = new Date(248*1000);
And youll have data object.
d.getHours(). for example
also you can use SimpleDateFormat. Which can format data with some pattern and output it to string
Like yyyy-MM-hh HH:mm
you have to define first, how this number is build up, e.g. why it can not stand for
00 hours, 02 minutes and 48 sseconds
Related
I am trying to build a meditation app with Android Studio.
I need that app plays a sound (bell) at random time but with minimum interval time (for example 1 minute).
Thanks
You can try using this:
Random r = new Random();
int minIntervalMs = 60000; //1 minute
int maxintervalMs = 5 * 60000; //5 minutes
int interval = r.nextInt(maxintervalMs - minIntervalMs) + minIntervalMs;
This way You get a millisecond interval between 1 an 5 minutes.
I'm making a project on android studio.
I must convert a number into an hour, minutes and seconds date.
The number is: 0,18865386
The result must be: 04:31:40
I've obtained this date whit excel (used for test calc) just changing the cell format to HOURS but i don't understand how to calculate it.
Any solutions?
1 corresponds to 24 hours.
In hours: 0,18865386 means 24 x 0,18865386 ~= 4,5 hours which corresponds to your 04:31:40.
If you need more precision, e.g. in seconds:
The number of seconds in a day = 24 x 60 x 60 = 86400.
Then 0,18865386 means 86400 x 0,18865386 = 16300 seconds = 04:31:40.
time Range picker to select time range, with hours mins and seconds android
Is there any library which supports seconds also
the only one i found was
https://github.com/tittojose/TimeRangePicker
but it does not support seconds
use this,
int time=(picker.getCurrentMinute() * 60 + picker.getCurrentHour() *
60 * 60) * 1000;
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
String formatted = format.format(time);
I'm new to Android development.I want to create a stopwatch with precision of 0.01 seconds.Here is part of my code(which I think the problem lies within):
private void runTimer()
{
final TextView timeView = (TextView) findViewById(R.id.time_view);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
int seconds = centiseconds / 100;
int centisecs = centiseconds % 100;
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
String time = String.format("%d:%02d:%02d.%02d", hours, minutes, secs, centisecs);
timeView.setText(time);
if (isRunning) {
centiseconds += 1;
}
handler.postDelayed(this, 10);
}
});
}
}
Since postDelayed method's delay is in milliseconds, ten times of it would be 1 centiseconds. So I'm incrementing my centiseconds variable every 10ms.So far so good.
But when I test my app on my device, it seems the seconds are ticking slower than they should. Is is probable that the codes corresponding to division and modulo operations cause so much lag that hinder the increment and reduce the accuracy?
I've rewritten the app for 0.1 seconds(deciseconds) (by: centiseconds / 10 and % 10 and postDelayed(..., 100) ) and it seems it is ticking correctly.
P.S.
Is this the reason my 4.3 Jellybean's Stopwatch has 0.1 seconds accuracy?
What is the limit of precision in android for such app? ( Timely's has 0.01 seconds so I think it is at least 0.01 seconds)
This is wrong approach as you cannot rely on system message queue as source of precise ticks as it does NOT guarantee any precision in delivery. postDelayed() queues your runnable to be delivered no sooner than now + delay but for precise delivery is not quaranteed and additional delays can happen for many reasons which in longer run would give you noticeable cumulative error in measurements.
You can however use postDelayed() to update your UI, but to know how much time passed you should use system clock methods, not own counters.
You also should fire your runnable at least twice per precision, i.e. if you want to update timer display once per minute, you should "tick" twice per minute so if there'd be any delay in message queue handling your UI shall still get at least one tick per second on time.
I know this is easy, but how can i make my widget update every 24 hours this what am presently trying,
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:autoAdvanceViewId="#id/stack_view"
android:initialLayout="#layout/cepf_appwidget_layout"
android:minHeight="100dp"
android:minWidth="180dp"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="3600000" >
</appwidget-provider>
24 hours = 24 hours * 60 minutes * 60 seconds * 1000 milliseconds = 86400000 milliseconds. Put this value to the android:updatePeriodMillis
P.S. Your current frequency is 3600000 milliseconds. This is: 3600000 / 1000 = 3600 seconds / 60 = 60 minutes = 1 hour.
Well, it's basic convertion. 1 hour is 60 minutes, 1 minute is 60 seconds, 1 second is 1000 milliseconds.