Sunday, September 27, 2015

Jython Date and time formats experiment

Basic date and time formats in Jython

This is common requirement in most of our WAS administration tasks. You need the date or time formats in the following tasks:

  1. Back of the deployment units such as .war or .ear or .jar files
  2. Log file rotations
  3. Monitoring report logs with time stamps
  4. Update the Logger with time stamp
Jython allow us to use time object only from Python module. other two date, datetime objects are not available in Jython. Lets experiments with the time object and its different functions in a simple script:

import time
timetuple=time.localtime()
print "Time tuple:", timetuple
print "Time tuple with loop"
for t in timetuple:
 print t

 # Week of the day
print "Today Weekday in short:",time.strftime('%a')
print "Today Weekday in full:", time.strftime('%A')

# Month  of the date
print "This month in short:", time.strftime('%b')
print "This month in full:", time.strftime('%B')
print "Month as a number", time.strftime('%m')

# Complete date
print "Current date and time : ", time.strftime('%c')

# Time formats
print "Hours in 24 hr clock:", time.strftime('%H') 
print "Hours in 12 hr clock:", time.strftime('%I') 
print "Minute of current time", time.strftime('%M')
print "Seconds in current time", time.strftime('%S')
print "AM/PM Now:", time.strftime('%p')

#  Day formats
print "Day of the Date", time.strftime('%d')
print "Day of the year", time.strftime('%j')


# Week  in numbers
print "Week number of Year:", time.strftime('%U')
print "Weekday as number :",time.strftime('%w')
print "Week number of Year:", time.strftime('%W')
print "Date with regular form:",time.strftime('%x')

print "Time in regular form:",time.strftime('%X')

# Year formats
print "Year in 2 decimals",time.strftime('%y')
print "Year in 4 decimals",time.strftime('%Y')

print "Current Time Zone:", time.strftime('%Z')

Execution of Date and time formats in wsadmin



If you are limited with time object and its functions then you can use Java objects!


wsadmin>execfile('F:/pavanbsd/Jython/DateTimeExperiments.py')
Time tuple: (2015, 9, 27, 19, 27, 47, 6, 270, 0)
Time tuple with loop
2015
9
27
19
27
47
6
270
0
Today Weekday in short: Sun
Today Weekday in full: Sunday
This month in short: Sep
This month in full: September
Month as a number 09
Current date and time :  Sun Sep 27 19:27:47 2015
Hours in 24 hr clock: 19
Hours in 12 hr clock: 07
Minute of current time 27
Seconds in current time 47
AM/PM Now: PM
Day of the Date 27
Day of the year 270
Week number of Year: 39
Weekday as number : 00
Week number of Year: 38
Date with regular form: 09/27/15
Time in regular form: 19:27:47
Year in 2 decimals 15
Year in 4 decimals 2015
Current Time Zone: IST


Exercises: The task might be included like this, a web application bankcustomer.war file backup with appending of today date it should look like bankcustomer.war27092015.
Log file for script trace as was_deploy.log270920152025 [filename+ddmmyyyyhhmm]

References:


  1. Date and Time formats link for Jython

No comments:

Post a Comment

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow