Get timestamp and convert it to string - java

I'm using this to get current timestamp in seconds and add it to a string Double.toString((System.currentTimeMillis()/1000))
However instead of decimal notation I get "1.23213E9". How do I switch to the decimal notation ?

The shortest is
String secs = "" + System.currentTimeMillis() / 1000;
If you want to retain milli-seconds you can use
String secs = String.format("%.3f", System.currentTimeMillis() / 1000.0);
produces a String like
1342604140.503

Try this:
TimeUnit.MILLISECONDS.toSeconds(((System.currentTimeMillis());

String.valueOf(System.currentTimeMillis() / 1000)
that should do the trick? No need to convert it to a double

If you need to deal with a Double you could do something like this:
double myNum = 1.23213E9;
String myString = NumberFormat.getInstance().format(myNum);
System.out.print(myString);

Related

Java Timestamp with Fractional Seconds

How can I print Java Instant as a timestamp with fractional seconds like 1558766955.037 ? The precision needed is to 1/1000, as the example shows.
I tried (double) timestamp.getEpochSecond() + (double) timestamp.getNano() / 1000_000_000, but when I convert it to string and print it, it shows 1.558766955037E9.
The result you're seeing is the secientific (e-) notation of the result you wanted to get. In other words, you have the right result, you just need to properly format it when you print it:
Instant timestamp = Instant.now();
double d = (double) timestamp.getEpochSecond() + (double) timestamp.getNano() / 1000_000_000;
System.out.printf("%.2f", d);
As others pointed out it is formatting issue. For your specific format you could use Formatter with Locale that supports dot delimetted fractions :
Instant now = Instant.now();
double val = (double) now.getEpochSecond() + (double) now.getNano() / 1000_000_000;
String value = new Formatter(Locale.US)
.format("%.3f", val)
.toString();
System.out.print(value);
Prints :
1558768149.514
System.out.printf("%.3f", instant.toEpochMilli() / 1000.0) should work.

How to format longs in android to always display two digits

I have a countdown timer which shows seconds from 60 to 0 (1 min countdown timer). When it reaches 1 digit numbers such as 9,8,7.. it shows 9 instead of 09. I tried using String.format("%[B]02d[/B]", x); where I converted x from long to string. It didn't work.
I want an equivalent of String.format("%2d", 1)
You can accomplish it with DecimalFormat:
NumberFormat f = new DecimalFormat("00");
long time = 9;
textView.setText(f.format(time));
Output:
09
Or you can use String.format() as well:
String format = "%1$02d"; // two digits
textView.setText(String.format(format, time));
Use: text.setText(String.format("%02d", i)); where i is the integer value
Why not just use an if statement?
String str = x < 10 ? "0" + String.valueOf(x) : String.valueOf(x);
That should do the trick.
TextView time;
int hour=0,minute=0,second=0;
time.setText((String.format("%02d", hour))+":"+(String.format("%02d", minute))+":"+(String.format("%02d", second)));
time to TextView
Try using this:
tv.setText(new DecimalFormat("##").format(var));

Converting string like "15:45" into a double like 15.45

I'm struggling to figure out how to convert a string like "15:45" into a double like 15.45,
I have previously come across methods like .toString() and was looking up a similar method to use in order to convert string to double, however there are several issues, I need to figure out how to make : be a place where dot . is inserted in double.
This is where I would like to use it.
if(westOne > (takeOff.firstInQueue().time)) {}
At the moment westOne is a double where as takeOff.firstInQueue().time is a string like "16.15" I'm assuming I'll need to create my own method to get this working so I can than apply it at the end of it?
You can simply:
myDouble = Double.parseDouble(myStr.replace(":", "."));
If that's really what you want, this will replace : with . and the String x:y will be the double x.y.
Edit:
If you want to add 5 minutes to the time, you can add 0.05 to the double and convert it back to String, but this is not a good way of doing it because:
You should take care of cases like 14:55, adding 0.05 will result 14.6.
And cases like 14:45 where adding 0.05 will result 14.5 (No 0 after 5).
If all what you want is to add 5 minutes to the time, you can use SimpleDateFormat and Calendar:
String myStr = "17:23";
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
Date date = df.parse(myStr );
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, 5);
myStr = dateFormat.format(calendar.getTime()); //Now myStr will be 17:28
I think this would work;
String value;
double value1;
value = String.replaceAll(":",".") // Converts the 15:45 to 15.45
value1 = Double.parseDouble(value) // Converts the 15.45 to a double value.
String timevalue = "15:45";
Double pointvalue = Double.parseDouble(timevalue.replace(':', '.'));
//Not double quote like the previous answer because it takes a char as a parameter and not a string

Convert cents to euro with NumberFormat

I have a long type value which represents cents in currency. I try to convert it to euros. So, I did the following:
long val = 348;
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
System.out.println(nf.format(val/100));
I thought the above code will print out 3,48 € but I got 3,00 €. Why?
because val/100 is an Integer operation, so the deciaml part is stripped away. For instane
int i = 1; int result = i / 2; will give you 0
Change to System.out.println(nf.format(((float)val/100)));
To force floating point arithmetic , you can use a double literal 100.0 or float literal 100.0f :
System.out.println(nf.format(val/100.0));

How can I format my results to show the digits after the decimal point

I need a line of code that I can use to get the digits after the decimal place when I execute
the code below:
double results = 1500 / 1000;
txtview.setText("K " + resultsoldk);
I need the results to include the reminder as well.
The problem is that the result of
double results = 1500 / 1000;
is 1.0, not 1.5, because you are doing integer division. Make sure at least one of the numbers is a floating-point number when you do the division. For example:
// By adding .0 you make it a double literal instead of an int literal
double results = 1500.0 / 1000;
You can cast using double like below
double results = (double)1500 / 1000;
Also you can format the result using DecimalFormat
DecimalFormat df = new DecimalFormat("0.00##");
String formattedResult=df.format(results);
You can use the String.format(...) method to format the String like this:
txtview.setText(String.format("K %f", resultsoldk));
However, this will not work in you case. As you are dividing two integers, the result will be an integer (which has no remainder). Afterwards it is converted to a double (because you assign it to a double) but at this point, the precision is already lost. You must at least convert one of the integers to a double before you divide them:
double results = (double)1500 / 1000;
or, if you use constants
double results = 1500.0 / 1000;
or
double results = 1500 / 1000.0;
or even
double results = 1500.0 / 1000.0;
String strResult = Double.toString(d);
strResult = strResult.replaceAll("\\d?\\.", "");
Don't know if I get it, but if what you want is the digits after the dot, then you can use this regex.

Categories

Resources