Page 1 of 1

convertJavaUtilDateToDateTime, convertDateTimeToJavaUtilDate

Posted: Sat Nov 23, 2013 11:54 am
by joleenf
These functions (convertJavaUtilDateToDateTime, convertDateTimeToJavaUtilDate) convert between the two different date object classes in McIDAS-V. If dates are retrieved from the filename, they are generally returned as a java.util.Date. When returned from getADDEImage, and some other visad classes, they will be returned as a visad.DateTime object. This may seem like more date classes than necessary, but remember, classes are easily checked in the jython shell.

For example:
1) date is retrieved from metadata of ADDE Image.
2) date is placed in variable imageDate
3) confirm type by using type()

Code: Select all

# **params not shown here see http://www.ssec.wisc.edu/mcidas/doc/mcv_guide/working/misc/Scripting/CreateObject.html for getADDEImage documentation
metaData, image = getADDEImage(**params)   
imageDate = metaData['nominal-time']
type(imageDate)


In code below, type is checked with the python if isinstance(): statement.

Both java.Util.Date and visad.DateTime have methods which provide simple date format conversions to customize the output date string, these are useful in creating nice labels. I prefer the visad.DateTime class conversion methods. viewtopic.php?f=26&t=1323

Both classes allow date subtraction, but only java.Util.Date provides method checks of whether a date is before or after another. (date1.before(date2), date1.after(date2), date1.compareTo(date2)).

Code: Select all

# Classes can be imported once:  place in user library outside of function definitions and top so that they are first to compile.

import time
import visad.DateTime
import java.util.Date

#===============================================================================
def convertJavaUtilDateToDateTime(inputDateObject):
   
   """
     Purpose:
        Convert a java.util.Date to a visad.DateTime object for compatibility with
        date objects returned from some McIDAS-V functions.  In addition, visad.DateTime
        offers quick date formatting for image labeling.  See:  "Formatting a Date"
        http://mcidas.ssec.wisc.edu/forums/viewtopic.php?f=26&t=1323&sid=156052736936aed24b1e9186292f942d
       
        Prior to McIDAS-V1.4, getADDEImage returned java.util.Date objects in
        the image metadata.  This provides a tool to make scripts backwards compatible.
         - If a dateTime object is entered, the dateTime object is returned
         - If a string is entered, this code raises an exception. 
       
     Usage:
        myDate = convertJavaUtilDateToDateTime(inputDateObject)
     
     Input:
        inputDateObject - a java.util.Date object
     Return:
        visad.DateTime object
           
     Uses the following classes.
     - time
     - visad.DateTime
     - java.util.Date
   """

   if isinstance(inputDateObject, java.util.Date):
     newDateTimeObject = DateTime(inputDateObject)
     time.sleep(3)
   elif isinstance(inputDateObject, DateTime):
     newDateTimeObject = inputDateObject
   else:
     myException = 'TypeError: The inputDateObject is the type: "' +str(type(inputDateObject))+ '." This is not a java.util.Date or visad.DateTime'
     raise(myException)

   return newDateTimeObject
#===============================================================================
def convertDateTimeToJavaUtilDate(inputDateTimeObject):
   """
     Purpose:
        Convert a visad.DateTime to a java.Util.Date.
       
         - If a java.Util.Date object is entered, the java.Util.Date object is returned
         - If a string is entered, this code raises an exception. 
       
     Usage:
        myDate = convertDateTimeToJavaUtilDate(inputDateTimeObject)
     
     Input:
        inputDateTimeObject - a visad.DateTime object
     Return:
        java.Util.Date
           
     Uses the following classes.
     - visad.DateTime
     - java.util.Date
   """

   if (isinstance(inputDateTimeObject, DateTime)):
      timeZone = inputDateTimeObject.getFormatTimeZone()
      dateTimeFormat = inputDateTimeObject.getFormatPattern()

      sdf = SimpleDateFormat(dateTimeFormat)
      sdf.setTimeZone(timeZone)

      myTimeString = inputDateTimeObject.toValueString()
      convertedDateObject = sdf.parse(myTimeString)
   elif (isinstance(inputDateTimeObject, java.util.Date)):
      convertedDateObject = inputDateTimeObject
   else:
     myException = 'TypeError: The input object is type: "' +str(type(inputDateTimeObject))+ '." This is not a java.util.Date or visad.DateTime'
     raise(myException)

   return convertedDateObject
#==============================================================================


dateObjectConversion.py
(3.09 KiB) Downloaded 631 times


Joleen