Example usage for java.text DateFormat setTimeZone

List of usage examples for java.text DateFormat setTimeZone

Introduction

In this page you can find the example usage for java.text DateFormat setTimeZone.

Prototype

public void setTimeZone(TimeZone zone) 

Source Link

Document

Sets the time zone for the calendar of this DateFormat object.

Usage

From source file:routines.system.BigDataParserUtils.java

public synchronized static java.util.Date parseTo_Date(String s, String pattern, boolean lenient) {
    if (isBlank(s)) {
        return null;
    }/*ww  w .j  a  va 2  s  .c om*/
    String s2 = s.trim();
    String pattern2 = pattern;
    if (isBlank(pattern2)) {
        pattern2 = Constant.dateDefaultPattern;
    }
    java.util.Date date = null;
    if (pattern2.equals("yyyy-MM-dd'T'HH:mm:ss'000Z'")) {
        if (!s2.endsWith("000Z")) {
            throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$
        }
        pattern2 = "yyyy-MM-dd'T'HH:mm:ss";
        s2 = s2.substring(0, s.lastIndexOf("000Z"));
    }
    DateFormat format = FastDateParser.getInstance(pattern2, lenient);
    ParsePosition pp = new ParsePosition(0);
    pp.setIndex(0);

    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    date = format.parse(s2, pp);
    if (pp.getIndex() != s2.length() || date == null) {
        throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return date;
}

From source file:org.projectforge.framework.xstream.converter.DateConverter.java

@Override
public Date fromString(String str) {
    if (StringUtils.isEmpty(str) == true || XmlConstants.NULL_IDENTIFIER.equals(str) == true) {
        return null;
    }/*w  ww  . j  a v a 2s.  c om*/
    String format = null;
    if (str.indexOf('-') > 0) {
        final int length = str.length();
        if (length == FORMAT_ISO_DATE.length()) {
            format = FORMAT_ISO_DATE;
        } else if (length == FORMAT_ISO_TIMESTAMP_MINUTES.length()) {
            format = FORMAT_ISO_TIMESTAMP_MINUTES;
        } else if (length == FORMAT_ISO_TIMESTAMP_SECONDS.length()) {
            format = FORMAT_ISO_TIMESTAMP_SECONDS;
        } else if (length == FORMAT_ISO_TIMESTAMP_MILLIS.length()) {
            format = FORMAT_ISO_TIMESTAMP_MILLIS;
        }
    }
    if (format != null) {
        final DateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(getTimeZone());
        try {
            return dateFormat.parse(str);
        } catch (ParseException ex) {
            log.warn("Can't parse date string '" + str + "'.");
            return null;
        }
    }
    try {
        final long timeInMillis = new Long(str);
        return new Date(timeInMillis);
    } catch (final NumberFormatException ex) {
        log.warn("Can't convert value '" + str + "' to time in millis (long value).");
        return null;
    }
}

From source file:com.ethercamp.harmony.keystore.FileSystemKeystore.java

private String getISODate(long milliseconds) {
    TimeZone tz = TimeZone.getTimeZone("UTC");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm'Z'");
    df.setTimeZone(tz);
    return df.format(new Date(milliseconds));
}

From source file:org.projectforge.continuousdb.UpdateEntryImpl.java

private Date parseUTCIsoDate(String isoDateString) {
    final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    df.setTimeZone(UTC);
    Date date;/* w w  w . j a v  a2 s.  c  o  m*/
    try {
        date = df.parse(isoDateString);
    } catch (final ParseException ex) {
        return null;
    }
    return date;

}

From source file:com.intuit.tank.harness.functions.JexlDateFunctions.java

/**
 * Get the current date./*from w  ww . j a v a2s . co m*/
 * 
 * @param format
 *            The format of the response (MM-dd-yyyy, MMddyyyy, etc)
 * @param timezone
 *            the timeZone that the date should be formatted in.
 * 
 * @return The current date
 */
public String currentDate(String format, String timezone) {
    DateFormat formatter = getFormatter(format);
    if (StringUtils.isNotBlank(timezone)) {
        formatter.setTimeZone(TimeZone.getTimeZone(timezone));
    }
    return formatter.format(new Date());
}

From source file:itdelatrisu.opsu.downloads.servers.OsuMirrorServer.java

/**
 * Returns a formatted date string from a raw date.
 * @param s the raw date string (e.g. "2015-05-14T23:38:47Z")
 * @return the formatted date, or the raw string if it could not be parsed
 *///  ww  w  .  ja va 2s.co m
private String formatDate(String s) {
    try {
        DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        f.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date d = f.parse(s);
        DateFormat fmt = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
        return fmt.format(d);
    } catch (ParseException e) {
        return s;
    }
}

From source file:org.coinspark.core.CSUtils.java

public static String date2iso8601(Date d) {
    if (d == null) {
        return null;
    }/*w ww . j a  v  a  2  s  . co  m*/

    TimeZone tz = TimeZone.getTimeZone("UTC");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
    df.setTimeZone(tz);

    return df.format(d);
}

From source file:com.cilogi.aws.cart.SignRequests.java

/**
 * Generate a ISO-8601 format timestamp as required by Amazon.
 *  /* www.  j a v a2s .c  o  m*/
 * @return  ISO-8601 format timestamp.
 */
private String timestamp() {
    Calendar cal = Calendar.getInstance();
    DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    dfm.setTimeZone(TimeZone.getTimeZone("GMT"));
    return dfm.format(cal.getTime());
}

From source file:com.telefonica.euro_iaas.sdc.util.MixlibAuthenticationDigesterImpl.java

/**
 * {@inheritDoc}/*from   w w w. j  ava2s .c o  m*/
 */
public Map<String, String> digest(String method, String path, String body, Date timestamp, String id,
        String pkUrl) {
    try {
        DateFormat df = new SimpleDateFormat(propertiesProvider.getProperty(CHEF_DATE_FORMAT));
        df.setTimeZone(TimeZone.getTimeZone(propertiesProvider.getProperty(CHEF_TIME_ZONE)));

        Map<String, String> headers = new HashMap<String, String>();
        headers.put(CONTENT_HASH_HEADER, getHash(body));
        headers.put(TIMESTAMP_HEADER, df.format(timestamp));
        headers.put(USER_HEADER, id);
        headers.put(SIGN_VERSION_HEADER, SIGN_VERSION_VALUE);

        String digest = MessageFormat.format(BODY_TEMPLATE, method.toUpperCase(), getHash(path), getHash(body),
                df.format(timestamp), id);
        digest = signer.sign(digest, new File(pkUrl));
        Integer length;
        Integer iteration = 1;
        while (!digest.isEmpty()) {
            length = digest.length() >= 60 ? 60 : digest.length();
            headers.put(AUTH_HEADER_PREFIX + iteration, digest.substring(0, length));
            digest = digest.substring(length);
            iteration = iteration + 1;
        }
        return headers;
    } catch (Exception e) {
        throw new SdcRuntimeException();
    }
}

From source file:com.healthmarketscience.jackcess.util.ExportTest.java

public void testExportToFile() throws Exception {
    DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    df.setTimeZone(TEST_TZ);

    for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
        Database db = create(fileFormat);
        db.setTimeZone(TEST_TZ);/*from  w  ww .  j  av a  2 s. co  m*/

        Table t = new TableBuilder("test").addColumn(new ColumnBuilder("col1", DataType.TEXT))
                .addColumn(new ColumnBuilder("col2", DataType.LONG))
                .addColumn(new ColumnBuilder("col3", DataType.DOUBLE))
                .addColumn(new ColumnBuilder("col4", DataType.OLE))
                .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN))
                .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME)).toTable(db);

        Date testDate = df.parse("19801231 00:00:00");
        t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(), true, testDate);

        t.addRow("crazy'data\"here", -345, -0.000345, createString(7).getBytes(), true, null);

        t.addRow("C:\\temp\\some_file.txt", 25, 0.0, null, false, null);

        StringWriter out = new StringWriter();

        new ExportUtil.Builder(db, "test").exportWriter(new BufferedWriter(out));

        String expected = "some text||some more,13,13.25,\"61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78\n79 7A 61 62  63 64\",true,"
                + testDate + NL + "\"crazy'data\"\"here\",-345,-3.45E-4,61 62 63 64  65 66 67,true," + NL
                + "C:\\temp\\some_file.txt,25,0.0,,false," + NL;

        assertEquals(expected, out.toString());

        out = new StringWriter();

        new ExportUtil.Builder(db, "test").setHeader(true).setDelimiter("||").setQuote('\'')
                .exportWriter(new BufferedWriter(out));

        expected = "col1||col2||col3||col4||col5||col6" + NL
                + "'some text||some more'||13||13.25||'61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78\n79 7A 61 62  63 64'||true||"
                + testDate + NL + "'crazy''data\"here'||-345||-3.45E-4||61 62 63 64  65 66 67||true||" + NL
                + "C:\\temp\\some_file.txt||25||0.0||||false||" + NL;
        assertEquals(expected, out.toString());

        ExportFilter oddFilter = new SimpleExportFilter() {
            private int _num;

            @Override
            public Object[] filterRow(Object[] row) {
                if ((_num++ % 2) == 1) {
                    return null;
                }
                return row;
            }
        };

        out = new StringWriter();

        new ExportUtil.Builder(db, "test").setFilter(oddFilter).exportWriter(new BufferedWriter(out));

        expected = "some text||some more,13,13.25,\"61 62 63 64  65 66 67 68  69 6A 6B 6C  6D 6E 6F 70  71 72 73 74  75 76 77 78\n79 7A 61 62  63 64\",true,"
                + testDate + NL + "C:\\temp\\some_file.txt,25,0.0,,false," + NL;

        assertEquals(expected, out.toString());
    }
}