Example usage for java.util TimeZone setDefault

List of usage examples for java.util TimeZone setDefault

Introduction

In this page you can find the example usage for java.util TimeZone setDefault.

Prototype

public static void setDefault(TimeZone zone) 

Source Link

Document

Sets the TimeZone that is returned by the getDefault method.

Usage

From source file:com.alibaba.wasp.jdbc.TestPreparedStatement.java

public void testDateTimeTimestampWithCalendar() throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("create table ts(x timestamp primary key)");
    stat.execute("create table t(x time primary key)");
    stat.execute("create table d(x date)");
    Calendar utcCalendar = new GregorianCalendar(new SimpleTimeZone(0, "Z"));
    TimeZone old = TimeZone.getDefault();
    DateTimeUtils.resetCalendar();//from  w ww.  ja v  a 2s.co m
    TimeZone.setDefault(TimeZone.getTimeZone("PST"));
    try {
        Timestamp ts1 = Timestamp.valueOf("2010-03-13 18:15:00");
        Time t1 = new Time(ts1.getTime());
        Date d1 = new Date(ts1.getTime());
        // when converted to UTC, this is 03:15, which doesn't actually exist
        // because of summer time change at that day
        Timestamp ts2 = Timestamp.valueOf("2010-03-13 19:15:00");
        Time t2 = new Time(ts2.getTime());
        Date d2 = new Date(ts2.getTime());
        PreparedStatement prep;
        ResultSet rs;
        prep = conn.prepareStatement("insert into ts values(?)");
        prep.setTimestamp(1, ts1, utcCalendar);
        prep.execute();
        prep.setTimestamp(1, ts2, utcCalendar);
        prep.execute();
        prep = conn.prepareStatement("insert into t values(?)");
        prep.setTime(1, t1, utcCalendar);
        prep.execute();
        prep.setTime(1, t2, utcCalendar);
        prep.execute();
        prep = conn.prepareStatement("insert into d values(?)");
        prep.setDate(1, d1, utcCalendar);
        prep.execute();
        prep.setDate(1, d2, utcCalendar);
        prep.execute();
        rs = stat.executeQuery("select * from ts order by x");
        rs.next();
        assertEquals("2010-03-14 02:15:00.0", rs.getString(1));
        assertEquals("2010-03-13 18:15:00.0", rs.getTimestamp(1, utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp(1).toString());
        assertEquals("2010-03-14 02:15:00.0", rs.getString("x"));
        assertEquals("2010-03-13 18:15:00.0", rs.getTimestamp("x", utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp("x").toString());
        rs.next();
        assertEquals("2010-03-14 03:15:00.0", rs.getString(1));
        assertEquals("2010-03-13 19:15:00.0", rs.getTimestamp(1, utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp(1).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getString("x"));
        assertEquals("2010-03-13 19:15:00.0", rs.getTimestamp("x", utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp("x").toString());
        rs = stat.executeQuery("select * from t order by x");
        rs.next();
        assertEquals("02:15:00", rs.getString(1));
        assertEquals("18:15:00", rs.getTime(1, utcCalendar).toString());
        assertEquals("02:15:00", rs.getTime(1).toString());
        assertEquals("02:15:00", rs.getString("x"));
        assertEquals("18:15:00", rs.getTime("x", utcCalendar).toString());
        assertEquals("02:15:00", rs.getTime("x").toString());
        rs.next();
        assertEquals("03:15:00", rs.getString(1));
        assertEquals("19:15:00", rs.getTime(1, utcCalendar).toString());
        assertEquals("03:15:00", rs.getTime(1).toString());
        assertEquals("03:15:00", rs.getString("x"));
        assertEquals("19:15:00", rs.getTime("x", utcCalendar).toString());
        assertEquals("03:15:00", rs.getTime("x").toString());
        rs = stat.executeQuery("select * from d order by x");
        rs.next();
        assertEquals("2010-03-14", rs.getString(1));
        assertEquals("2010-03-13", rs.getDate(1, utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate(1).toString());
        assertEquals("2010-03-14", rs.getString("x"));
        assertEquals("2010-03-13", rs.getDate("x", utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate("x").toString());
        rs.next();
        assertEquals("2010-03-14", rs.getString(1));
        assertEquals("2010-03-13", rs.getDate(1, utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate(1).toString());
        assertEquals("2010-03-14", rs.getString("x"));
        assertEquals("2010-03-13", rs.getDate("x", utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate("x").toString());
    } finally {
        TimeZone.setDefault(old);
        DateTimeUtils.resetCalendar();
    }
    stat.execute("drop table ts");
    stat.execute("drop table t");
    stat.execute("drop table d");
}

From source file:org.jfree.data.time.MinuteTest.java

/**
 * Some checks for the getFirstMillisecond() method.
 *//*from ww w  .j av a2 s  .  c  om*/
@Test
public void testGetFirstMillisecond() {
    Locale saved = Locale.getDefault();
    Locale.setDefault(Locale.UK);
    TimeZone savedZone = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Minute m = new Minute(43, 15, 1, 4, 2006);
    assertEquals(1143902580000L, m.getFirstMillisecond());
    Locale.setDefault(saved);
    TimeZone.setDefault(savedZone);
}

From source file:org.jfree.data.time.SecondTest.java

/**
 * Some checks for the getFirstMillisecond() method.
 *//*  w  ww . j ava 2 s .c  o m*/
@Test
public void testGetFirstMillisecond() {
    Locale saved = Locale.getDefault();
    Locale.setDefault(Locale.UK);
    TimeZone savedZone = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Second s = new Second(15, 43, 15, 1, 4, 2006);
    assertEquals(1143902595000L, s.getFirstMillisecond());
    Locale.setDefault(saved);
    TimeZone.setDefault(savedZone);
}

From source file:com.serotonin.m2m2.Lifecycle.java

public synchronized void initialize(ClassLoader classLoader) {
    for (Module module : ModuleRegistry.getModules()) {
        module.preInitialize();/*  ww  w.ja  v  a2 s  .c o m*/
    }

    String tzId = Common.envProps.getString("timezone");
    if (!StringUtils.isEmpty(tzId)) {
        TimeZone tz = TimeZone.getTimeZone(tzId);
        if ((tz == null) || (!tz.getID().equals(tzId)))
            throw new RuntimeException("Time zone id '" + tzId + "' in env properties is not valid");
        this.LOG.info("Setting default time zone to " + tz.getID());
        TimeZone.setDefault(tz);
        DateTimeZone.setDefault(DateTimeZone.forID(tzId));
    }

    Common.timer.init(new ThreadPoolExecutor(0, 1000, 30L, TimeUnit.SECONDS, new SynchronousQueue()));

    Providers.add(TimerProvider.class, new TimerProvider() {
        public AbstractTimer getTimer() {
            return Common.timer;
        }
    });
    Common.JSON_CONTEXT.addResolver(new EventTypeResolver(), new Class[] { EventType.class });
    Common.JSON_CONTEXT.addResolver(new BaseChartRenderer.Resolver(), new Class[] { ChartRenderer.class });
    Common.JSON_CONTEXT.addResolver(new BaseTextRenderer.Resolver(), new Class[] { TextRenderer.class });
    Common.JSON_CONTEXT.addResolver(new EmailRecipientResolver(), new Class[] { EmailRecipient.class });

    Providers.add(InputStreamEPollProvider.class, new InputStreamEPollProviderImpl());
    Providers.add(ProcessEPollProvider.class, new ProcessEPollProviderImpl());

    //    lic();
    freemarkerInitialize();
    databaseInitialize(classLoader);

    for (Module module : ModuleRegistry.getModules()) {
        module.postDatabase();
    }
    utilitiesInitialize();
    eventManagerInitialize();
    runtimeManagerInitialize();
    maintenanceInitialize();
    imageSetInitialize();
    webServerInitialize(classLoader);

    for (Module module : ModuleRegistry.getModules()) {
        module.postInitialize();
    }

    SystemEventType.raiseEvent(new SystemEventType("SYSTEM_STARTUP"), System.currentTimeMillis(), false,
            new TranslatableMessage("event.system.startup"));

    for (Runnable task : this.STARTUP_TASKS)
        Common.timer.execute(task);
}

From source file:org.apache.sqoop.connector.jdbc.oracle.util.OracleQueries.java

public static void setConnectionTimeZone(Connection connection, String timeZone) {
    String timeZoneStr = timeZone;
    if (StringUtils.isEmpty(timeZoneStr)) {
        timeZoneStr = "GMT";
    }/*  www  .jav  a2 s .  c  om*/
    TimeZone timeZoneObj = TimeZone.getTimeZone(timeZoneStr);
    try {
        Method methSession = oracleConnectionClass.getMethod("setSessionTimeZone", String.class);
        Method methDefault = oracleConnectionClass.getMethod("setDefaultTimeZone", TimeZone.class);
        methSession.invoke(connection, timeZoneObj.getID());
        methDefault.invoke(connection, timeZoneObj);
        TimeZone.setDefault(timeZoneObj);
        DateTimeZone.setDefault(DateTimeZone.forTimeZone(timeZoneObj));
        LOG.info("Session Time Zone set to " + timeZoneObj.getID());
    } catch (Exception e) {
        LOG.error("Error setting time zone: " + e.getMessage());
    }
}

From source file:net.audumla.astronomy.algorithims.AstronomicalTest.java

@Test
public void testWrapperMethodsMexico() throws Exception {
    TimeZone.setDefault(TimeZone.getTimeZone("America/Mexico_City"));
    CelestialObject sun = new Sun();
    Calendar c = Calendar.getInstance(TimeZone.getDefault());
    c.setTimeInMillis(0);// w w w . ja  v  a  2  s  . c o  m
    c.set(Calendar.YEAR, 2009);
    c.set(Calendar.MONTH, Calendar.AUGUST);
    c.set(Calendar.DAY_OF_MONTH, 8);

    Date date = c.getTime();
    Geolocation.Location location = new Geolocation.Location();
    location.setLatitude(19.4328, Geolocation.Direction.NORTH);
    location.setLongitude(99.1333, Geolocation.Direction.WEST);
    TransitDetails details = sun.getTransitDetails(date, location, Sun.CIVIL);
    logger.debug("Mexico");
    logger.debug("Date    : " + date);
    logger.debug("Julian  : " + new JulianDate(date).julian());
    logger.debug("Sunrise : Algorithms: " + details.getRiseTime() + " : " + details.getRiseTime().getTime());
    logger.debug("Sunset  : Algorithms: " + details.getSetTime() + " : " + details.getSetTime().getTime());
    Assert.assertEquals(details.getRiseTime().getTime(), 1249732325341l, 1000);
    Assert.assertEquals(details.getSetTime().getTime(), 1249781509341l, 1000);
}

From source file:ch.agent.crnickl.demo.stox.Chart.java

/**
 * Construct a StockChart object. Set up the map for runtime arguments and
 * initialize it with valid names and default values.
 *///from w  ww .  jav  a2 s . c  o  m
public Chart() {
    this.chartSeries = new ArrayList<Chart.ChartSeries>();
    /* eliminate time zone and DST effects */
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

From source file:org.pentaho.di.junit.rules.RestorePDIEnvironment.java

@Override
protected void after() {
    cleanUp();//ww w.  j av  a 2  s  .co  m

    System.setProperties(originalProperties);
    Locale.setDefault(originalLocale);
    Locale.setDefault(Locale.Category.FORMAT, originalFormatLocale);
    LanguageChoice.getInstance().setDefaultLocale(originalLocale);
    TimeZone.setDefault(originalTimezone);
    FileUtils.deleteQuietly(tmpKettleHome.toFile());
}

From source file:org.openhab.binding.ntp.test.NtpOSGiTest.java

@AfterClass
public static void tearDownClass() {
    // Stopping the local time server
    timeServer.stopServer();/*  w  w  w. ja v  a 2 s . com*/
    // Set the default time zone and locale to their initial value.
    TimeZone.setDefault(systemTimeZone);
    Locale.setDefault(locale);
}

From source file:org.jfree.data.time.MillisecondTest.java

/**
 * Some checks for the getFirstMillisecond() method.
 *///from w  ww  .j a  va2s .  co  m
@Test
public void testGetFirstMillisecond() {
    Locale saved = Locale.getDefault();
    Locale.setDefault(Locale.UK);
    TimeZone savedZone = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
    Millisecond m = new Millisecond(500, 15, 43, 15, 1, 4, 2006);
    assertEquals(1143902595500L, m.getFirstMillisecond());
    Locale.setDefault(saved);
    TimeZone.setDefault(savedZone);
}