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:net.antidot.sql.model.core.SQLConnector.java

/**
 * Convert timeZone into a valid xsd:dateTime format.
 * /*from   w ww.  j  a  v  a 2s. c o m*/
 * @param tz
 * @return
 */
public static String timeZoneToStr(TimeZone tz) {
    if (tz == null)
        return null;
    // Exception if timeZone is UTC
    if (tz.getID().equals("UTC"))
        return "Z";
    // Convert timeZone into a valid xsd:dateTime format
    SimpleDateFormat df = new SimpleDateFormat("Z");
    df.setTimeZone(tz);
    String result = df.format(new Date());
    // xsd:dateTime requires a ":" in timeZone
    result = result.substring(0, result.length() - 2) + ":" + result.substring(result.length() - 2);
    return result;
}

From source file:com.bearstech.android.myownsync.client.NetworkUtilities.java

/**
 * Fetches the list of friend data updates from the server
 * //  www  . j  av a 2 s .  co  m
 * @param account The account being synced.
 * @param authtoken The authtoken stored in AccountManager for this account
 * @param lastUpdated The last time that sync was performed
 * @return list The list of updates received from the server.
 */
public static List<User> fetchFriendUpdates(Account account, String authtoken, Date lastUpdated)
        throws JSONException, ParseException, IOException, AuthenticationException {
    final ArrayList<User> friendList = new ArrayList<User>();
    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, account.name));
    params.add(new BasicNameValuePair(PARAM_PASSWORD, authtoken));
    if (lastUpdated != null) {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        params.add(new BasicNameValuePair(PARAM_UPDATED, formatter.format(lastUpdated)));
    }
    Log.i(TAG, params.toString());

    HttpEntity entity = null;
    entity = new UrlEncodedFormEntity(params);
    final HttpPost post = new HttpPost(base_url + FETCH_FRIEND_UPDATES_URI);
    post.addHeader(entity.getContentType());
    post.setEntity(entity);
    maybeCreateHttpClient();

    final HttpResponse resp = mHttpClient.execute(post);
    final String response = EntityUtils.toString(resp.getEntity());

    if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        // Succesfully connected to the samplesyncadapter server and
        // authenticated.
        // Extract friends data in json format.
        final JSONArray friends = new JSONArray(response);
        Log.d(TAG, response);
        for (int i = 0; i < friends.length(); i++) {
            friendList.add(User.valueOf(friends.getJSONObject(i)));
        }
    } else {
        if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            Log.e(TAG, "Authentication exception in fetching remote contacts");
            throw new AuthenticationException();
        } else {
            Log.e(TAG, "Server error in fetching remote contacts: " + resp.getStatusLine());
            throw new IOException();
        }
    }
    return friendList;
}

From source file:com.yahoo.dba.perf.myperf.springmvc.MyPerfBaseController.java

/**
 * Date range in yyyyMMddHHmmss format, for metrics and alert retrieval.
 * @param startDate/*from w ww .ja v  a2s  . c  o  m*/
 * @param endDate
 * @return
 */
public static String[] getDateRange(String startDate, String endDate) {
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");//metricsDB TS column format
    java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("yyyy-MM-dd HH");//URL request format
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    sdf2.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d1 = null;
    if (startDate != null && !startDate.isEmpty()) {
        try {
            if (startDate.indexOf('-') < 0)
                d1 = sdf.parse(startDate);
            else
                d1 = sdf2.parse(startDate);
        } catch (Exception ex) {
        }
    }
    //default to one day earlier
    if (d1 == null) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        d1 = c.getTime();
    }
    String startDateFmt = sdf.format(d1);

    Date d2 = null;
    if (endDate != null && !endDate.isEmpty()) {
        try {
            if (endDate.indexOf('-') < 0)
                d2 = sdf.parse(endDate);
            else
                d2 = sdf2.parse(endDate);
        } catch (Exception ex) {
        }
    }
    //default to now
    if (d2 == null) {
        d2 = new Date();
    }
    String endDateFmt = sdf.format(d2);
    return new String[] { startDateFmt, endDateFmt };
}

From source file:org.hl7.fhir.client.ResourceAddress.java

