Getting the Right Date

I recently ran into an issue where I was using the Jython function datetime.now() to populate a status date in Maximo.

For example, I used the following code to get the current date/time to populate a status date (doing a status change via code):

import datetime
 
statusDate = datetime.datetime.now()
...

However, it generated the error “You cannot set a status date before the last status date.” It had me scratching my head as how can the current date/time be in the future? After some debugging, I realized the problem. When you use the Jython date/time object, you are getting the date-time of the server it is running on. However, the user was in a different timezone and thus the server’s date/time was in the future. As such, it is not a good idea to use this method to get the current date/time.

To make sure you get the current date/time in the user’s timezone, you need to use the MXServer object to get the date. Something like this:

statusDate = MXServer.getMXServer().getDate()
...

This will return the date/time based on the user’s timezone and not the time on the server hosting Maximo.