Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 

Re: Question about units package...

Steve and Bob,

I said:
> > > Digging around in the various examples and docs, I have not been able
> > > to figure out how to do the equivalent using the Java units package.
> > > Can somebody please provide me with a pointer a specific example or
> > > tutorial that covers this?

> > Steve said: 
> > Regretably, the VisAD units subsystem does not yet contain an equivalent
> > of the "valtocal" function.  At the moment, the best advice I can give
> > is to use the standard Java date and time-handling classes to do what
> > you want (e.g. the "Data" and "Calendar" classes in the "java.util"
> > package).
> > > Bob said:
> > > i haven't contributed too many answers to the VisAD mailing list. 
> > > But i do know how to use a (simple) timer in Java.  Here's a 
> > > simple timer class:

> > > public class QuestionTimer {

Thanks to Steve for the suggestions and Bob for the sample code.  Once I looked
into it deeper using the Date and Calendar classes along with the
SimpleDateFormat class worked out quite well.

In case anybody else struggling with this, the quick and dirty code I hacked up
is below.

Roland

(No doubt I should use the units package to get conversion factors I need.  That
will be for the Version 1.1)
-- 
Roland Schweitzer
NOAA-CIRES Climate Diagnostics Center           325 Broadway
NOAA/ERL/CDC - (R/CDC1)                         Boulder, CO 80303
.... (303) 497-6249 .... (303) 497-7013 FAX .... rhs@xxxxxxxxxxxx ....

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
import java.util.*;
import java.text.*;

/**
 * Classes to convert COARDS times to calendar times.
 *
 * @author R.H. Schweitzer
 * @version 1.0, 12/2000
 */
public class ConvertTime
{

  //instance variables
  long HoursFactor = 3600000;
  long HoursPerDay = 24;

  public Calendar doubleToCalendar(String Units, double value)
  {

      Calendar theCalendar;

      long millis;

      theCalendar = Calendar.getInstance();
      theCalendar.setTimeZone(TimeZone.getTimeZone("UTC"));


      if ( Units.toLowerCase().startsWith("hours since") ) {
         millis = (long) value * HoursFactor;
      }
      if ( Units.toLowerCase().startsWith("days since") ) {
         millis = (long) value * HoursFactor * HoursPerDay;
      }
      else {
         millis = 0;
      }

      SimpleDateFormat formatter
      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",new Locale("en","US"));

      formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

      try {

         Date origin
         formatter.parse(Units.substring(Units.indexOf("since ")+6));

         millis = millis + origin.getTime();

         theCalendar.setTime(new Date(millis));

      }
      catch (Exception e) {

         System.out.println("Could not parse time unit. "+e.toString());

      }

      return theCalendar;
  }
}


 
 
  Contact Us     Site Map     Search     Terms and Conditions     Privacy Policy     Participation Policy
 
National Science Foundation (NSF) UCAR Community Programs   Unidata is a member of the UCAR Community Programs, is managed by the University Corporation for Atmospheric Research, and is sponsored by the National Science Foundation.
P.O. Box 3000     Boulder, CO 80307-3000 USA     Tel: 303-497-8643     Fax: 303-497-8690