Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
So, my first app will be a personal one to help me meet deadlines. As you would imagine it is going to be very date oriented.
So, how do I grab the current date from my phone/emulator?
Bonus points if you can explain how to put it into an integer.
Any link or tutorial would be super great.
Thanks in advance.
You can get the time of your phone by creating a calender.
Calendar calendar = Calendar.getInstance();
This stores your actual time. If you want to get the date in a number, just us this:
long timeInANumber = calendar.getTimeInMillis();
You need to use a long instead of a integer, because this number is so big. A long is like a integer, but it can store greater values.
The number you get (called UNIX Timestamp) represents the count of milliseconds since 00:00 Janurary 1st, 1970.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I'm looking for the module ChronoUnit
java.time.temporal.ChronoUnit
implementation in Python, found in Java 8.
The reason this modules is useful, is because it contains a procedure that computes the Days, Months, Years etc.. between to arbitrary dates.
PS: Implementing the date computation in python, can result in a lot of problems as there are a lot of corner cases that I simply have no time consider at the moment, so please be constructive while answering.
Edit: I think my question is not clear enough, but what I'm trying to accomplish is to be able to actually substract one date from another and as a result to get the months, days, years etc.. between the two.
As per juanpa.arrivillaga comment the arrow library provides a useful method that provides a near similar function, I think that I'll answer my own question now.
Thanks to #junpa.arrivillaga I figured out that the procedure might be easily implemented in python thanks to the arrow library.
The api to be used it the following:
arrow.Arrow.range('hour', start, end)
The final method in Python would be:
'''
Computes day, month, year s
between two dates.
frame - time frame
start - date start
end - date finish
This method only works if the generated timeframe between to dates
is finite, as Arrow.range returns a generator!
'''
def count_frame_between_dates(frame, start, end):
return len(list(arrow.Arrow.range(frame, start, end)))
Edit: Arrow.range returns a generator, in theory you can't compute the length of a generator, but if you are sure the generator you're using is returning a finite set of elements then you can convert this generato into a list then use len() to compute it's length.
Thanks to everyone.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I store some duration values in properties, such as "period: 30s" or "period: 15m" and then convert it to milliseconds in my method.
Are there any Java built-in mappers from such format to millis ?
For example:
long millis = mapToMillis("15s") // millis variable should be 15000 here
long milllis2 = mapToMillis("1m") // millis2 variable shoul be 60000 here
I looked for such mappers in Duration class but none of them match my problem.
Duration.parse() does offer this feature. The how-to is mentioned in the documentation. You just need to store the values in the format mentioned there.
Example:
System.out.println(
Duration.parse("PT15S").toMillis()
);
prints 15000.
If you need to deal with dates instead of time, you can use the equivalent parse method in Period class.
If you are sure that your duration values are time-based amounts (i.e. seconds, minutes, days, ...), you can use the Duration type.
With the method
private static long mapToMillis(String duration) {
return Duration.parse("PT" + duration).toMillis();
}
the code snippet
long millis = mapToMillis("15s"); // millis variable should be 15000 here
long milllis2 = mapToMillis("1m"); // millis2 variable shoul be 60000 here
System.out.println(millis);
System.out.println(milllis2);
prints out
15000
60000
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Why does org.apache.commons.lang.time.DateUtils have in its source code a limitation on the number of years?
if (val.get(Calendar.YEAR) > 280000000) {
throw new ArithmeticException("Calendar value too large for accurate calculations");
}
I wanted to know, why exactly 280 millions, not for exmaple 285. I guessed already, that we won't have java anymore. Also I know, that Long can hold 292278994 years in milliseconds.
Well, I'd have to guess but this is probably related to: When will the java date collapse?
Since year 280,000,000 is very close to the maximum of 292,278,994 there might be any anticipated accuracy problems, hence the message says
Calendar value too large for accurate calculations.
Btw, we now know when the universe is going to end: Sun Aug 17 07:12:55 GMT 292278994 - ;)
As indicated by Thomas there are woes due to date being stored in 64 bits. This guard was introduced in 2004. See the test case for details and also bug LANG-24.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I was wondering is there a kind of algorithm or pattern which allows you to compare and find similar words
It will be easier if I use example, here it is:
Supposing that we have a strig:
String keywords = "Mummy's girl";
ArrayList = "Mom, cat, dog, girlfriend, house, mum, girls, fire";
I want to get in result those words (cause they're similar or the same in writing) = "Mom, girlfriend, mum, girls, girl"
Your question is little bit unclear. But in java you can use substring function.
String n = in.next();
String a = n.substring(0,3);
Here, a = Mum . Then go through all elements in the arraylist and find the similar word. In substring 0 is starting point and 3 is ending point.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to format Date object in Android similar like Gmail displays it
1. If the date is <24h from now, print hour ie 12:12am
2. If the date >24h but <1 week, print day name, ie Wednesday
3. If the date > 1 week, print date ie 2013-08-12
Thanks
Here's a good place to get started:
DateUtils.getRelativeTimeSpanString (long time, long now, long minResolution)
Parameters
time is the time to describe, in milliseconds.
now is the current time in milliseconds.
minResolution is the minimum timespan to report.
For example, a time 3 seconds in the past will be reported as "0 minutes ago" if this is set to MINUTE_IN_MILLIS. Pass one of 0, MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS, WEEK_IN_MILLIS
http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString(long,%20long,%20long)