I am trying to get this string to return Minute:Second:Millisecond for my MediaPlayer. I have found this code, but can't figure out how to make the Milliseconds work and put it at 2 decimal places. I'm sure its simple to the right person!
private String getTimeString(long millis) {
StringBuffer buf = new StringBuffer();
int hours = (int) (millis / (1000*60*60));
int minutes = (int) (( millis % (1000*60*60) ) / (1000*60));
int seconds = (int) (( ( millis % (1000*60*60) ) % (1000*60) ) / 1000);
buf
.append(String.format("%02d", hours))
.append(":")
.append(String.format("%02d", minutes))
.append(":")
.append(String.format("%02d", seconds));
return buf.toString();
}
Thanks always guys
There are 1000 milliseconds in one second, i.e. you'd need 3 decimal places for the milliseconds:
/** return time in format 1:23.456 */
private String getTimeString(long millis) {
int minutes = (int) (millis / (1000 * 60));
int seconds = (int) ((millis / 1000) % 60);
int milliseconds = (int) (millis % 1000);
String.format("%d:%02d.%03d", minutes, seconds, milliseconds);
}
If you absolutely want 2 digits for milliseconds, you actually get 1/100 seconds and not milliseconds:
/** return time in format 1:23.45 */
private String getTimeString(long millis) {
int minutes = (int) (millis / (1000 * 60));
int seconds = (int) ((millis / 1000) % 60);
int seconds100 = (int) ((millis / 10) % 100);
String.format("%d:%02d.%02d", minutes, seconds, seconds100);
}
However, a common display format for media players is to use one digit for 10ths of seconds:
/** return time in format 1:23.4 */
private String getTimeString(long millis) {
int minutes = (int) (millis / (1000 * 60));
int seconds = (int) ((millis / 1000) % 60);
int seconds10 = (int) ((millis / 100) % 10);
String.format("%d:%02d.%d", minutes, seconds, seconds10);
}
If you want to put the milliseconds at two decimal places, keep in mind that you will only be able to show increments of 10ms; 1ms = 0.001s, three decimal places. But regardless, the code you are looking for is:
int rem_milliseconds = (int)(millis % 1000); // Remaining ms after last second
...
.append(String.format("%02d", rem_milliseconds));
I left it at 2 decimal places.
Try this:
private String getTimeString(long millis) {
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));
millis -= TimeUnit.MINUTES.toMillis(minutes) + TimeUnit.SECONDS.toMillis(seconds);
return String.format("%d:%d:%d", minutes, seconds, millis);
}
Related
I have this code
public void LoadCurrentTime(long time_last) {
long millis_now = System.currentTimeMillis();
long time_till = millis_now - time_last;
String showTime;
int showHours, showMin, showSec;
if (time_till > 604800000) {
showTime = getString(R.string.long_ago);
} else {
//long longHours = (time_till / 1000 / 60 / 60);
//long longMin = (time_till / 1000 / 60 % 60);
//long longSec = (time_till / 1000 % 60);
//showHours = (int) longHours;
//showMin = (int) longMin;
//showSec = (int) longSec;
//showTime = String.format("%02d", showHours) + ":" + String.format("%02d", showMin) + ":" + String.format("%02d", showSec);
// v1.0^
long longsec = (time_till / 1000) % 60;
long longmin = (time_till / (1000 * 60)) % 60;
long longhours = (time_till / (1000 * 60 * 60)) % 24;
showHours = (int) longhours;
showMin = (int) longmin;
showSec = (int) longsec;
showTime = String.format("%02d", showHours) + ":" + String.format("%02d", showMin) + ":" + String.format("%02d", showSec);
}
TextView lastTime = findViewById(R.id.textView4);
I want to update a TextView content every second and show a notification, but the TextView update and the notification show only when open the app, so
How can I update the TextView and show notification every one second in background?
You can user Timer and TimerTask to for recurring tasks. Don't forget to call timer.cancel() at end of the program.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//Call your method
LoadCurrentTime()
}
},0, 2000);
Add Hours in Count Down Timer , i have only minutes then how to format it
private void updateCountDownText() {
int hours = (int) (mTimeLeftInMillis / 1000) / 36000;
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
}
I'd recommend to use TimeUnit instead of manually converting time.
Try this:
int hours = TimeUnit.MILLISECONDS.toHours(mTimeLeftInMillis);
int minutes = TimeUnit.MILLISECONDS.toMinutes(mTimeLeftInMillis) % TimeUnit.HOURS.toMinutes(1);
int seconds = TimeUnit.MILLISECONDS.toSeconds(mTimeLeftInMillis) % TimeUnit.MINUTES.toSeconds(1);
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
This part of the program is for calculating how long it takes to face a part.
It starts with some basic info, work piece diameter, feed rate and surface speed you want to tool to operate at. Then it runs a while loop, each time the tool advances 0.010", it calculates the new rpm the piece will rotate at and calculates the time for that cut adding it all up at the end.
The problem: I need to be able to limit the rpms. As the tool gets closer to the center of the work piece the rpms will climb to a very high unattainable rpm, I want to be able to set a limit, 2000 for example.
I cannot figure out how to do that with out affecting my loop... I have searched, but I'm such a noob maybe I've stumbled across a solution that would work and never realized it, or I am not searching for the correct key words. Here is my code:
public static void main(String[] args) {
double startRadius = 6; //Radius of stock diameter
double faceFinish = 0;
double feed = .010; //Amount the tool will advance per revolution
double sfm = 200; //Surface speed of tool (Surface feet per minute)
double rpm = 0;
double totalTime = 0;
while(faceFinish < startRadius) {
startRadius -= feed; //reduces diameter by feed
rpm = (sfm * 3.82) / (startRadius * 2); //establishes new rpm per tool advance
totalTime += (feed / (feed * rpm)) * 60;
}
int hours = (int) (totalTime / 3600);
int minutes = (int) ((totalTime % 3600) / 60);
int seconds = (int) (totalTime % 60);
System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds);
}
Edit - If/else which seems to be working.
public static void main(String[] args) {
double startRadius = 6;
double faceFinish = 0;
double feed = .010;
double sfm = 200;
double rpm = 0;
double rpm2 = 0;
double total = 0;
double total2 = 0;
double totalTime = 0;
while(faceFinish < startRadius) {
startRadius -= feed;
rpm = (sfm * 3.82) / (startRadius * 2);
if(rpm > 2000) {
rpm = 2000;
total += (feed / (feed * rpm)) * 60;
}else {
total2 += (feed / (feed * rpm)) * 60;
}
totalTime = total + total2;
}
int hours = (int) (totalTime / 3600);
int minutes = (int) ((totalTime % 3600) / 60);
int seconds = (int) (totalTime % 60);
System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds);
}
Use the below code snippet for the while loop you are using
while(faceFinish < startRadius) {
startRadius -= feed; //reduces diameter by feed
rpm = ((sfm * 3.82) / (startRadius * 2))>2000?2000:((sfm * 3.82) / (startRadius * 2));
totalTime += (feed / (feed * rpm)) * 60;
}
Please try below code.
class Rpm{
double value=0.0;
final double LIMIT = 2000.0;
public void setValue(double value){
if(value < LIMIT)
this.value= value;
}
public double getValue(){
return this.value;
}
}
public class yourclassname {
public static void main(String[] args) {
double startRadius = 6; //Radius of stock diameter
double faceFinish = 0;
double feed = .010; //Amount the tool will advance per revolution
double sfm = 200; //Surface speed of tool (Surface feet per minute)
Rpm rpm = new Rpm();
double totalTime = 0;
while(faceFinish < startRadius) {
startRadius -= feed; //reduces diameter by feed
rpm.setValue( (sfm * 3.82) / (startRadius * 2)); //establishes new rpm per tool advance
totalTime += (feed / (feed * rpm.getValue())) * 60;
}
int hours = (int) (totalTime / 3600);
int minutes = (int) ((totalTime % 3600) / 60);
int seconds = (int) (totalTime % 60);
System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds);
}
}
txtTimerDay = (TextView) findViewById(R.id.txtTimerDay);
txtTimerHour = (TextView) findViewById(R.id.txtTimerHour);
txtTimerMinute = (TextView) findViewById(R.id.txtTimerMinute);
txtTimerSecond = (TextView) findViewById(R.id.txtTimerSecond);
tvEvent = (TextView) findViewById(R.id.tvhappyevent);
countDownStart();
}
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
// Please here set your event date//YYYY-MM-DD
Date futureDate = dateFormat.parse("2017-03-18");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
Okay... Here is my question... Currently in this code... The Output which includes the layout and some more lines...
Will generate a proper timer heading towards march 18th... However... I need the timer to countdown towards march 18th at 20.30 at night.. Any Help will be appreciated and please tolerate me.. i am new in this website
import java.util.Date;
import java.util.Calendar;
public class cal {
public static int SECONDS_IN_A_DAY = 24 * 60 * 60;
public static void main(String[] args) {
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(new Date(0)); /* reset */
thatDay.set(Calender.HOUR_OF_DAY,2);/*here Add ur Time */
thatDay.set(Calendar.DAY_OF_MONTH,1);
thatDay.set(Calendar.MONTH,0); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
Calendar today = Calendar.getInstance();
long diff = thatDay.getTimeInMillis() - today.getTimeInMillis();
long diffSec = diff / 1000;
long days = diffSec / SECONDS_IN_A_DAY;
long secondsDay = diffSec % SECONDS_IN_A_DAY;
long seconds = secondsDay % 60;
long minutes = (secondsDay / 60) % 60;
long hours = (secondsDay / 3600); // % 24 not needed
System.out.printf("%d days, %d hours, %d minutes and %d seconds\n", days, hours, minutes, seconds);
}
}
Try To use Calender It will help to set your Time and Date Try it....do not forget to accept if goal is accomplished
Brief Java question:
I have this code:
// edited above
} else if ((now >= (1000 * 60 * 60 * 24) && (now < (1000 * 60 * 60 * 48)))) {
now = (now / (1000 * 60 * 60 * 24));
time = String.valueOf(now + " day ago");
} else if ((now >= (1000 * 60 * 60 * 48) && (now < (1000 * 60 * 60 * 24 * 30)))) {
now = (now / (1000 * 60 * 60 * 24));
time = String.valueOf(now + " days ago");
You can see what I am doing. If something happened between 24-48 hours ago, it was "1 day ago", but if it was between 48 hours and 1 month (or 30 days) it was "X day*s* ago." I am not sure how to put the month in this situation. If I delete the right side of the second if statement... it works. It will say 3 days ago, or 15 days ago, of 376 days ago. Obviously, after 30 days, I want it to say over 1 month ago.... but based on how it is, something that is, for example, 15 days ago, will not register inside that clause and it will skip. Am I missing something really small here?
Supplementary code:
Here is the entire thing in context; everything works up until "days":
lCDateTime = Calendar.getInstance();
now = lCDateTime.getTimeInMillis();
now = now - total;
if (now <= ((1000 * 60 * 1))) {
now = (now / 1000);
time = String.valueOf(now + " seconds ago");
} else if ((now > (1000 * 60 * 1)) && (now < 1000 * 60 * 2)) {
now = (now / (1000 * 60));
time = String.valueOf(now + " minute ago");
} else if ((now >= (1000 * 60 * 2)) && (now < 1000 * 60 * 60)) {
now = (now / (1000 * 60));
time = String.valueOf(now + " minutes ago");
} else if ((now >= (1000 * 60 * 60) && now < (1000 * 60 * 60 * 2))) {
now = (now / (1000 * 60 * 60));
time = String.valueOf(now + " hour ago");
} else if ((now >= (1000 * 60 * 60 * 2) && (now < (1000 * 60 * 60 * 24)))) {
now = (now / (1000 * 60 * 60));
time = String.valueOf(now + " hours ago");
} else if ((now >= (1000 * 60 * 60 * 24) && (now < (1000 * 60 * 60 * 48)))) {
now = (now / (1000 * 60 * 60 * 24));
time = String.valueOf(now + " day ago");
} else if ((now >= (1000 * 60 * 60 * 48) && (now < (1000 * 60 * 60 * 24 * 30)))) {
now = (now / (1000 * 60 * 60 * 24));
time = String.valueOf(now + " days ago");
Your last statement
else if ((now >= (1000 * 60 * 60 * 48) && (now < (1000 * 60 * 60 * 24 * 30))))
can never be true since (1000 * 60 * 60 * 24 * 30) equals -1702967296 (aka overflow). If you really insist on doing it like this, use BigInteger. However I strongly advise you to rewrite your code.