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:contestTabulation.Main.java

private static void updateContestInfo(Map<Test, Pair<Integer, Integer>> testsGraded, List<Test> tiesBroken,
        Entity contestInfo, StringBuilder errorLog) throws JSONException {
    SimpleDateFormat isoFormat = new SimpleDateFormat("hh:mm:ss a EEEE MMMM d, yyyy zzzz");
    isoFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
    contestInfo.setProperty("updated", isoFormat.format(new Date()).toString());

    List<String> testsGradedList = new ArrayList<String>();
    for (Test test : testsGraded.keySet()) {
        if (testsGraded.get(test).x > 0 && tiesBroken.contains(test)) {
            testsGradedList.add(test.toString());
        }//from   w  w w. j a va2  s  .  c  o  m
    }
    contestInfo.setProperty("testsGraded", testsGradedList);

    JSONObject testsGradedJSON = new JSONObject();
    for (Entry<Test, Pair<Integer, Integer>> entry : testsGraded.entrySet()) {
        JSONArray numTests = new JSONArray().put(entry.getValue().x).put(entry.getValue().y);
        testsGradedJSON.put(entry.getKey().toString(), numTests);
    }

    contestInfo.setProperty("testsGradedNums", new Text(testsGradedJSON.toString()));

    contestInfo.setProperty("errorLog", new Text(errorLog.toString()));

    datastore.put(contestInfo);
}

From source file:com.icesoft.tutorial.TimeZoneBean.java

public static DateFormat buildDateFormatForTimeZone(TimeZone timeZone) {
    SimpleDateFormat currentFormat = new SimpleDateFormat("EEE, HH:mm:ss");
    Calendar currentZoneCal = Calendar.getInstance(timeZone);
    currentFormat.setCalendar(currentZoneCal);
    currentFormat.setTimeZone(timeZone);
    return currentFormat;
}

From source file:com.terremark.impl.AbstractAPIImpl.java

/**
 * Returns the ISO 8601 format date/time.
 *
 * @param time Date/time.//from  w  ww. j a  v a  2s  .  co m
 * @return ISO 8601 format date/time.
 */
protected static String getISO8601Time(final Date time) {
    if (time == null) {
        throw new IllegalArgumentException("Invalid date/time argument");
    }

    final SimpleDateFormat sdf = new SimpleDateFormat(TerremarkConstants.ISO_8601_DATE_FORMAT,
            Locale.getDefault());
    sdf.setTimeZone(TerremarkConstants.GMT_TIME_ZONE);

    return sdf.format(time);
}

From source file:com.ecyrd.jspwiki.preferences.Preferences.java

/**
 *  Get SimpleTimeFormat according to user browser locale and preferred time
 *  formats. If not found, it will revert to whichever format is set for the
 *  default/* w ww . j  av a 2 s . c o m*/
 * 
 *  @param context WikiContext to use for rendering.
 *  @param tf Which version of the dateformat you are looking for?
 *  @return A SimpleTimeFormat object which you can use to render
 *  @since 2.8
 */
