Is there a way to convert the event.timestamp to regular metrics. My current output is 267185117663619 (nanosecond??). Is there a way to conver this to, let's say miliseconds or even seconds?
timestamp = String.valueOf(event.timestamp);
Thanks in advance!
Well, you could just divide it by 1,000,000,000 .
This way:
double seconds = (double)Long.parseLong(timestamp)/1000000000.0;
Related
I am looking to calculate the number of minutes given the time of the day.
Eg.: when input is 11:34, the output should be 11*60+34. The date doesn't matter.
I only need it down to the minutes scale. Seconds, milliseconds... don't matter.
Is there a method somewhere in Java doing this the neat way without me calculating it?
Right now, i'm using theTime.split(":"), theTime is a String holding "11:34" here, parsing the integers on each side and doing the calculation.
I saw Time but what I'm doing right now seemed more direct.
Nothing in Systems either.
There is no build in method for it. However here is a one-liner for it:
int timeInMins = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) * 60 + Calendar.getInstance().get(Calendar.MINUTE);
Your approach looks good and sound, however to answer your question it would be simple to say that there is no such build in method which does that. You have to calculate it the way you are doing it right now.
Hi maybe you could use JodaTime? Below example how to get number of minutes from parsed string and from current time. In java 8 there is similar api but I haven't found exactly method like minutesOfDay()
#Test
public void learnHowManyMinutesPassedToday() {
DateTime time = DateTimeFormat.forPattern("HH:mm").parseDateTime("11:34");
System.out.println(time.getMinuteOfDay());
System.out.println(DateTime.now().getMinuteOfDay());
}
If you are looking to have input not from a String, take a look at
Java.util.Calendar.
It has Calendar.HOUR_OF_DAY and Calendar.HOUR and Calendar.MINUTE which could be your input. I'm not sure what the "neat" way of doing this would be. It is a simple calculation.
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR);
int min = rightNow.get(Calendar.MINUTE);
System.out.println("TimeMinutes:" + hour * 60 + min);
EDIT:
Except using split use the above.
Please Help me on this.
I had two duration times in string format:
Ex: "10" and "0.30"(actually there are in minutes)
I parsed those values Double and find the find the difference?
But I am getting the output as 9.7 which is wrong
I want it as 9.30.. Please suggest
Thanks in advance!
You can use the Joda-Time Library.Use the Period class from which you can get the difference between two times in terms of hours and minutes.For example refer the second answer to this question
Convert the double into minutes, Here is the link that can help you Convert the string "8:00" into the minutes (integer value).
i am learning to program mobile aplications on Android. My first app is a unit converter. Everithing is working for now, but i have a question about formating numbers. I hava this code to get text from buttons and to convert the appropriet output:
if (bPrevodZ.getText() == "milimeter"){
if (bPrevodDo.getText()=="kilometer"){
String PomocnaPremenna = jednotkaZ.getText().toString();
double cisloNaPrevod = Double.parseDouble(PomocnaPremenna);
cisloNaPrevod = cisloNaPrevod*0.0000001;
vysledok.setText(Double.toString(cisloNaPrevod));
}
The final result is "cisloNaPrevod", but i have problems to show a good format of that number. For example:
12345 mm = 0,0012345 km this is good right ? :)
but if i convert:
563287 mm = 0.05632869999999995 this is bad :) i need it to show 0.0563287
Thx for any help
Use String.format:
String.format("%.6f", cisloNaPrevod);
If you want your number to always have 6 significant figures, use
vysledok.setText(String.format("%.6g", cisloNaPrevod));
giving the result 0.0563287.
If you want to round to 6 numbers after the decimal place, use
vysledok.setText(String.format("%.6f", cisloNaPrevod));
giving the result 0.056329.
Here's some good resources that cover number formatting:
Floating-point cheat sheet for Java
java.util.Formatter
If it's something you're going to do often, perhaps you should use DecimalFormat.
DecimalFormat df = new DecimalFormat("#.#######");
Then call:
df.format(someDoubleValue);
I'm an objective-c beginner and I was assigned to create an iPhone app for our client. I have some background with Java but almost no experience in this objective-c and this is my first time to developping a complete application...
Anyway, I'm currently stack at several problems. One of those problem is that I need to send an integer value for PHP's date function from my iOS app. I've been searching around for the solution, but all of them are dealing with opposite ways (int to NSDate), not NSDate to integer value.
I tried solutions like answered here
but it's obvious it returns double, not an integer...
Or this
but this couldn't get the System time.
I know I could get current system's NSDate with:
NSDate *theDay = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]];
But I could not figure out how to convert this to an integer (or long) value.
I just need to get the same value as we can get in Java with System.currentTimeMillis().
Also, this is my first time to ask questions here in stackoverflow. So please let me know if there's anything I should do/not to do when posting questions here, etc.
Thank you.
To get the current date, you should use this:
NSDate *theDate = [NSDate date];
To get it in seconds since January 1st, 1970, as a double, you would use:
NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
To get it in ms, simply multiply the previous value by 1000 and let the compiler cast it into an integer without needing any additional code:
long long milliseconds = [[NSDate date] timeIntervalSince1970] * 1000;
And as #HotLicks points out, while System.currentTimeMillis() is in GMT, if you need the local time, you could use:
long long milliseconds = ([[NSDate date] timeIntervalSince1970]
+ [[NSTimeZone defaultTimeZone] secondsFromGMT]) * 1000;
(I think that most web services will want GMT though.)
I need to calculate the time passed between two dates.
The catch here is that I need to show it as YouTube does with its video comments timestamps. That is, to show it by just the largest measure.
For example,
if the time is 50 seconds ago it should say 50 seconds ago.
if the time is more than one minute it should say one minute ago/ten minutes ago etc..
if the time difference is 1 hour 30 mins it should show: an hour ago.
if the time is one and a half week than it should say one week ago.
if the time is more than a month it should say one month ago/two months ago etc...
and so on and so on..
So what is the best way to handle this?
Should I make a method with case or if statements that would return something like this? Or is there a better approach (maybe a library which already does something like it)?
Use DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution). time is the start time, and now is the end time (in milliseconds). To report "seconds ago," set minResolution to zero.
Example:
String result = DateUtils.getRelativeTimeSpanString(1306767830, 1306767835, 0);
// result = "5 seconds ago"
date1.getTime() - date2.getTime()
This will return you the time in miliseconds between the 2 dates. Just convert that to what ever you want to show (e.g. hours minutes seconds)
On Android, use this: http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString%28android.content.Context,%20long,%20boolean%29
Take a look at PrettyTime!
Also, everytime you want to do something date/time-related in Java, you should take a look at Joda Time. Do it now, you will thank me later.
Your need is very specific, and I don't know any lib that would solve the problem for you out of the box.
However the problem is not very complex and a small function full of "ifs" should do the trick.
Of course, a nice date library like Joda Time will help you keep your code clean. Who wants to use GregorianCalendar!?
Looks like you have a set of custom rules and the algorithm to choose a rule is based on the time in seconds between two timestamps. The easiest approach is to handle the rules in a series of if/else if statements:
private String getTimeAsString(int seconds) {
if (seconds < 60) { // rule 1
return String.format("%s seconds ago", seconds);
} else if (seconds < 3600) { // rule 2
return String.format("%s minutes ago", seconds/60);
} // ... and so on
}