Example usage for java.util TimeZone getTimeZone

List of usage examples for java.util TimeZone getTimeZone

Introduction

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

Prototype

public static TimeZone getTimeZone(ZoneId zoneId) 

Source Link

Document

Gets the TimeZone for the given zoneId .

Usage

From source file:com.amazon.pay.impl.Util.java

public static String getTimestamp() {
    final Date date = new Date();
    final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";
    final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
    final TimeZone utc = TimeZone.getTimeZone("UTC");
    sdf.setTimeZone(utc);/*from w w  w. j a  v a  2s . c  o m*/
    String timeStamp = sdf.format(date);
    return timeStamp.replace("UTC", "Z");
}

From source file:moefou4j.internal.util.Moefou4JInternalParseUtil.java

public static Date getDate(final String name, final String format) throws MoefouException {
    SimpleDateFormat sdf = formatMap.get().get(format);
    if (null == sdf) {
        sdf = new SimpleDateFormat(format, Locale.ENGLISH);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        formatMap.get().put(format, sdf);
    }/*from   ww  w. jav a2 s.c o  m*/
    try {
        return sdf.parse(name);
    } catch (final ParseException pe) {
        throw new MoefouException("Unexpected date format(" + name + ") returned from twitter.com", pe);
    }
}

From source file:inti.util.DateFormatterPerformanceTest.java

public void format(int count) throws Exception {
    Date date = new Date();
    DateFormatter dateFormatter = new DateFormatter();
    FastDateFormat fastDateFormat = FastDateFormat.getInstance("EEE, dd-MMM-yyyy hh:mm:ss 'GMT'");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss 'GMT'");
    StringBuilder builder = new StringBuilder();
    long start, end;
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            dateFormatter.formatDate(date.getTime(), Format.RFC1123, builder, cal);
            builder.delete(0, builder.length());
        }/*from w w  w  .ja  va  2s  .  c om*/
        Thread.sleep(10);
    }

    start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        dateFormatter.formatDate(date.getTime(), Format.RFC1123, builder, cal);
        builder.delete(0, builder.length());
    }
    end = System.currentTimeMillis();
    System.out.format("format(DateFormatter-special) - count: %d, duration: %dms, ratio: %#.4f", count,
            end - start, count / ((end - start) / 1000.0));
    System.out.println();

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            dateFormatter.formatDate(date.getTime());
        }
        Thread.sleep(10);
    }

    start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        dateFormatter.formatDate(date.getTime());
    }
    end = System.currentTimeMillis();
    System.out.format("format(DateFormatter-simple) - count: %d, duration: %dms, ratio: %#.4f", count,
            end - start, count / ((end - start) / 1000.0));
    System.out.println();

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            fastDateFormat.format(date);
        }
        Thread.sleep(10);
    }

    start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        fastDateFormat.format(date);
    }
    end = System.currentTimeMillis();
    System.out.format("format(FastDateFormat) - count: %d, duration: %dms, ratio: %#.4f", count, end - start,
            count / ((end - start) / 1000.0));
    System.out.println();

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 10; j++) {
            simpleDateFormat.format(date);
        }
        Thread.sleep(10);
    }

    start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        simpleDateFormat.format(date);
    }
    end = System.currentTimeMillis();
    System.out.format("format(SimpleDateFormat) - count: %d, duration: %dms, ratio: %#.4f", count, end - start,
            count / ((end - start) / 1000.0));
    System.out.println();
}

From source file:com.cloudkick.monitoring.CheckState.java

public CheckState(JSONObject state) throws JSONException {
    // Grab the "whence" if available
    if (state.has("whence")) {
        whenceWireFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        whenceWireFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        String whenceString = state.getString("whence");
        try {/* ww  w.  j a va  2 s.  co  m*/
            long whenceMillis = whenceWireFormat.parse(whenceString).getTime();
            Time now = new Time();
            now.setToNow();
            long diffMillis = ((now.toMillis(true)) - whenceMillis);
            if (whenceMillis == 0) {
                whence = "never";
            } else if (diffMillis < 3600 * 1000) {
                whence = String.format("%d m", diffMillis / (1000 * 60));
            } else if (diffMillis < (24 * 3600 * 1000)) {
                long mins = (diffMillis / (1000 * 60)) % 60;
                long hours = (diffMillis / (1000 * 3600));
                whence = String.format("%d h, %d m", hours, mins);
            } else if (diffMillis < (7 * 24 * 3600 * 1000)) {
                long hours = (diffMillis / (1000 * 60 * 60)) % 24;
                long days = (diffMillis / (1000 * 60 * 60 * 24));
                whence = String.format("%d d, %d h", days, hours);
            } else {
                long days = (diffMillis / (1000 * 60 * 60 * 24)) % 7;
                long weeks = (diffMillis / (1000 * 60 * 60 * 24 * 7));
                whence = String.format("%d w, %d d", weeks, days);
            }
        } catch (ParseException e) {
            whence = null;
        }
    } else {
        whence = null;
    }

    // Grab the "service_state" if available
    if (state.has("service_state")) {
        serviceState = state.getString("service_state");
    } else {
        serviceState = "UNKNOWN";
    }

    // Depending on the serviceState set the status and color
    if (serviceState.equals("OK")) {
        status = state.getString("status");
        stateColor = 0xFF088A08;
        stateSymbol = "\u2714";
        priority = 4;
    } else if (serviceState.equals("ERROR")) {
        status = state.getString("status");
        stateColor = 0xFFE34648;
        stateSymbol = "\u2718";
        priority = 0;
    } else if (serviceState.equals("WARNING")) {
        status = state.getString("status");
        stateColor = 0xFFDF7401;
        stateSymbol = "!";
        priority = 1;
    } else if (serviceState.equals("NO-AGENT")) {
        status = "Agent Not Connected";
        stateColor = 0xFF6E6E6E;
        stateSymbol = "?";
        priority = 2;
    } else if (serviceState.equals("UNKNOWN")) {
        status = "No Data Available";
        stateColor = 0xFF6E6E6E;
        stateSymbol = "?";
        priority = 3;
    } else {
        Log.e("Check", "Unknown Service State: " + serviceState);
        status = state.getString("status");
        stateColor = 0xFF6E6E6E;
        stateSymbol = "?";
        priority = 3;
    }
}

