Java How to get the time when timer is stopped? - java

I'm currently referring to this link to create the timer for my program.
But I don't know how to get the time when timer is stopped.
I think System.getcurrentTimeMillis() cannot be used at this moment.
So, how can I get the current time when timer is stopped with the method mentioned?
EDIT :
I'm currently building a simple 4X4 memory game, and I have timer in my program, using the method from this link.
At the end of the game, I'll use a showMessageDialog to display the number of clicks and time used. However, I'm just able to display the clicks, which is the easiest part.
Based on the method from the link, I think I can use an easy way such as
JOptionPane.showMessageDialog( null, "Congratulations. \n You have win the game with " + click + " click(s) in " + hour + "hour(s) " + minute + " minute(s) " + second + " second(s).", "Win", 1);
Is there any better way of doing it?

You can use
Date d = new Date();
DateFormat df = new SimpleDateFormat("hh:mm:ss");
String dateStr = df.format(d);

Related

How do I set a default time to my timepicker?

I have I time picker in my code and when I press the edit text it shows up and everything looks good but I want to set a default time for my time picker so when the user opens the time picker it will be shown a specific time like "3:34 am" as a default time for the user, how can I do that?. Any help will be appreciated. { you can use java and kotlin}
My time picker
private fun showTimePicker() {
picker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(0)
.setTitleText("حدد الموعد الذي تريدة")
.build()
picker.show(supportFragmentManager, "AdhanNotifacations")
picker.addOnPositiveButtonClickListener {
if (picker.hour > 12) {
fajrEditTxt.setHint(String.format("%02d", picker.hour - 12) + ":"
+ String.format("%02d", picker.minute) + " PM")
} else {
fajrEditTxt.setHint(String.format("%02d", picker.hour) + ":"
+ String.format("%02d", picker.minute) + " AM")
}
calender = Calendar.getInstance()
calender[Calendar.HOUR_OF_DAY] = picker.hour
calender[Calendar.MINUTE] = picker.minute
calender[Calendar.SECOND] = 0
calender[Calendar.MILLISECOND] = 0
}
}
You were almost there, just change the value of setHour and setMinute to the hour and minute you want respectively.
I made few changes to your code to show default time of 3:34am:
picker = MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(3) //3 hour
.setMinute(34) //34 minutes, so 3:34am
.setTitleText("حدد الموعد الذي تريدة")
.build()
If you want to show hour in PM, just add 12 to the hour. For example:
setHour(15)// will show 3pm
Let me know if you have any questions. Thanks.
EDIT
If you want to show time in strict HH:MM format you can keep your code and go as-is. But if you do not wish to be strict, then use the following code:
For example, this will show 9:3PM instead of 09:03PM.
//removed unnecesarry String.format
if(picker.hour > 12){
fajrEditTxt.setHint((picker.hour - 12).toString() + ":" + (picker.minute).toString() + " PM")
}else{
fajrEditTxt.setHint((picker.hour).toString() + ":" + (picker.minute).toString() + " AM")
}
OP asked how to access time:
val time = if(picker.hour > 12){
String.format("%02d",picker.hour - 12) + ":" + String.format("%02d", picker.minute) + " PM"
}else{
String.format("%02d",picker.hour) + ":" + String.format("%02d", picker.minute) + " AM"
}
time //use time, its in HH:MM format
fajrEditTxt.setHint(time)
I'm not understanding what it is you're asking, but if you're trying to sort the times in the library, you could place the times in a list and then sort the list, or if you just want to compare the magnitude values of two different times with just a couple of lines of code, you could remove the time separators from the time strings, convert those values to numbers ( of your choice ) and compare. The numbers generated from the conversions have no real world values, but their magnitudes with respect to one another will always be valid.
Oh. Snippet editor does not support touch devices.
Ok then.
String time = "00:00";
time.replace( ":", "" );
Double d = Double.parseDouble( time );
So now you can do with d as you wish. As you can see you don't have to bother with whether any of the values are to great. If you're dealing with a pair of valid times it's their relative magnitudes that matters. Of course if the time is in 12 Hr format, you'll need to strip the time modifier ( A, a, P, p, Am, AM .... ) too.

Using sensor in android to detect a jump

First of all I am only getting started with android and java so please be indulgent.
So, i want to make a "jump" detector using the sensor in Android devices.
Basically i want to use the information returned by my sensor and compare it to detect if the user jumped or not.
Physics is pretty simple, I only need two very close value (a jump last for about a second) of the Y-axis and compare the average of these two values to a "limit value".
My main problem is I don't know how to get two differents values from my sensor.
I tried to use the System.currentTimeMillis() to catch a value at a very specific moment and compare it with a older or newer value, but it doesn't seem to work very well.
Here is my code.
public void onSensorChanged(SensorEvent event) {
acceleration.setText("X: " +event.values[0]+ " Y:" +event.values[1] + "Z: " +event.values[2] +"Nombre de sauts" +saut);
Yacc = event.values[1];
long curTime = System.currentTimeMillis();
long lastUpdate = curTime - 400;
if((curTime - lastUpdate)>390){
lastUpdate = curTime;
float moyY = (Yacc - last_Yacc)/2;
if(moyY > 5){
saut = saut +1;
}
compteur.setText("currenttime " +curTime + " lastUpdate "+ lastUpdate +" saut=" +saut + " Valeur moyenne" +moyY + " saut=" +saut);
}
last_Yacc = Yacc;
}
I thank you all in advance for you consideration.

