Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a java program which makes a JNI call. From java, I am sending Date.getTime() (basically seconds * 1000), and I expect C++ to convert it to a perfect date.
NOT DUPLICATE: I KNOW MANY SOLUTIONS EXIST ON STACKOVERFLOW, BUT NONE OF THEM SEEM TO WORK WITH DATES SINCE 01-01-0001
I need the dates to get perfectly generated, for as early as 01-01-0001 00:00:00.
Any clue or code snippet in C++?
This assumes that getTime() works according to the java docs outputting time since 1970.
Try this:
How can I convert seconds since the epoch to hours/minutes/seconds in Java?
(Edit: even though the question is for Java the implementation given in the first answer is in C, which is (mostly) valid C++ code. It doesn't account for leap seconds so just subtract the number of leap seconds since 1970 before evaluating)
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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Pardon me if it's not the right place to post, but I'm so curious about this.
So December 5th is a date. Last year it fell on a Tuesday.
How can modulo be used to work this out?
I know this is a modulo problem, but just don't get how modulo is used here.
A famous algorithm for calculating the day of the week based on the date is Zeller's algorithm. If you follow that link to wikipedia, you will see the algorithm, which uses the modulus operator.
It is used to calculate the year of the century (denoted K) by doing
current year % 100
It's also used at the end of the expression, where we calculate modulo 7 of the formula shown on wikipedia:
https://wikimedia.org/api/rest_v1/media/math/render/svg/0f95195dcc0d98b351294277071736e97053324e
With a little intuition, you will realize 7 is used because there are 7 days in the week.
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 needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I use j2objc to translate a java util Date to a objective-c JavaUtilDate class.
I imported #import "java/util/Date.h"to store a translated java Date variable.
var myDate: JavaUtilDate
How do I convert a JavaUtilDate into an NSDate?
Depending on where/how you get the Java Date, your best bet would be to get the milliseconds and instantiate the NSDate with it.
So call the getTime() method on the Java Date to get milliseconds since Epoch, then create your NSDate with the dateWithTimeIntervalSince1970: method. The NSDate method takes seconds (as a floating point), so divide by 1000.0 to keep the precision (thanks to Martin R for pointing this out in comments). :)
Java Util Date method
NSDate method
There seems to be some confusion on what exactly is being asked. To be as general as possible, time objects typically have a method to get the milliseconds since epoch and a constructor (or setter) to pass in the seconds since epoch. So all you have to do is get the seconds from one object and instantiate the other object with the seconds.
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)