public static SimpleDateFormat getDateFormat(WikiContext context, TimeFormat tf) {
    InternationalizationManager imgr = context.getEngine().getInternationalizationManager();
    Locale clientLocale = Preferences.getLocale(context);
    String prefTimeZone = Preferences.getPreference(context, "TimeZone");
    String prefDateFormat;

    log.debug("Checking for preferences...");

    switch (tf) {
    case DATETIME:
        prefDateFormat = Preferences.getPreference(context, "DateFormat");
        log.debug("Preferences fmt = " + prefDateFormat);
        if (prefDateFormat == null) {
            prefDateFormat = imgr.get(InternationalizationManager.CORE_BUNDLE, clientLocale,
                    "common.datetimeformat");
            log.debug("Using locale-format = " + prefDateFormat);
        }
        break;

    case TIME:
        prefDateFormat = imgr.get("common.timeformat");
        break;

    case DATE:
        prefDateFormat = imgr.get("common.dateformat");
        break;

    default:
        throw new InternalWikiException("Got a TimeFormat for which we have no value!");
    }

    try {
        SimpleDateFormat fmt = new SimpleDateFormat(prefDateFormat, clientLocale);

        if (prefTimeZone != null) {
            TimeZone tz = TimeZone.getTimeZone(prefTimeZone);
            // TimeZone tz = TimeZone.getDefault();
            // tz.setRawOffset(Integer.parseInt(prefTimeZone));

            fmt.setTimeZone(tz);
        }

        return fmt;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.krawler.spring.authHandler.authHandler.java

public static DateFormat getDateFormatter(String userTimeFormatId, String timeZoneDiff)
        throws ServiceException {
    SimpleDateFormat sdf = null;
    try {/*w  ww  .  j  a va2  s. com*/
        String dateformat = "";
        if (userTimeFormatId.equals("1")) {
            dateformat = "MMMM d, yyyy hh:mm:ss aa";
        } else {
            dateformat = "MMMM d, yyyy HH:mm:ss";
        }
        sdf = new SimpleDateFormat(dateformat);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT" + timeZoneDiff));
    } catch (Exception e) {
        throw ServiceException.FAILURE("authHandlerDAOImpl.getDateFormatter", e);
    }
    return sdf;
}

From source file:agileinterop.AgileInterop.java

private static JSONObject getPSRData(String body) throws ParseException, InterruptedException, APIException {
    // Parse body as JSON
    JSONParser parser = new JSONParser();
    JSONArray jsonBody = (JSONArray) parser.parse(body);

    Map<String, Object> data = new HashMap<>();

    class GetObjectData implements Runnable {
        private String psrNumber;
        private Map<String, Object> data;
        private IServiceRequest psr;

        public GetObjectData(String psrNumber, Map<String, Object> data)
                throws APIException, InterruptedException {
            this.psrNumber = psrNumber;
            this.data = data;

            psr = (IServiceRequest) Agile.session.getObject(IServiceRequest.OBJECT_TYPE, psrNumber);
        }/*ww w .jav a  2s. c  om*/

        @Override
        public void run() {
            this.data.put(psrNumber, new HashMap<String, Object>());

            try {
                if (psr != null) {
                    getCellValues();
                    getAttachments();
                    getHistory();
                }
            } catch (APIException ex) {
                Logger.getLogger(AgileInterop.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        private void getCellValues() throws APIException {
            Map<String, Object> cellValues = new HashMap<>();

            long startTime = System.currentTimeMillis();

            // Get cell values
            ICell[] cells = psr.getCells();
            for (ICell cell : cells) {
                if (cell.getDataType() == DataTypeConstants.TYPE_DATE) {
                    if (cell.getValue() != null) {
                        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz");
                        sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
                        cellValues.put(cell.getName(), sdf.format((Date) cell.getValue()));
                    } else {
                        cellValues.put(cell.getName(), cell.toString());
                    }
                } else {
                    cellValues.put(cell.getName(), cell.toString());
                }
            }

            long endTime = System.currentTimeMillis();

            String logMessage = String.format("%s: getCellValues executed in %d milliseconds", psrNumber,
                    endTime - startTime);
            System.out.println(logMessage);

            ((HashMap<String, Object>) this.data.get(psrNumber)).put("cellValues", cellValues);
        }

        private void getAttachments() throws APIException {
            List<Map<String, String>> attachments = new ArrayList<>();

            long startTime = System.currentTimeMillis();

            // Get attachments information
            ITable table = psr.getTable("Attachments");
            ITwoWayIterator tableIterator = table.getTableIterator();
            while (tableIterator.hasNext()) {
                IRow row = (IRow) tableIterator.next();
                Map<String, String> attachment = new HashMap<>();

                ICell[] cells = row.getCells();
                for (ICell cell : cells) {
                    if (cell.getDataType() == DataTypeConstants.TYPE_DATE) {
                        if (cell.getValue() != null) {
                            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz");
                            sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
                            attachment.put(cell.getName(), sdf.format((Date) cell.getValue()));
                        } else {
                            attachment.put(cell.getName(), cell.toString());
                        }
                    } else {
                        attachment.put(cell.getName(), cell.toString());
                    }
                }

                attachments.add(attachment);
            }

            long endTime = System.currentTimeMillis();

            String logMessage = String.format("%s: getAttachments executed in %d milliseconds", psrNumber,
                    endTime - startTime);
            System.out.println(logMessage);

            ((HashMap<String, Object>) this.data.get(psrNumber)).put("attachments", attachments);
        }

        private void getHistory() throws APIException {
            List<Map<String, String>> histories = new ArrayList<>();

            long startTime = System.currentTimeMillis();

            // Get history information
            ITable table = psr.getTable("History");
            ITwoWayIterator tableIterator = table.getTableIterator();
            while (tableIterator.hasNext()) {
                IRow row = (IRow) tableIterator.next();
                Map<String, String> history = new HashMap<>();

                ICell[] cells = row.getCells();
                for (ICell cell : cells) {
                    if (cell.getDataType() == DataTypeConstants.TYPE_DATE) {
                        if (cell.getValue() != null) {
                            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz");
                            sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
                            history.put(cell.getName(), sdf.format((Date) cell.getValue()));
                        } else {
                            history.put(cell.getName(), cell.toString());
                        }
                    } else {
                        history.put(cell.getName(), cell.toString());
                    }
                }

                histories.add(history);
            }

            long endTime = System.currentTimeMillis();

            String logMessage = String.format("%s: getHistory executed in %d milliseconds", psrNumber,
                    endTime - startTime);
            System.out.println(logMessage);

            ((HashMap<String, Object>) this.data.get(psrNumber)).put("history", histories);
        }
    }

    synchronized (data) {
        // Do something funky with the first one
        Thread t = new Thread(new GetObjectData(jsonBody.get(0).toString(), data));
        t.start();
        t.join();

        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (Object object : jsonBody.subList(1, jsonBody.size() - 1)) {
            executor.execute(new Thread(new GetObjectData(object.toString(), data)));
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
        }
    }

    JSONObject obj = new JSONObject();
    obj.put("data", data);
    return obj;
}

From source file:com.akamai.edgegrid.auth.EdgeGridV1Signer.java

/**
 * Helper to get the formatted time stamp. 
 * //from ww w  . j av a  2  s  . co m
 * @param time the time stamp as millisecond since the UNIX epoch.
 * @return the formatted time stamp.
 */
private static String getTimeStamp(long time) {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ");
    Date date = new Date(time);

    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    return format.format(date);
}

From source file:org.dasein.cloud.terremark.Terremark.java

private static String getRfcDate() {
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat(RFC1123_PATTERN);
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    return format.format(date);
}

From source file:com.blackducksoftware.integration.hub.rest.RestConnection.java

public static Date parseDateString(final String dateString) throws ParseException {
    final SimpleDateFormat sdf = new SimpleDateFormat(JSON_DATE_FORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    return sdf.parse(dateString);
}

From source file:com.blackducksoftware.integration.hub.rest.RestConnection.java

public static String formatDate(final Date date) {
    final SimpleDateFormat sdf = new SimpleDateFormat(JSON_DATE_FORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    return sdf.format(date);
}