Search code examples
javajython

Jython Date to String conversion


How to print a date object 2017-09-09 as a string '2017-09-09' in jython?

I need to set the value [ 2017-09-09 + string ] as a string in Jython. I tried the following :

  1. str(2017-09-09)
  2. print ("'" + 2017-09-09 + "'")

But it gives error. Example :

package com.myexample;

public class SomeClassInJAVA(){ 
    public static Date getDate(){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse("1972-09-09");
        return d;
    }
}

//Jython Script

from com.myexample import SomeClassInJAVA
d = SomeClassInJAVA.getDate() 
print d  //prints d as 1972-09-09
print str(d) //returns error
print "'" + d + "'"

Assuming that I convert the recived date back to Java string in jython

s = SimpleDateFormat("yyyy-MM-dd").format(d)
print s // prints s as 1972-09-09

How do I convert it to '1972-09-09'

Can someone please help? Thanks in advance


Solution

  • Based on the comments, your conversion needs to happen after you get the JythonDate, not before:

    myStr = "some string" + str(jythonDate);