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

/**
 * Try to parse input with SimpleDateFormat
 *
 * @param input input.//from  w ww .j  av a  2  s .co  m
 * @param format      SimpleDateFormat
 * @param setYear1700 When true the age will be not displayed in brackets
 * @param locale locale.
 * @return Date object if successful, otherwise null
 */
private static Date parseStringWithSimpleDateFormat(String input, String format, boolean setYear1700,
        Locale locale) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
    dateFormat.setTimeZone(TimeZone.getDefault());

    try {
        Date parsedDate = dateFormat.parse(input);

        /*
         * Because no year is defined in address book, set year to 1700
         *
         * When year < 1800, the age will be not displayed in brackets
         */
        if (setYear1700) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(parsedDate);
            cal.set(Calendar.YEAR, 1700);
        }

        return parsedDate;
    } catch (ParseException ignored) {
        return null;
    }
}

From source file:Main.java

/**
 * uses format "yyyy-MM-dd:hh'h'mm'm'ss's'SSS'Z'"
 * @param cal/*  ww  w.  j av  a 2 s .c o m*/
 * @return
 */
public static String getStringDate(Calendar cal) {
    SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd:hh'h'mm'm'ss's'SSS'Z'"); /*set the date format*/
    dFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    return dFormat.format(cal.getTime());
}

From source file:Main.java

public static SimpleDateFormat getServerDateTimeInTimezone(String timezone) {

    SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy");
    if (timezone == null || timezone.isEmpty()) {
        format.setTimeZone(TimeZone.getDefault());
    } else {/*from   ww w. j a v a2 s . c om*/
        format.setTimeZone(TimeZone.getTimeZone("GMT+" + timezone));
    }
    return format;
}

From source file:Main.java

public static String StampToyyyyMMdd(long stamp) {

    Date date = null;//  w  w w  .ja  v  a2  s .c om
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(stamp);
    date = calendar.getTime();

    String format = "yyyy-MM-dd";
    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 SimpleDateFormat getStartDateTimeInTimezone(String timezone) {
    SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, h:mm a");
    if (timezone == null || timezone.isEmpty()) {
        format.setTimeZone(TimeZone.getDefault());
    } else//from  ww  w.j a v a  2s. co m
        format.setTimeZone(TimeZone.getTimeZone("GMT+" + timezone));
    return format;
}

From source file:Main.java

public static DateFormat createDateFormat(String pattern, TimeZone timeZone, boolean strict) {
    if (pattern == null) {
        String msg = "The argument 'pattern' should not be null!";
        throw new IllegalArgumentException(msg);
    }/*from  w ww .ja v  a2  s.c  om*/
    final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    if (timeZone != null) {
        sdf.setTimeZone(timeZone);
    }
    sdf.setLenient(!strict);
    return sdf;
}

From source file:Main.java

public static String StampToStringMMddHHmm(long stamp) {

    Date date = null;/*from  www.  j  a  v  a  2s. c  o  m*/
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(stamp);
    date = calendar.getTime();

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

    return sdf.format(date);

}

From source file:net.portalblockz.portalbot.Utils.java

public static String getDate() {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    return sdf.format(calendar.getTime());
}

From source file:Main.java

public static String StampToyyyyMMddHHmmss(long stamp) {

    Date date = null;/*from w w w  .  j av a  2  s.co  m*/
    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"; 
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    //sdf.setTimeZone(TimeZone.getTimeZone("UTC"));   
    sdf.setTimeZone(TimeZone.getTimeZone("PRC"));

    String time = sdf.format(date);
    return time.substring(0, "yyyy-MM-dd HH:mm:ss".length());

}

From source file:Main.java

/**
 * Parse Date by provided pattern along with target Time Zone.
 * Pattern Reference: http://developer.android.com/reference/java/text/SimpleDateFormat.html
 *
 * @param dateString String//from  ww  w . j ava 2s  .  c  om
 * @param pattern String
 * @param targetTimeZone TimeZone
 * @return java.util.Date which represents based on provided dateString and pattern
 * @throws ParseException ParseException
 */
public static Date parseDate(String dateString, String pattern, TimeZone targetTimeZone) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    if (targetTimeZone != null)
        simpleDateFormat.setTimeZone(targetTimeZone);
    return simpleDateFormat.parse(dateString);
}