public static String getCalendarDateInIsoTimeFormat(Calendar calendar) {
    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd'T'hh:mm:ss");//TODO Move out
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    return format.format(calendar.getTime());
}

From source file:com.arantius.tivocommander.Utils.java

private final static Date parseDateTimeStr(SimpleDateFormat dateParser, String dateStr) {
    TimeZone tz = TimeZone.getTimeZone("UTC");
    dateParser.setTimeZone(tz);
    ParsePosition pp = new ParsePosition(0);
    return dateParser.parse(dateStr, pp);
}

From source file:com.ibm.jaql.json.type.JsonDate.java

/** Converts the given format string into a {@link DateFormat}. */
public static DateFormat getFormat(String formatString) {
    SimpleDateFormat format = new SimpleDateFormat(formatString); // TODO: add cache of formats
    if (formatString.endsWith("'Z'") || formatString.endsWith("'z'")) {
        TimeZone tz = new SimpleTimeZone(0, "UTC");
        format.setTimeZone(tz);
    }//from w  w  w.j a va2  s  .co  m
    return format;
}

From source file:DateParser.java

/**
 * Parses the date value using the given date formats.
 * //from w ww.ja  va 2s .c  o  m
 * @param dateValue the date value to parse
 * @param dateFormats the date formats to use
 * 
 * @return the parsed date
 * 
 * @throws DateParseException if none of the dataFormats could parse the dateValue
 */
public static Date parseDate(String dateValue, Collection dateFormats) {

    if (dateValue == null) {
        throw new IllegalArgumentException("dateValue is null");
    }
    if (dateFormats == null) {
        dateFormats = DEFAULT_PATTERNS;
    }
    // trim single quotes around date if present
    // see issue #5279
    if (dateValue.length() > 1 && dateValue.startsWith("'") && dateValue.endsWith("'")) {
        dateValue = dateValue.substring(1, dateValue.length() - 1);
    }

    SimpleDateFormat dateParser = null;
    Iterator formatIter = dateFormats.iterator();

    while (formatIter.hasNext()) {
        String format = (String) formatIter.next();
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, Locale.US);
            dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
        } else {
            dateParser.applyPattern(format);
        }
        try {
            return dateParser.parse(dateValue);
        } catch (ParseException pe) {
            // ignore this exception, we will try the next format
        }
    }

    // we were unable to parse the date
    throw new RuntimeException("Unable to parse the date " + dateValue);
}

From source file:com.kubotaku.android.openweathermap.lib.WeatherParser.java

private static void parseForecastDateTime(WeatherInfo info, JSONObject object) {
    try {/*from   w  w  w .  j ava  2s  .  co  m*/
        if (object.has(KEY_DT)) {
            // received data's unit is sec, so convert unit to millisecond.
            long time = object.getLong(KEY_DT) * 1000;
            info.setTime(time);

            TimeZone tz = TimeZone.getDefault();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd kk:mm");
            sdf.setTimeZone(tz);
            Date date = new Date(time);
            String forecastDate = sdf.format(date);
            info.setForecastDate(forecastDate);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.microsoft.tfs.client.clc.prompt.Prompt.java

private static String getPATDisplayName() {
    // following IntelliJ Plugin's format
    final String formatter = "TF from: %s on: %s"; //$NON-NLS-1$
    final String machineName = LocalHost.getShortName();

    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); //$NON-NLS-1$
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
    final String time = dateFormat.format(new Date());

    return String.format(formatter, machineName, time);
}

From source file:com.ykun.commons.utils.commons.DateUtils.java

/**
 * @param timezone //from   w ww. j  a va2s .c om
 * @param dateTime 
 * @return
 * @throws ParseException
 * @throws
 * @Description:??
 */
public static long getTimestamp(String timezone, String dateTime, String pattern) throws ParseException {
    SimpleDateFormat sdf = null;
    if (null == pattern || "".equals(pattern)) {
        sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
    } else {
        sdf = new SimpleDateFormat(pattern);
    }
    sdf.setTimeZone(TimeZone.getTimeZone(timezone));
    long b = sdf.parse(dateTime).getTime() / 1000;
    return b;
}