In this
topic, we are going to see how to manipulate Maximo dates using Automation scripts.
The different kinds of manipulation are
- Adding Number to the date
- Subtracting Number to the date
- Changing the format of the date
- Difference between two dates in days
In the below examples we are going to use Python scripts in Maximo
- Object Launch Point is used
- Asset object
- The “INSTALLDATE” attribute is modified
Java Calendar class is used for Date Manipulation.
1. Adding Number to the date
Current date from MXServer is got and adding number (10) to that date.
print("**************Start of C_TEST ********************") from java.util import Date from java.util import Calendar from psdi.server import MXServer currentDate = MXServer.getMXServer().getDate() cal = Calendar.getInstance() cal.setTime(currentDate) cal.add(Calendar.DATE, 10) print ("After Additional to Current Date ---> ",cal.getTime()) mbo.setValue("INSTALLDATE",cal.getTime()) print("**************End of C_TEST ********************")
2. Subtracting Number to the date
Current date from MXServer is got and subtracting number (10) to that date.
print("**************Start of C_TEST ********************") from java.util import Date from java.util import Calendar from psdi.server import MXServer currentDate = MXServer.getMXServer().getDate() cal = Calendar.getInstance() cal.setTime(currentDate) cal.add(Calendar.DATE, -10) print ("After Additional to Current Date --->",cal.getTime()) mbo.setValue("INSTALLDATE",cal.getTime()) print("**************End of C_TEST ********************")
3. Change the Date Format
To convert
the date format,
print("**************Start of C_TEST ********************") from java.util import Date from java.text import SimpleDateFormat installdate = mbo.getDate("INSTALLDATE") sdf = SimpleDateFormat("yyyyMMdd") installdate_v1 = sdf.format(installdate) print("installdate_v1 String-------> ",str(installdate_v1)) print("**************End of C_TEST ********************")
Output:
**************Start of C_TEST ******************** installdate_v1 String-------> 20200226 **************End of C_TEST ********************
4. Find the difference between Dates in Days-Method 1
print("**************Start of C_TEST ********************") from java.util import Date from psdi.server import MXServer from java.util import Calendar currentDate = MXServer.getMXServer().getDate() print ("Current Date ",currentDate) print ("Current Date milliseconds ",currentDate.getTime()) installationDate = mbo.getDate("INSTALLDATE") print ("Installation Date ",installationDate) print ("Installation Date milliseconds ",installationDate.getTime()) difference_1 = ((currentDate.getTime()- installationDate.getTime())/86400000) print ("Difference between dates in seconds---> ",difference_1) print("**************End of C_TEST ********************")
Output:
**************Start of C_TEST ******************** Current Date Mon Mar 08 11:23:59 IST 2021 Current Date milliseconds 1615182839222L Installation Date Fri Feb 26 00:00:00 IST 2021 Installation Date milliseconds 1614277800000L Difference between dates in seconds---> 10L **************End of C_TEST ********************
5. Find the difference between Dates in Days-Method 2
print("**************Start of C_TEST ********************") from java.util import Date from java.util import Calendar from java.util.concurrent import TimeUnit from java.text import SimpleDateFormat from psdi.server import MXServer from java.lang import Math currentDate = MXServer.getMXServer().getDate() print ("Current Date ",currentDate) installationDate = mbo.getDate("INSTALLDATE") print ("Installation Date ",installationDate) diffInMillies = Math.abs(currentDate.getTime() - installationDate.getTime()) diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS) print ("The date difference in Days---> ",diff) print("**************End of C_TEST ********************")
Output:
**************Start of C_TEST ******************** Current Date Mon Mar 08 11:41:04 IST 2021 Installation Date Fri Feb 26 00:00:00 IST 2021 The date difference in Days---> 10L **************End of C_TEST ********************
Wonderful scripts thanks Keshav bro
ReplyDelete