Format Days from Milliseconds - java

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.

Related

Is there something wrong with my formula?

My code calculates the average height of a child once they become adult using their parents heights
I am getting height for the child being 7 feet+ when the mother and father are between 4-6 feet
Hmale_child = ((Hmother * 13 / 12) + Hfather ) / 2
Hfemale_child = ((Hfather * 12 / 13) + Hmother) / 2
if (gender.equalsIgnoreCase("Male")) {
childHeightInch = (((monHeightInch + (monHeightFeet * 12)) * 13 / 12) + (dadHeightInch + (dadHeightFeet * 12)) / 2);
childHeightFeet = childHeightInch / 12;
childHeightInch = childHeightInch % 12;
} else if (gender.equalsIgnoreCase("Female")) {
childHeightInch = (((dadHeightInch + (dadHeightFeet * 12)) * 12 / 13) + (monHeightInch + (monHeightFeet * 12)) / 2);
childHeightFeet = childHeightInch / 12;
childHeightInch = childHeightInch % 12;
Add some Brackets,it is only dividing dad height by 2
childHeightInch = (((((monHeightInch + (monHeightFeet * 12)) * 13) / 12) + (dadHeightInch + (dadHeightFeet * 12))) / 2);
childHeightInch = (((((dadHeightInch + (dadHeightFeet * 12)) * 12) / 13) + (monHeightInch + (monHeightFeet * 12))) / 2);
If you try to do all the calculations on one line, then no matter how you do it, you'll end up with far too many parentheses. The result is that it will be difficult to read the code accurately, and you'll make mistakes. As indeed you have done.
Don't write any parentheses that you don't mathematically need. And consider calculating the total heights of Mum and Dad separately first, as shown below.
int dadsHeightTotal = dadsHeightInches + dadsHeightFeet * 12;
int mumsHeightTotal = mumsHeightInches + mumsHeightFeet * 12;
if (gender.equalsIgnoreCase("male")) {
childsHeightTotal = ( mumsHeightTotal * 13 / 12 + dadsHeightTotal ) / 2;
} else {
childsHeightTotal = ( dadsHeightTotal * 12 / 13 + mumsHeightTotal ) / 2;
}
childsHeightFeet = childsHeightTotal / 12;
childsHeightInches = childsHeightTotal % 12;

Update textview every x seconds to show notification in background

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);

Countdown Timer that is date and time specific

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

Implementation of the Riemann Siegel Formula in Java, curious about improving the remainder term