Hadoop 2.6.0 JobCounters always returning zero

I am trying to get the total time spent by the map phase and by the reduce phase in a job run. So I've tried to use the JobCounters MILLIS_MAPS and MILLI_REDUCES, but its seems that when I try to get the value these one is always zero. Besides this, I also notice that these counters aren't printed when the job is completed successfully, which leads to my questions.
The JobCounters are in use in Hadoop 2.6.0? Were they deprecated?
If not, how can we use them correctly?
This is what I've tried so far:
Date startTime = new Date();
System.out.println("Job started: " + startTime);
job.waitForCompletion(true);
Date end_time = new Date();
System.out.println("Job ended: " + end_time);
System.out.println("The job took " +
(end_time.getTime() - startTime.getTime()) /1000 + " seconds.");
Counters counters = job.getCounters();
Counter mapTime = counters.findCounter(JobCounter.MILLIS_MAPS);
System.out.println(mapTime.getDisplayName() + " : " + mapTime.getValue());
Counter reduceTime = counters.findCounter(JobCounter.MILLIS_REDUCES);
System.out.println(reduceTime.getDisplayName() + " : " +reduceTime.getValue());
UPDATE:
It seems that I can only print these counters when running yarn. Any ideas how to get them without running yarn?
Thanks for the attention!

understanding roll method in calendar class

If today's date is 19 July 2014
then my code works as follows :
Calendar c = Calendar.getInstance();
My code and output is:
c.roll(3, true);
System.out.print(c.get(Calendar.DATE) + " " + c.get(Calendar.MONTH) + " " + c.get(Calendar.YEAR));
o/p : 26 6 2014
c.roll(2, true);
System.out.print(c.get(Calendar.DATE) + " " + c.get(Calendar.MONTH) + " " + c.get(Calendar.YEAR)) o/p : 19 7 2014
c.roll(1, true);
System.out.print(c.get(Calendar.DATE) + " " + c.get(Calendar.MONTH) + " " + c.get(Calendar.YEAR)) o/p : 19 6 2015
roll now will add 1st parameter to my date and give the answer. Am i write? once it is adding to year once to the month... I am not getting the exact working of this method. Please feel free to correct me.
See javadoc of roll()-method. You should really use the predefined Calendar-constants for readability and avoiding errors. So your integer constants has following meaning:
c.roll(1, true); // is effectively the same as c.roll(YEAR, true);
c.roll(2, true); // is effectively the same as c.roll(MONTH, true);
c.roll(3, true); // is effectively the same as c.roll(WEEK_OF_YEAR, true);
Important: Be aware of the fact that rolling is different from adding a temporal amount. If you roll up the month December then you will just change to January in CURRENT year, not in next year! So I bet you are rather interested in the method add(int, int) using 1 as second argument.
Use the field constants within Calendar rather than passing integer constants. Then the method calls make a bit more sense:
c.roll(Calendar.MONTH, true);
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#YEAR
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#MONTH
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#DAY_OF_MONTH
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#HOUR_OF_DAY

phonegap displaying text in native textbox

I'm making an android app which uses bluetooth using phonegap and I'm having issues handling code because it get's called every 100ms.
I want to try if the issue occurs because my phonegap javascript code is to long causing delays.
To try this I want to dispaly the received message using a native textbox.
Is this possible? and How would I do this?
Thanks
Martijn
PS: I've tried using Toast but since I want to change the displayed value every 100ms this doesn't work.
Code
upon receiving a message this is the order the code is processed
bytes = mmInStream.read(buffer);
s_message = new String(buffer, 0, bytes);
String[] strArray = s_message.split(",");
mHandleReceivedThread = new HandleReceivedThread(strArray);
mHandleReceivedThread.start();
In the HandleReceivedThread:
if(strArrayReceived[0].startsWith("Pressure"))
{
if(strArrayReceived[1].contains("Pressure"))
{
double Pressure = Double.parseDouble(strArrayReceived[1].replace("Pressure", ""));
double PressureResult = Pressure/10;
String PressureValue = String.valueOf(PressureResult);
webView.sendJavascript("PressureValue = " + Pressure + ";");
webView.sendJavascript("document.getElementById('Pressure').innerHTML = " + PressureValue + ";");
Log.e(TAG, "Message fail: " + strArrayReceived[1] );
}
else
{
double Pressure = Double.parseDouble(strArrayReceived[1]
double PressureResult = Pressure/10;
String PressureValue = String.valueOf(PressureResult);
webView.sendJavascript("PressureValue = " + Pressure + ";");
webView.sendJavascript("document.getElementById('Pressure').innerHTML = " + PressureValue + ";");
}
}
this is basically what happens.
And this happens every 100ms , and for some reason the displaying of the Value has lag and sometimes the received message (which should be "Pressure,1500" , or a different value) is Pressure,1500Pressure , causing errors (which I've managed to fix). but this still isn't optimal.
The message is send correctly and if I use a simple chat app they are received correctly.
if there is anything else I should supply , just ask ;-)

Categories