Convert JavaUtilDate to NSDate and vice versa? [closed] - java

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.

Related

Python compute timeframe length between two dates [closed]

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.

Java Time format return as time object [duplicate]

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
If I do:
ZonedDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE)
It returns a String, rather I'd like it to return a ZonedDateTime object. Is there a clean way of doing that. This is the obvious solution:
ZonedDateTime.parse(ZonedDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE));
Is there an easier way to do that? That just seems a little too verbose.
just use ZonedDateTime.now()
source: https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#now--

MilliSec To Date C++ [closed]

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)

multiple timestamps in one program execution [closed]

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 8 years ago.
Improve this question
Need help!
I am not able to get multiple timestamps in one execution,
basically i am running a test suite , and i am using ordernumbers as the current timestamp in hhmmss,
But when first time timestamp is taken, it has the same value throughout the execution, i want changing current values, during the execution.
code used :
static DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
static Date date = new Date();
public static String ordernum = dateFormat.format(date);
You're always reusing the same Date object. You need to create a new Date() every time you need a new timestamp.
Update
Took another look at your question: using a second-precision timestamp is probably not the best guarantee for uniqueness, especially in test execution.
Why not do:
String ordernum = java.util.UUID.randomUUID().toString();
Or:
String ordernum = "" + System.nanoTime();

Rescue long Date data that has been stored as Integer [closed]

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 8 years ago.
Improve this question
I have been storing date records (Date().getTime()) in an SQLite database with an Integer type. I realize now that Date().getTime() is returning Long values, not Integer.
Is there any way I can rescue the date data that is already stored in the database? Going forward I can reduce the resolution of the time, to make it fit into an integer. (ie. divide by 1000, and cast to int)
I expect that forcing a long value into an integer space has truncated the most significant digits - which might work in my favour, as the dates in question have all occurred within the past 6 months, so can probably be calculated.
question
so, how exactly would the long representation of today's date map onto an integer, and how might I use that knowledge (combined with the restricted time range) to build these integers back into their original long values?
Any suggestions?
Turns out the SQLite database integer type is perfectly capable of storing long values. So the data rescue was unnecessary in the end.

Categories

Resources