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 convertCurrentTimeToGmt(String format) {
    final SimpleDateFormat dateFormatGmt = new SimpleDateFormat(format, Locale.getDefault());
    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    return dateFormatGmt.format(new Date());
}

From source file:Main.java

public static String getPastTime(long seconds) {
    SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
    gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+4"));
    String s = gmtDateFormat.format(new Date(System.currentTimeMillis() - seconds * 1000));
    //Current Date Time in GMT
    System.out.println("Current Date and Time in GMT time zone: " + s);
    return s;/* w w  w  .  j a  v  a  2 s.com*/
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
static String setTime() {
    // add DateTime to filename
    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    Date currentLocalTime = cal.getTime();
    SimpleDateFormat date = new SimpleDateFormat("HH-mm-ss");
    date.setTimeZone(TimeZone.getDefault());
    _currentTime = date.format(currentLocalTime);

    return _currentTime;
}

From source file:Main.java

public static String calcUntil(Calendar untilDate, String granularity) {
    String pattern = "";
    if (granularity.equals("YYYY-MM-DDThh:mm:ssZ")) {
        untilDate.add(Calendar.SECOND, -1);
        pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    } else {//w  w  w.ja va2 s  .  c  o m
        untilDate.add(Calendar.DATE, -1);
        pattern = "yyyy-MM-dd";
    }

    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    return sdf.format(untilDate.getTime());

}

From source file:Main.java

public static String formatTime(final long totalSeconds, final int timer) {
    if (timer == 0) {
        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        return sdf.format(new Date(totalSeconds * 1000));
    } else {/*from  w ww .j  a  v  a 2  s .  c o m*/

        String seconds = Integer.toString((int) (totalSeconds % 60));
        String minutes = Integer.toString((int) (totalSeconds / 60));
        if (seconds.length() < 2) {
            seconds = "0" + seconds;
        }
        if (minutes.length() < 2) {
            minutes = "0" + minutes;
        }
        return minutes + ":" + seconds;
    }
}

From source file:Main.java

/**
 * As there is no easy constructor for making a SimpleDateFormat specifying
 * both a Locale and Timezone a utility is provided here
 * //  w w  w  . j  av  a 2s . com
 * @param pattern
 * @param locale
 * @param timezone
 * @return
 */
public static SimpleDateFormat newSimpleDateFormat(String pattern, Locale locale, TimeZone timezone) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
    sdf.setTimeZone(timezone);
    return sdf;
}

From source file:Main.java

public static Date getDateFromFormattedStringInGMT(String str) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date date = null;/* ww  w .j a  v  a  2  s  .  c  o m*/
    try {
        date = simpleDateFormat.parse(str);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

From source file:Main.java

/**
 * Convert Date to String - For Audio Record FileName ONLY
 * @param date//from  www.  j a va2 s .co m
 * @param format
 * @return
 */
public static String convertDateToStrForAudioFileName() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
    String dateStr = dateFormat.format(new Date());
    return dateStr;
}

From source file:Main.java

/**
 * Convert Date to String in HK TimeZone
 * @param date//from  w  w  w .  j a  v a  2s .  c  om
 * @param format Format of the output string (e.g. "yyyy-MM-dd HH:mm:ss", "HH:mm:ss")
 * @return output string
 */
public static String convertDateToStr(Date date, String format) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.ENGLISH);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
    String dateStr = "";
    if (date != null) {
        dateStr = dateFormat.format(date);
    } else {
        dateStr = dateFormat.format(new Date());
    }
    return dateStr;
}

From source file:Main.java

public static String StampToString(long stamp) {
    /*/*from   w ww  .  j a v a  2s.  co 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 = STAMP_TO_STRING;
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_PRC));

    return sdf.format(date);

}