From source file:com.vangent.hieos.xutil.hl7.date.Hl7Date.java

/**
 * Convert a Java date to HL7 format.// ww  w.j a v a  2  s  . c  om
 *
 * @param date Java date.
 * @return String In HL7 format.
 */
static public String toHL7format(Date date) {
    String hl7DateFormat = "yyyyMMdd";
    SimpleDateFormat formatter = new SimpleDateFormat(hl7DateFormat);
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    String hl7formattedDate = formatter.format(date);
    hl7formattedDate.replaceAll("UTC", "Z");
    return hl7formattedDate;
}

From source file:GmtDate.java

/**
 *  Description of the Method/*from  www  . ja  v  a 2s .  c  om*/
 *
 * @param  strDate                       Description of the Parameter
 * @return                               Description of the Return Value
 * @exception  java.text.ParseException  Description of the Exception
 */
public final static Date parseDateFormat(String strDate) throws java.text.ParseException {
    Date result = null;
    if (strDate.length() == 10) {
        dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        result = dateFormatter.parse(strDate);
    } else if (strDate.length() == 8) {
        date2dyFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        result = date2dyFormatter.parse(strDate);
    }

    return result;
}

From source file:Activities.java

private String addData(String endpoint) {
    String data = null;/*from  ww w  .  ja  va2s.co m*/
    try {
        // Construct request payload
        JSONObject attrObj = new JSONObject();
        attrObj.put("name", "URL");
        attrObj.put("value", "http://www.nvidia.com/game-giveaway");

        JSONArray attrArray = new JSONArray();
        attrArray.add(attrObj);

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

        // Required attributes
        JSONObject obj = new JSONObject();
        obj.put("leadId", "1001");
        obj.put("activityDate", dateAsISO);
        obj.put("activityTypeId", "1001");
        obj.put("primaryAttributeValue", "Game Giveaway");
        obj.put("attributes", attrArray);
        System.out.println(obj);

        // Make request
        URL url = new URL(endpoint);
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("POST");
        urlConn.setAllowUserInteraction(false);
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty("Content-type", "application/json");
        urlConn.setRequestProperty("accept", "application/json");
        urlConn.connect();
        OutputStream os = urlConn.getOutputStream();
        os.write(obj.toJSONString().getBytes());
        os.close();

        // Inspect response
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            System.out.println("Status: 200");
            InputStream inStream = urlConn.getInputStream();
            data = convertStreamToString(inStream);
            System.out.println(data);
        } else {
            System.out.println(responseCode);
            data = "Status:" + responseCode;
        }
    } catch (MalformedURLException e) {
        System.out.println("URL not valid.");
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace();
    }

    return data;
}

From source file:XSDDateTime.java

public static String getDateTime(Calendar cal) {
    if (!cal.getTimeZone().equals(TimeZone.getTimeZone("GMT+00:00"))) {
        throw new InvalidParameterException();
    }//  ww  w.j  a v  a2  s  .com
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    month++;
    String monthString = pad(month);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    String dayString = pad(day);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    String hourString = pad(hour);
    int minutes = cal.get(Calendar.MINUTE);
    String minutesString = pad(minutes);
    int seconds = cal.get(Calendar.SECOND);
    String secondsString = pad(seconds);

    return year + "-" + monthString + "-" + dayString + "T" + hourString + ":" + minutesString + ":"
            + secondsString + "Z";
}

From source file:com.amaxilatis.orion.test.OrionTest2.java

@Before
public void setUp() throws Exception {
    BasicConfigurator.configure();//from   www.ja va2s . c  o  m
    mapper = new ObjectMapper();
    TimeZone tz = TimeZone.getTimeZone("UTC");
    df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
    df.setTimeZone(tz);
    final String serverUrl = "http://146.169.46.162:1026/";
    client = new OrionClient(serverUrl, "", "organicity", "/");
}

From source file:com.github.rnewson.couchdb.lucene.DocumentConverterTest.java

@Before
public void setup() {
    context = Context.enter();
    tz = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
}