Example: TimeSeries

This example illustrates setting up a different time zone.


import com.imsl.stat.TimeSeries;
import java.text.*;
import java.util.*;

public class TimeSeriesEx2 {

    public static void main(String args[]) throws ParseException {
        String dateStrings[] = {
            "11/23/2011 11:13:27", "9/14/2011 13:15:10", "7/28/2012 20:18:32",
            "8/7/2012 00:00:16", "6/3/2011 1:21:03", "9/14/2011 17:18:22"
        };
        double data[] = {-1.0, 2.5, 6.773, -8.92, 4.117, 16.27};
        Date dates[] = new Date[data.length];
        SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/y H:mm:ss");
        SimpleDateFormat printDateFormat
                = new SimpleDateFormat("M/d/y H:mm:ss, a");

        for (int i = 0; i < data.length; i++) {
            dates[i] = dateFormat.parse(dateStrings[i]);
        }

        TimeSeries ts;
        ts = new TimeSeries();

        ts.setSeriesValues(data);
        ts.setDates(dates);

        System.out.println("Local Timezone offset in hours is "
                + ts.getTimeZoneOffset());
        System.out.println("Local Timezone name is " + ts.getTimeZone().getID());

        /* Note: changing the time zone does not change the time values that 
         were set in setValues().  Subtract the offset in order to adjust the 
         times, if necessary.
         */
        ts.setTimeZone(-8, "PST");
        TimeZone tz = ts.getTimeZone();
        System.out.println("New offset is " + ts.getTimeZoneOffset());
        System.out.println("New name is " + tz.getID());


        /* Default printing will use the local time zone.  Here is the manual 
         way to print the time zone that was set. */
        System.out.println(printDateFormat.format(ts.getDates()[4]) + " "
                + tz.getDisplayName());

        /* Use the offset to display the equivalent GMT time */
        Date gmtTime = new Date(ts.getDates()[4].getTime()
                - ts.getTimeZoneOffset() * 60 * 60 * 1000);
        System.out.println(printDateFormat.format(gmtTime) + " GMT ");
    }
}

Output

Local Timezone offset in hours is -6
Local Timezone name is America/Chicago
New offset is -8
New name is PST
7/28/2012 20:18:32, PM Pacific Standard Time
7/29/2012 4:18:32, AM GMT 
Link to Java source.