February 19, 2012

Formatting and converting data in Java

This entry is part of the Maximo Java Development series.

If you need to format dates and numbers into a locale-sensitive string or if you just need to convert data between different types then take a look at psdi.util.MaxType class.
For example, the following piece of code shows how to correctly format a currency amount into a string.
MaxType mt = new MaxType(MaxType.AMOUNT);
mt.setValue(10.1234);
String s = mt.asLocaleString();

5 comments:

  1. Hi,

    thanks for this article. It points me in the right direction, however as Java dummy I have big problem with converting a date.
    I need to copy DATETIME field into DATE field. I tried setValue("DATEFIELD", getDate("DATETIMEFIELD")); but this fails with "Date format invalid" whenever locale is set to anything else than "us". How to make it work? Can you support me?

    ReplyDelete
    Replies
    1. Interesting...
      I have tried this code and it works:
      Date d = asset.getDate("STATUSDATE");
      asset.setValue("INSTALLDATE", d);

      Where STATUSDATE is a DATETIME and INSTALLDATE is a DATE.
      Where are you setting the locale?

      Delete
  2. Hi Bruno,

    thanks for the prompt answer!
    Now it works! It seems that the cast made a huge difference.
    Thanks a lot!

    Robert

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Hi Bruno,

    Also, you can use following code:
    String s = psdi.util.MXFormat.amountToString(10.1234);

    Kind Regards,
    Max.

    ReplyDelete