Example usage for java.util SimpleTimeZone SimpleTimeZone

List of usage examples for java.util SimpleTimeZone SimpleTimeZone

Introduction

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

Prototype

public SimpleTimeZone(int rawOffset, String ID) 

Source Link

Document

Constructs a SimpleTimeZone with the given base time zone offset from GMT and time zone ID with no daylight saving time schedule.

Usage

From source file:edu.ucsb.nceas.ezid.test.EZIDServiceTest.java

/** Generate a timestamp for use in IDs. */
public static String generateTimeString() {
    StringBuffer guid = new StringBuffer();

    // Create a calendar to get the date formatted properly
    String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
    SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
    pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    Calendar calendar = new GregorianCalendar(pdt);
    Date trialTime = new Date();
    calendar.setTime(trialTime);//from   w w w.  j ava  2s .  c  o m
    guid.append(calendar.get(Calendar.YEAR));
    guid.append(calendar.get(Calendar.DAY_OF_YEAR));
    guid.append(calendar.get(Calendar.HOUR_OF_DAY));
    guid.append(calendar.get(Calendar.MINUTE));
    guid.append(calendar.get(Calendar.SECOND));
    guid.append(calendar.get(Calendar.MILLISECOND));
    double random = Math.random();
    guid.append(random);

    return guid.toString();
}

From source file:com.amazonaws.util.JodaTime.java

private static boolean checkParseIso8601Date() throws ParseException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
    String formatted = sdf.format(date);
    String alternative = DateUtils.iso8601DateFormat.print(date.getTime());
    if (formatted.equals(alternative)) {
        Date expectedDate = sdf.parse(formatted);
        Date actualDate = DateUtils.doParseISO8601Date(formatted);
        return expectedDate.equals(actualDate);
    }/*ww  w.ja v a  2 s  .  c  om*/
    return false;
}

From source file:com.amazonaws.sqs.util.SQSClient.java

private static String getExpires() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 60);

    SimpleTimeZone timeZone = new SimpleTimeZone(0, "PST");

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    format.setTimeZone(timeZone);/* ww  w  .j av a  2s .  com*/
    String timestampStr = format.format(cal.getTime());

    return timestampStr;
}

From source file:org.globus.workspace.service.impls.site.PilotNotificationUtil.java

static Calendar parseTimestamp(String pilot, String timestamp) throws Exception {

    String errmsg = "non conforming timestamp sent by pilot '" + pilot + "', timestamp = '" + timestamp + "', ";

    if (timestamp == null) {
        throw new Exception(errmsg + "timestamp may not be null");
    }/*from www . j a v  a 2s.  c  om*/

    final String[] tokens = timestamp.trim().split("-");

    if (tokens.length != 6) {
        throw new Exception(errmsg + "token length = " + tokens.length);
    }

    final int year;
    final int month;
    final int day;
    final int hour;
    final int minute;
    final int second;
    try {
        year = Integer.parseInt(tokens[0]);
        int mo = Integer.parseInt(tokens[1]);
        month = mo - 1; // Calendar needs month to start from zero
        day = Integer.parseInt(tokens[2]);
        hour = Integer.parseInt(tokens[3]);
        minute = Integer.parseInt(tokens[4]);
        second = Integer.parseInt(tokens[5]);
    } catch (NumberFormatException e) {
        throw new Exception(errmsg + "a token is not an integer");
    }

    // negative numbers are impossible because we split by -

    // calendar is OK with greater numbers than expected (it just
    // increases its internal timestamp by that much); we're not

    if (month > 11) {
        throw new Exception(errmsg + "month > 11");
    }

    if (day > 31) {
        throw new Exception(errmsg + "day > 31");
    }

    if (hour > 23) {
        throw new Exception(errmsg + "hour > 23");
    }

    if (minute > 59) {
        throw new Exception(errmsg + "minute > 59");
    }

    // 61 is intentional, leaps etc...
    if (second > 61) {
        throw new Exception(errmsg + "second > 61");
    }

    final TimeZone tz = new SimpleTimeZone(0, "GMT");
    final Calendar cal = Calendar.getInstance(tz);
    cal.clear();
    cal.set(year, month, day, hour, minute, second);
    return cal;
}

From source file:org.wso2.ei.analytics.elk.publisher.ElasticStatisticsPublisher.java

/**
 * Takes time in milliseconds and returns the formatted date and time according to Elasticsearch
 *
 * @param time long time in millis// w  ww  . java2 s .  c  o m
 * @return timeStamp formatted according to the Elasticsearch
 */
private static String getFormattedDate(long time) {
    Date date = new Date(time);

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDate = dateFormat.format(date);

    DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
    timeFormat.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
    String formattedTime = timeFormat.format(date);

    return formattedDate + "T" + formattedTime + "Z";
}

From source file:com.amazonaws.util.JodaTime.java

private static boolean checkParseIso8601DateUsingAlternativeFormat() throws ParseException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
    String formatted = sdf.format(date);
    String alternative = DateUtils.alternateIso8601DateFormat.print(date.getTime());
    if (formatted.equals(alternative)) {
        Date expectedDate = sdf.parse(formatted);
        Date actualDate = DateUtils.parseISO8601Date(formatted);
        return expectedDate.equals(actualDate);
    }/*  w  w w .ja va2 s.  c  om*/
    return false;
}

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  .  j  a  va 2  s . c o  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.datavyu.util.UIUtils.java

/**
 * Converts milliseconds to a timestamp string.
 * @param milliseconds number of milliseconds
 * @return Timestamp String/*from  w  w  w. jav a  2 s.co m*/
 */
public static String millisecondsToTimestamp(final long milliseconds) {

    // DateFormat is not thread safe.
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
    sdf.setTimeZone(new SimpleTimeZone(0, "NO_ZONE"));

    return sdf.format(milliseconds);
}

From source file:com.amazonaws.util.JodaTime.java

private static boolean checkAlternateIso8601DateFormat() throws ParseException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
    String expected = sdf.format(date);
    String actual = DateUtils.alternateIso8601DateFormat.print(date.getTime());
    if (expected.equals(actual)) {
        Date expectedDate = sdf.parse(expected);
        DateTime actualDateTime = DateUtils.alternateIso8601DateFormat.parseDateTime(actual);
        return expectedDate.getTime() == actualDateTime.getMillis();
    }/*w  ww .j av  a2 s .  c  o m*/
    return false;
}

From source file:com.qut.middleware.saml2.SAML2Test.java

private XMLGregorianCalendar generateXMLCalendar(int offset) {
    SimpleTimeZone gmt;//from  w w w.ja va 2 s. c  o  m
    GregorianCalendar calendar;
    XMLGregorianCalendar xmlCalendar;

    /* GMT timezone TODO: I am sure this isn't correct need to ensure we set GMT */
    // GMT or UTC ??
    gmt = new SimpleTimeZone(0, "UTC");
    calendar = new GregorianCalendar(gmt);
    calendar.add(Calendar.MILLISECOND, offset);
    xmlCalendar = new XMLGregorianCalendarImpl(calendar);

    return xmlCalendar;
}