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

public static String formatISO8601(Date date, String format) {
    String result = "";
    if (date != null) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        dateFormat.setTimeZone(timeZone);
        try {/* w  ww  . j av  a2s  .  c  o m*/
            result = dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }
    return result;
}

From source file:Main.java

/**
 * getStringAsDate/*from   ww  w.ja v a 2s .c om*/
 *
 * @param dateString a string in date format
 * @param format     the resulting date format
 * @return a new date in the specified format
 */
public static Date getStringAsDate(String dateString, String format, String timezone) {
    SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
    if (timezone == null) {
        formatter.setTimeZone(TimeZone.getDefault());
    } else {
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    }
    Date date = new Date();
    try {
        date = formatter.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

public static final Date stringToDate(String paramString1, String paramString2, boolean paramBoolean) {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString2, Locale.getDefault());
    localSimpleDateFormat.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
    try {//  w  ww .j  a va 2  s  .com
        Date localDate = localSimpleDateFormat.parse(paramString1);
        return localDate;
    } catch (Throwable localThrowable) {
        if (!paramBoolean) {
            return new Date(1000L + System.currentTimeMillis());
        }
    }
    return null;
}

From source file:Main.java

public static String StampToString(long stamp) {
    /*//www. j a  v a2s.c o  m
     String[] formats = new String[] {  
     "yyyy-MM-dd",   
     "yyyy-MM-dd HH:mm",  
     "yyyy-MM-dd HH:mmZ",   
     "yyyy-MM-dd HH:mm:ss.SSSZ",
     "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
     }; 
     */
    Date date = null;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(stamp);
    date = calendar.getTime();

    //       String format = "yyyy-MM-dd HH:mm:ss.SSSZ"; 
    String format = "yyyy-MM-dd HH:mm:ss.SSSZ";
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    //sdf.setTimeZone(TimeZone.getTimeZone("UTC"));   
    sdf.setTimeZone(TimeZone.getTimeZone("PRC"));

    return sdf.format(date);

}

From source file:Main.java

public static String getHours(String dateString) {

    SimpleDateFormat parserSDF = new SimpleDateFormat("HH:mm:ss");
    SimpleDateFormat formater = new SimpleDateFormat("HH:mm");

    parserSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
    formater.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = null;//ww  w  .ja v  a2s.c  o m
    try {
        parserSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
        date = parserSDF.parse(dateString);
    } catch (ParseException e) {
        Log.d(TAG, e.getMessage());
    }

    return formater.format(date);
}

From source file:Main.java

public static String getFormattedDate(Long millis, String timeZone) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
    if (!TextUtils.isEmpty(timeZone))
        sdf.setTimeZone(TimeZone.getTimeZone(timeZone));

    return millis != null ? sdf.format(new Date(millis)) : "";
}

From source file:Main.java

/**
 * Format Date by provided pattern along with target Time Zone
 * Pattern Reference: http://developer.android.com/reference/java/text/SimpleDateFormat.html
 * @param date Date//from w  w  w  .  j  a  va 2s  .c om
 * @param pattern String
 * @param targetTimeZone TimeZone
 * @return String - formatted string
 */
public static String formatDate(Date date, String pattern, TimeZone targetTimeZone) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    if (targetTimeZone != null)
        simpleDateFormat.setTimeZone(targetTimeZone);
    return simpleDateFormat.format(date);
}

From source file:Main.java

@SuppressWarnings("unused")
public static String iso08601ToString(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz", Locale.getDefault());
    TimeZone timeZone = TimeZone.getTimeZone("UTC");
    sdf.setTimeZone(timeZone);
    String output = sdf.format(date);
    int insetFirst = 9;
    int insetLast = 6;
    String retval = output.substring(0, output.length() - insetFirst)
            + output.substring(output.length() - insetLast, output.length());
    return retval.replaceAll("UTC", "+00:00");
}

From source file:Main.java

public static Date parseTimestamp(String timestamp) {
    for (SimpleDateFormat format : ACCEPTED_TIMESTAMP_FORMATS) {
        // TODO: We shouldn't be forcing the time zone when parsing dates.
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        try {/*from w w w  .  j  a va  2  s  .  c om*/
            return format.parse(timestamp);
        } catch (ParseException ex) {
            continue;
        }
    }

    // All attempts to parse have failed
    return null;
}

From source file:Main.java

public static String getConvertDate(String dateStr) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

    String dateString = dateStr.replace("Z", "GMT+00:00");
    dateFormat.setTimeZone(TimeZone.getDefault());
    Date date = null;/*from   w  w w .  ja v  a  2s.  c  o m*/
    try {
        date = dateFormat.parse(dateString);
    } catch (java.text.ParseException e1) {
        e1.printStackTrace();
    }

    dateFormat = new SimpleDateFormat("hh:mm a - dd MMM yy");
    String outputText = dateFormat.format(date);

    return outputText;
}