I wrote a basic program which calculates the Riemann-Siegel Z(t) function. I was curious if there is a better way to approximate the remainder term. The method I have now uses the awful table approximations from Haselgrove.
Further information about the Riemann Siegel formula. This might be a tad bit advanced, although further details of this are found in this thesis. Also, in Edwards book.
I know that my for loop is not optimal, I was just using it for testing purposes. I can use a different method to approximate zeroes.
Here is the implementation that I wrote:
import java.util.*;
public class Main {
public static void main(String[] args) {
double s[] = new double[10];
s[0] = 2;
for (double i = 0; i < 500; i += 0.0001) {
if (RiemennZ(i, 4) < 0.0001 && RiemennZ(i, 4) > -1*0.0001)
System.out.println("Found a zero at " + i + ", the value of Zeta(s) is "
+ RiemennZ(i, 4));
}
//System.out.println(4);
//System.out.println("Value of the Zeta Function " + Arrays.toString(Riemann.zeta(s)));
System.out.println("The function you wrote is- " + RiemennZ(16, 4));
System.out.println(fAbs(1.3) -1.0);
//System.out.println(theta(25));
}
// Riemann-Siegel theta function using the approximation by the Stirling series
public static double theta (double t) {
return (t/2.0 * Math.log(t/(2.0*Math.PI)) - t/2.0 - Math.PI/8.0
+ 1.0/(48.0*Math.pow(t, 1)) + 7.0/(5760*Math.pow(t, 3)));
}
// Computes Math.Floor of the absolute value term passed in as t.
public static double fAbs(double t) {
return Math.floor(Math.abs(t));
}
// Riemann-Siegel Z(t) function implemented per the Riemenn Siegel formula.
// See http://mathworld.wolfram.com/Riemann-SiegelFormula.html for details
public static double RiemennZ(double t, int r) {
double twopi = Math.PI * 2.0;
double val = Math.sqrt(t/twopi);
double n = fAbs(val);
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += (Math.cos(theta(t) - t * Math.log(i))) / Math.sqrt(i);
}
sum = 2.0 * sum;
// Add the remainder terms
double remainder;
double frac = val - n;
int k = 0;
double R = 0.0;
while (k <= r) {
R = R + C(k, 2.0*frac-1.0) * Math.pow(t / twopi, ((double) k) * -0.5);
k++;
}
remainder = Math.pow(-1, (int)n-1) * Math.pow(t / twopi, -0.25) * R;
return sum + remainder;
}
// C terms for the Riemann-Siegel formula
public static double C (int n, double z) {
if (n==0)
return(.38268343236508977173 * Math.pow(z, 0.0)
+.43724046807752044936 * Math.pow(z, 2.0)
+.13237657548034352332 * Math.pow(z, 4.0)
-.01360502604767418865 * Math.pow(z, 6.0)
-.01356762197010358089 * Math.pow(z, 8.0)
-.00162372532314446528 * Math.pow(z,10.0)
+.00029705353733379691 * Math.pow(z,12.0)
+.00007943300879521470 * Math.pow(z,14.0)
+.00000046556124614505 * Math.pow(z,16.0)
-.00000143272516309551 * Math.pow(z,18.0)
-.00000010354847112313 * Math.pow(z,20.0)
+.00000001235792708386 * Math.pow(z,22.0)
+.00000000178810838580 * Math.pow(z,24.0)
-.00000000003391414390 * Math.pow(z,26.0)
-.00000000001632663390 * Math.pow(z,28.0)
-.00000000000037851093 * Math.pow(z,30.0)
+.00000000000009327423 * Math.pow(z,32.0)
+.00000000000000522184 * Math.pow(z,34.0)
-.00000000000000033507 * Math.pow(z,36.0)
-.00000000000000003412 * Math.pow(z,38.0)
+.00000000000000000058 * Math.pow(z,40.0)
+.00000000000000000015 * Math.pow(z,42.0));
else if (n==1)
return(-.02682510262837534703 * Math.pow(z, 1.0)
+.01378477342635185305 * Math.pow(z, 3.0)
+.03849125048223508223 * Math.pow(z, 5.0)
+.00987106629906207647 * Math.pow(z, 7.0)
-.00331075976085840433 * Math.pow(z, 9.0)
-.00146478085779541508 * Math.pow(z,11.0)
-.00001320794062487696 * Math.pow(z,13.0)
+.00005922748701847141 * Math.pow(z,15.0)
+.00000598024258537345 * Math.pow(z,17.0)
-.00000096413224561698 * Math.pow(z,19.0)
-.00000018334733722714 * Math.pow(z,21.0)
+.00000000446708756272 * Math.pow(z,23.0)
+.00000000270963508218 * Math.pow(z,25.0)
+.00000000007785288654 * Math.pow(z,27.0)
-.00000000002343762601 * Math.pow(z,29.0)
-.00000000000158301728 * Math.pow(z,31.0)
+.00000000000012119942 * Math.pow(z,33.0)
+.00000000000001458378 * Math.pow(z,35.0)
-.00000000000000028786 * Math.pow(z,37.0)
-.00000000000000008663 * Math.pow(z,39.0)
-.00000000000000000084 * Math.pow(z,41.0)
+.00000000000000000036 * Math.pow(z,43.0)
+.00000000000000000001 * Math.pow(z,45.0));
else if (n==2)
return(+.00518854283029316849 * Math.pow(z, 0.0)
+.00030946583880634746 * Math.pow(z, 2.0)
-.01133594107822937338 * Math.pow(z, 4.0)
+.00223304574195814477 * Math.pow(z, 6.0)
+.00519663740886233021 * Math.pow(z, 8.0)
+.00034399144076208337 * Math.pow(z,10.0)
-.00059106484274705828 * Math.pow(z,12.0)
-.00010229972547935857 * Math.pow(z,14.0)
+.00002088839221699276 * Math.pow(z,16.0)
+.00000592766549309654 * Math.pow(z,18.0)
-.00000016423838362436 * Math.pow(z,20.0)
-.00000015161199700941 * Math.pow(z,22.0)
-.00000000590780369821 * Math.pow(z,24.0)
+.00000000209115148595 * Math.pow(z,26.0)
+.00000000017815649583 * Math.pow(z,28.0)
-.00000000001616407246 * Math.pow(z,30.0)
-.00000000000238069625 * Math.pow(z,32.0)
+.00000000000005398265 * Math.pow(z,34.0)
+.00000000000001975014 * Math.pow(z,36.0)
+.00000000000000023333 * Math.pow(z,38.0)
-.00000000000000011188 * Math.pow(z,40.0)
-.00000000000000000416 * Math.pow(z,42.0)
+.00000000000000000044 * Math.pow(z,44.0)
+.00000000000000000003 * Math.pow(z,46.0));
else if (n==3)
return(-.00133971609071945690 * Math.pow(z, 1.0)
+.00374421513637939370 * Math.pow(z, 3.0)
-.00133031789193214681 * Math.pow(z, 5.0)
-.00226546607654717871 * Math.pow(z, 7.0)
+.00095484999985067304 * Math.pow(z, 9.0)
+.00060100384589636039 * Math.pow(z,11.0)
-.00010128858286776622 * Math.pow(z,13.0)
-.00006865733449299826 * Math.pow(z,15.0)
+.00000059853667915386 * Math.pow(z,17.0)
+.00000333165985123995 * Math.pow(z,19.0)
+.00000021919289102435 * Math.pow(z,21.0)
-.00000007890884245681 * Math.pow(z,23.0)
-.00000000941468508130 * Math.pow(z,25.0)
+.00000000095701162109 * Math.pow(z,27.0)
+.00000000018763137453 * Math.pow(z,29.0)
-.00000000000443783768 * Math.pow(z,31.0)
-.00000000000224267385 * Math.pow(z,33.0)
-.00000000000003627687 * Math.pow(z,35.0)
+.00000000000001763981 * Math.pow(z,37.0)
+.00000000000000079608 * Math.pow(z,39.0)
-.00000000000000009420 * Math.pow(z,41.0)
-.00000000000000000713 * Math.pow(z,43.0)
+.00000000000000000033 * Math.pow(z,45.0)
+.00000000000000000004 * Math.pow(z,47.0));
else
return(+.00046483389361763382 * Math.pow(z, 0.0)
-.00100566073653404708 * Math.pow(z, 2.0)
+.00024044856573725793 * Math.pow(z, 4.0)
+.00102830861497023219 * Math.pow(z, 6.0)
-.00076578610717556442 * Math.pow(z, 8.0)
-.00020365286803084818 * Math.pow(z,10.0)
+.00023212290491068728 * Math.pow(z,12.0)
+.00003260214424386520 * Math.pow(z,14.0)
-.00002557906251794953 * Math.pow(z,16.0)
-.00000410746443891574 * Math.pow(z,18.0)
+.00000117811136403713 * Math.pow(z,20.0)
+.00000024456561422485 * Math.pow(z,22.0)
-.00000002391582476734 * Math.pow(z,24.0)
-.00000000750521420704 * Math.pow(z,26.0)
+.00000000013312279416 * Math.pow(z,28.0)
+.00000000013440626754 * Math.pow(z,30.0)
+.00000000000351377004 * Math.pow(z,32.0)
-.00000000000151915445 * Math.pow(z,34.0)
-.00000000000008915418 * Math.pow(z,36.0)
+.00000000000001119589 * Math.pow(z,38.0)
+.00000000000000105160 * Math.pow(z,40.0)
-.00000000000000005179 * Math.pow(z,42.0)
-.00000000000000000807 * Math.pow(z,44.0)
+.00000000000000000011 * Math.pow(z,46.0)
+.00000000000000000004 * Math.pow(z,48.0));
}
}
The remainder terms are defined by: (cos[2pi(p^2-p-1/(16))])/(cos(2pip))
Doing multiple derivatives of this function inside Wolfram Alpha is a complete mess. Has anyone ever experienced this sort of problem before?
In order to use multiple remainder terms, I need to compute multiple derivatives for: (cos[2pi(p^2-p-1/(16))])/(cos(2pip))
Is there some way around this that can be implemented in Java?
One way is by using finite difference methods. This is not a very good solution but is the first thing that I thought about.
// Derivation of the first C term using first order central difference
public static double firstDerivative(double p) {
double epsilon = 0.0000000001;
double d1, d2;
double dx = 0.00001;
double diff = 1.0;
d1 = (function(p + dx) - function(p - dx)) / (2 * dx);
while (diff > epsilon) {
dx /= 2;
d2 = (function(p + dx) - function(p - dx)) / (2 * dx);
diff = Math.abs(d2 - d1);
d1 = d2;
}
return d1;
}
// Derivation of the second C term using second order central difference
public static double secondDerivative(double p) {
double epsilon = 0.0000000001;
double d1, d2;
double dx = 0.00001;
double diff = 1.0;
d1 = (function(p + dx) - 2.0 * function(p) + function(p - dx)) / Math.pow(dx, 2);
while (diff > epsilon) {
dx /= 2;
d2 = (function(p + dx) - 2.0 * function(p) + function(p - dx)) / Math.pow(dx, 2);
diff = Math.abs(d2 - d1);
d1 = d2;
}
return d1;
}
How many derivatives do you need?
Do you want to pre-calculate them or do them "on-the-fly"?
You can use GeoGebra (in Java) easily to pre-calculate them, eg
CopyFreeObject[Derivative[cos(2π (x² - x - 1 / 16)) / cos(2π x), 3]]
If you want to delve a bit more, internally GeoGebra can either use the Giac CAS engine (in C++) to do derivatives, or it can calculate them directly, see ExpressionNode.derivative()

Getting millisecond format

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);
}

Categories