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.pwcgarage.ibeaconref.restclients.EventHubRestClient.java

protected static String getCurrentTimeUtc() {
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    final String utcTime = sdf.format(new Date());
    return utcTime;

}

From source file:Main.java

public static String getDateForHistory(String dateString) {
    String[] shortMonths = new DateFormatSymbols().getShortMonths();

    SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat formater = new SimpleDateFormat("dd");
    String month = "";

    parserSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
    formater.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = null;/*from ww  w .  j a va2 s  .  c  o m*/
    try {
        parserSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
        date = parserSDF.parse(dateString);
        month = shortMonths[date.getMonth()];
    } catch (ParseException e) {
        Log.d(TAG, e.getMessage());
    }

    return month + " " + formater.format(date);
}

From source file:Main.java

private static String createDate(Date date, String defaultFormat, Boolean utc) {
    SimpleDateFormat gmtFormat = new SimpleDateFormat();
    gmtFormat.applyPattern(defaultFormat);
    TimeZone gmtTime = (utc) ? TimeZone.getTimeZone("GMT") : TimeZone.getDefault();
    gmtFormat.setTimeZone(gmtTime);
    return gmtFormat.format(date);
}

From source file:Main.java

public static String getDaysWithoutYear(String srcDate) {

    SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat formater = new SimpleDateFormat("MM/dd");

    Date date = null;//from  w ww  . j  a  va2 s . c  o m
    try {
        parserSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
        date = parserSDF.parse(srcDate);
    } catch (ParseException e) {
        Log.d(TAG, e.getMessage());
    }

    return formater.format(date);
}

From source file:Main.java

/**
 * Returns date before given days//ww w.j a  va 2  s.  co  m
 *
 * @param days days to before
 * @return string date string before days
 */
public static String getDateBefore(int days) {
    Date today = new Date();
    Calendar cal = new GregorianCalendar();
    cal.setTime(today);
    cal.add(Calendar.DAY_OF_MONTH, days * -1);
    Date date = cal.getTime();
    SimpleDateFormat gmtFormat = new SimpleDateFormat();
    gmtFormat.applyPattern("yyyy-MM-dd 00:00:00");
    TimeZone gmtTime = TimeZone.getTimeZone("GMT");
    gmtFormat.setTimeZone(gmtTime);
    return gmtFormat.format(date);
}

From source file:Main.java

public static Date parse(String dateFormatted, SimpleDateFormat dateFormat, boolean useUtc) {
    Date date = null;//from   w w  w  . j av  a2  s .  com
    if (!dateFormatted.isEmpty()) {
        try {
            if (useUtc) {
                dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            }
            date = dateFormat.parse(dateFormatted);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Error parsing the dateFormatted: " + dateFormatted + " pattern: " + dateFormat.toPattern(),
                    e);
        }
    }
    return date;
}

From source file:net.solarnetwork.node.reactor.io.json.JsonReactorSerializationService.java

private static ObjectMapper defaultObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    mapper.setDateFormat(sdf);/* ww  w .  j av a2  s.c om*/
    return mapper;
}

From source file:com.clustercontrol.jobmanagement.util.TimeToANYhourConverter.java

public static boolean main() {
    String a = "2009/04/19 12:23:01";
    String b = "2009/04/22 12:23:00";
    String c = "2009/05/20 12:23:00";
    String d = "2010/04/20 12:23:00";
    String e = "2009/04/19 12:24:00";
    String f = "2009/04/19 13:23:00";
    String g = "2009/04/19 12:24:01";
    String h = "2009/04/19 13:23:01";
    String i = "2009/04/19 12:23:00";
    SimpleDateFormat DFYS = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    DFYS.setTimeZone(TimezoneUtil.getTimeZone());
    Date ta = null;//from  w  w w  .  j a  va  2  s.co m
    Date tb = null;
    Date tc = null;
    Date td = null;
    Date te = null;
    Date tf = null;
    Date tg = null;
    Date th = null;
    Date ti = null;
    try {
        ta = DFYS.parse(a);
        tb = DFYS.parse(b);
        tc = DFYS.parse(c);
        td = DFYS.parse(d);
        te = DFYS.parse(e);
        tf = DFYS.parse(f);
        tg = DFYS.parse(g);
        th = DFYS.parse(h);
        ti = DFYS.parse(i);
    } catch (java.text.ParseException z) {
        m_log.debug(z.getMessage(), z);
        throw new InternalError(z.getMessage());
    }
    long s = ta.getTime();
    long e1 = tb.getTime();
    long e2 = tc.getTime();
    long e3 = td.getTime();
    long e4 = te.getTime();
    long e5 = tf.getTime();
    long e6 = tg.getTime();
    long e7 = th.getTime();
    long e8 = ti.getTime();
    m_log.info("from date = : " + a);
    m_log.info("test case 1 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + b);
    m_log.info("diffTime   = : " + toDiffTime(s, e1));
    m_log.info("test case 2 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + c);
    m_log.info("diffTime   = : " + toDiffTime(s, e2));
    m_log.info("test case 3 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + d);
    m_log.info("diffTime   = : " + toDiffTime(s, e3));
    m_log.info("test case 4 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + e);
    m_log.info("diffTime   = : " + toDiffTime(s, e4));
    m_log.info("test case 5 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + f);
    m_log.info("diffTime   = : " + toDiffTime(s, e5));
    m_log.info("test case 6 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + g);
    m_log.info("diffTime   = : " + toDiffTime(s, e6));
    m_log.info("test case 7 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + h);
    m_log.info("diffTime   = : " + toDiffTime(s, e7));
    m_log.info("test case 8 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + a);
    m_log.info("diffTime   = : " + toDiffTime(s, s));
    m_log.info("test case 9 :>>>>>>>>>>>>");
    m_log.info("to   date = : " + i);
    m_log.info("diffTime   = : " + toDiffTime(s, e8));
    return true;
}

From source file:Main.java

static void initializeXMLReport(int numThreads, int experiment, int sampleInterval, String managerClassName,
        String benchmarkClassName, String adapterClassName) {
    try {//from  w  ww .  j a va 2 s  .  c o m
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = builder.newDocument();

        Element root = doc.createElement("Statistics");
        doc.appendChild(root);

        Element element = doc.createElement("Benchmark");
        element.setTextContent(benchmarkClassName);
        root.appendChild(element);

        element = doc.createElement("Adapter");
        element.setTextContent(adapterClassName);
        root.appendChild(element);

        element = doc.createElement("ContentionManager");
        element.setTextContent(managerClassName);
        root.appendChild(element);

        element = doc.createElement("Threads");
        element.setTextContent(Integer.toString(numThreads));
        root.appendChild(element);

        element = doc.createElement("Mix");
        element.setTextContent(Integer.toString(experiment));
        root.appendChild(element);

        element = doc.createElement("SampleInterval");
        element.setTextContent(Long.toString(sampleInterval));
        root.appendChild(element);

        String name = System.getProperty("user.name");
        if (name == null)
            name = "";
        element = doc.createElement("Owner");
        element.setTextContent(name);
        root.appendChild(element);

        java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getDefault());
        String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(java.util.TimeZone.getDefault());
        element = doc.createElement("Date");
        element.setTextContent(sdf.format(cal.getTime()));
        root.appendChild(element);

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:com.traffitruck.web.JsonController.java

public static Date convertDriveDate(String drivedate) {
    Date driveDateObj = null;/* ww w  .  j  ava2 s .  c  o  m*/
    if (drivedate != null && drivedate.length() > 0) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        try {
            driveDateObj = sdf.parse(drivedate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    return driveDateObj;
}