New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of java.time.LocalDate #105

Closed
suriyanto opened this Issue Jun 4, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@suriyanto

suriyanto commented Jun 4, 2018

I have a Java class that I need to initialize from Jython. This class is using java.time.LocalDate as one of the constructor argument. What Jython/Python data type should I use to match the LocalDate in Java?

@jamesmudd

This comment has been minimized.

Contributor

jamesmudd commented Jun 25, 2018

I think you hoping there is a Python type that can be automatically coerced to Java LocalDate. This is not the case. However you can easily make a LocalDate instance then pass that to your constructor an example is:

Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43)
[Java HotSpot(TM) 64-Bit Server VM ("Oracle Corporation")] on java10.0.1
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.time import LocalDate
>>> ld = LocalDate.now() # If you want today
>>> ld
2018-06-25
>>> ld2 = LocalDate.parse('2020-02-02') # If you want a specific date from a string
>>> ld2
2020-02-02
>>> my_obj = MyClass(ld)

Hopefully that will allow your code to work.

@suriyanto

This comment has been minimized.

suriyanto commented Jun 26, 2018

Thanks. Considering that I want the scripter to be able to have a more native python experience and able to use python datetime.date class, which Java Date datatype is more suitable to be used as data to be passed to python side from Java?

@jamesmudd

This comment has been minimized.

Contributor

jamesmudd commented Jun 27, 2018

The Jython datetime.date class supports automatic coercion into java.util.Calendar or java.sql.Timestamp So if you make an additional constructor which takes one of those then it should be able to work with python datetime.date. Note: I didn't try it!

Then your new constructor would need to call the existing LocalDate one something like this:

public class MyClass {
    public MyClass(LocalDate ld) {
        // Existing LocalDate constructor
        // Does stuff here ...
    }
    
    // Only to help out Jython
    public MyClass(Calendar c) {
        // Convert Calander to LocalDate and call other constructor
       this(LocalDateTime.ofInstant(c.toInstant(), c.getTimeZone().toZoneId()).toLocalDate());
    }
}

But I do think this is messy. Jython should support automatic coercion into the newer LocalDateTime classes. Here is the Jython bug for this issue 2695

@jamesmudd

This comment has been minimized.

Contributor

jamesmudd commented Nov 8, 2018

Closing for now the issue is resolved and there is an open bug for an improvement, which might need to wait until Java 7 support is dropped.

@jamesmudd jamesmudd closed this Nov 8, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment