# Java

**URL:** <https://forum.inductiveautomation.com/t/java/250>\
**Category:** Archived Topics\
**Created:** [March 26, 2007, 5:56pm UTC](https://forum.inductiveautomation.com/t/java/250 "2007-03-26T17:56:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamal\_Lang](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Kamal\_Lang](https://forum.inductiveautomation.com/u/Kamal_Lang)\
**Post date:** [March 26, 2007, 5:56pm UTC](https://forum.inductiveautomation.com/t/java/250/1 "2007-03-26T17:56:04Z")

</div>

I have used bits of java code in the form of 1 or 2 method calls like

```auto
from java.net import InetAddress 
compname = InetAddress.getLocalHost().getHostName() 
```

But what if i want to instantiate a standard Java class and use the new method, declare some variables and so on. Where can i insert it and how do i indicate to the PMI script interpreter that the code is Java.

Can you help me on this

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 26, 2007, 6:44pm UTC](https://forum.inductiveautomation.com/t/java/250/2 "2007-03-26T18:44:12Z")

</div>

Do you mean you want to define a new, custom class? You’d declare a new class in Python, not Java. If this isn’t what you meant, please clarify.

---

<div class="post-metadata">

**Author:** ![Kamal\_Lang](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Kamal\_Lang](https://forum.inductiveautomation.com/u/Kamal_Lang)\
**Post date:** [March 26, 2007, 7:42pm UTC](https://forum.inductiveautomation.com/t/java/250/3 "2007-03-26T19:42:13Z")

</div>

Where do i declare this new python class and how do i call it.

I am asking how to use java inside PMI because i saw in the help using Java code when pmi’s own functions fall short

I would for example like to insert a few lines of Java code like the bit below. Where do i insert it and can i use it as is or do i have to modify it?  
(The code below is just cut out of another context)

```auto
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

...

SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
TimeZone local = TimeZone.getDefault();
sdf.setTimeZone( local );

// new Date() gets current date/elapsedTime
String dateString = sdf.format( new Date() );

// Get TimeZone
String timeZoneName = local.getDisplayName();
```

Here the functions Date() and TimeZone.getDefault() are used.

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 26, 2007, 8:10pm UTC](https://forum.inductiveautomation.com/t/java/250/4 "2007-03-26T20:10:07Z")

</div>

Oh, I think I misunderstood. You simply want to use existing classes from the Java standard library?

This is possible because the Python engine that we use is Jython, which has the ability to use Java classes. See more here: [jython.org/docs/usejava.html](http://www.jython.org/docs/usejava.html)

The code you posted translated to Jython would become:

[code]  
from java.text import SimpleDateFormat  
from java.util import Date  
from java.util import TimeZone  
sdf = SimpleDateFormat(“yyyy-MM-dd”)  
local = TimeZone.getDefault()  
sdf.setTimeZone(local)

dateString = sdf.format(Date())

timeZoneName = local.getDisplayName()[/code]

Note that for date formatting you could also just call fpmi.db.dateFormat()

Hope this helps,

---

<div class="post-metadata">

**Author:** ![Kamal\_Lang](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Kamal\_Lang](https://forum.inductiveautomation.com/u/Kamal_Lang)\
**Post date:** [March 26, 2007, 10:31pm UTC](https://forum.inductiveautomation.com/t/java/250/5 "2007-03-26T22:31:14Z")

</div>

Hi  
Just what i needed.  
Thanks again

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [March 26, 2007, 10:41pm UTC](https://forum.inductiveautomation.com/t/java/250/6 "2007-03-26T22:41:23Z")

</div>

No problem!
