Hem
Useful code snippets PDF Skriv ut Skicka sidan
Tisdag, 22 Februari 2011 08:45

How to convert a java date type to an XML date used in JAXB bindings 

The variable result is a java type generated by an XSD (XML-schema) containing a dateTime. Below snippet is an example how to set the attribute.

      GregorianCalendar cal = new GregorianCalendar();
      XMLGregorianCalendar date2;
      if (product.getCreated()!=null) {
              cal.setTime(new java.sql.Date(product.getCreated().getTime()));
              date2 = javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
              result.setCreated(date2);
      }

How to create an instance of an arbitrary class name

Class pluginClass = Class.forName(MyClass.getClassname());
Constructor constructor = pluginClass.getConstructor(TypeOfParam1.class, TypeOfParam2.class);
MyClass plugin = (MyClass)constructor.newInstance(new Object[]{param1, param2});

Convert upsize_ts to java.sql.Timestamp

upsizeTs = rs.getBytes("upsize_ts");
// Convert
java.io.DataInputStream in = new java.io.DataInputStream(new java.io.ByteArrayInputStream(upsizeTs));
try {
   long ts = in.readLong();
   lastChanged = new java.sql.Timestamp((ts+322477200)*1000);
} catch (IOException e) {
   e.printStackTrace();
}

 

LAST_UPDATED2