Example usage for java.text SimpleDateFormat setTimeZone

List of usage examples for java.text SimpleDateFormat setTimeZone

Introduction

In this page you can find the example usage for java.text SimpleDateFormat 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:com.amazonaws.util.JodaTime.java

private static boolean checkParseCompressedIso8601Date() throws ParseException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
    sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
    String formatted = sdf.format(date);
    Date expected = sdf.parse(formatted);
    Date actual = new Date(DateUtils.compressedIso8601DateFormat.parseMillis(formatted));
    return expected.equals(actual);
}

From source file:DateTimeUtil.java

/**
 * Return a String value of Now() in a specify format
 * @param lang lingua di default// w  ww .  j a  va2s.  c om
 * @param format formato della data vedi esempi tsDefaultFormats
 * @return String value of Now() in a specify format
 */
public static final String getNowWithFormat(String format, String lang) {
    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    String DATE_FORMAT = format;
    SimpleDateFormat sdf = null;

    if (lang != null) {
        sdf = new SimpleDateFormat(DATE_FORMAT, new Locale(lang));
    } else {
        sdf = new SimpleDateFormat(DATE_FORMAT);
    }

    sdf.setTimeZone(TimeZone.getDefault());
    return sdf.format(cal.getTime());
}

From source file:com.mendhak.gpslogger.common.OpenGTSClient.java

public static String NMEAGPRMCTime(Date dateToFormat) {
    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    return sdf.format(dateToFormat);
}

From source file:com.mendhak.gpslogger.common.OpenGTSClient.java

public static String NMEAGPRMCDate(Date dateToFormat) {
    SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    return sdf.format(dateToFormat);
}

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);
    }/*from w ww. ja  v a  2s  . com*/
    return false;
}

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();
    }//from  w  w  w .j a  va2s  .com
    return false;
}

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

private static boolean checkFormatIso8601Date() 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 expected = sdf.format(date);
    String actual = DateUtils.iso8601DateFormat.print(date.getTime());
    if (expected.equals(actual)) {
        Date expectedDate = sdf.parse(expected);
        Date actualDate = DateUtils.doParseISO8601Date(actual);
        return expectedDate.equals(actualDate);
    }// www  . ja v a 2 s  . c o m
    return false;
}

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);
    }/* w ww. j a v  a 2s . co m*/
    return false;
}

From source file:api.util.JsonUtil.java

public static Date asDate(Object val) {
    if (val == JSONObject.NULL)
        return null;
    if (val instanceof String) {
        String stringValue = (String) val;
        try {/*from ww w  .  ja v a  2  s .  c  o m*/
            // try ISO 8601
            Calendar cal = DatatypeConverter.parseDateTime(stringValue);
            if (!iso8601Timezone.matcher(stringValue).matches()) {
                // timezone is absent from input string. Assume UTC
                cal.setTimeZone(TimeZone.getTimeZone("UTC"));
            }
            return cal.getTime();
        } catch (IllegalArgumentException iae) {
            // fallback to RFC 2822
            for (String format : new String[] { "EEE, dd MMM yyyy HH:mm:ss Z", "dd MMM yyyy HH:mm:ss Z",
                    "EEE, dd MMM yyyy HH:mm Z", "dd MMM yyyy HH:mm Z", "EEE, dd MMM yyyy HH:mm:ss",
                    "dd MMM yyyy HH:mm:ss", "EEE, dd MMM yyyy HH:mm", "dd MMM yyyy HH:mm" }) {
                SimpleDateFormat rfc2822Fmt = new SimpleDateFormat(format, Locale.ENGLISH);
                rfc2822Fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
                try {
                    return rfc2822Fmt.parse(stringValue);
                } catch (ParseException e) {
                }
            }
        }
    } else if (val instanceof Number) {
        return new Date(((Number) val).longValue() * 1000);
    }
    throw new IllegalArgumentException(
            "Value is expected to be a unix timestamp or a string in either ISO 8601 or RFC 2822 format: "
                    + val);
}

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

private static boolean checkParseRfc822Date() throws ParseException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
    String formatted = sdf.format(date);
    Date expected = sdf.parse(formatted);
    Date actual2 = new Date(DateUtils.rfc822DateFormat.parseMillis(formatted));
    return expected.equals(actual2);
}