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

public static Date getDateFromString(String format, String dateString) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    return sdf.parse(dateString);
}

From source file:Main.java

public static String StampToyyyyMMddHHmmss(long stamp) {

    Date date = null;//from w  w  w .jav 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

public static String convertToHumanReadableTime(Date givenDate, long currentTimeLong) {

    Calendar currentTime = Calendar.getInstance();
    currentTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    currentTime.setTimeInMillis(currentTimeLong);

    Calendar givenTime = Calendar.getInstance();
    givenTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    givenTime.setTime(givenDate);//from w ww.  j a v  a2s . c om

    // Step 1: To see if time difference is less than 24 hours of not
    long timeDiff = currentTime.getTimeInMillis() - givenTime.getTimeInMillis();
    if (timeDiff <= 0) {
        return "Now";
    }

    String humanString = null;
    // Checking if timeDiff is less than 24 or not
    if ((timeDiff / TIME_DAY) >= 1) {
        // days
        int days = (int) (timeDiff / TIME_DAY);
        humanString = String.format(Locale.getDefault(), "%dd", days);
    } else {
        // checking if greater than hour
        if ((timeDiff / TIME_HOUR) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dh", (timeDiff / TIME_HOUR));
        } else if ((timeDiff / TIME_MINUTE) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dm", (timeDiff / TIME_MINUTE));
        } else {
            humanString = String.format(Locale.getDefault(), "%ds", (timeDiff / TIME_MILLIS));
        }
    }

    return humanString;
}

From source file:Main.java

public static String getGMTDate(Date date) {
    if (date == null) {
        return null;
    }//from  www . j  a v  a  2s .com
    DateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    String dateStr = dateFormat.format(date);
    return dateStr;
}

From source file:Main.java

public static int StampToDateInt(long stamp) {
    int date = 0;

    Date datetime = null;/*from   ww w. j ava  2 s .c  om*/
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(stamp);
    datetime = calendar.getTime();

    String format = STAMP_TO_DATE_INT;
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_PRC));

    try {
        date = Integer.parseInt(sdf.format(datetime));
    } catch (Exception e) {
        e.printStackTrace();
    }
    //Log.i("StampHelper", "date_int:"+date);
    return date;
}

From source file:Main.java

public static DateFormat getAuthFormat() {
    if (mAuthFormat == null) {
        mAuthFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'EST' yyyy");
        mAuthFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    }/*from  w  w  w  .j  a v  a 2 s.com*/
    return mAuthFormat;
}

From source file:Main.java

/**
 * getStringAsDate//w ww  . j  a va 2  s .c  o  m
 *
 * @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 String getTime(long millisecond, String format) {
    Date date = new Date(millisecond);
    String time = "";
    if (date != null) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        df.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        time = df.format(date);// w  w w .  java  2  s . c o m
    }
    return time;
}

From source file:Main.java

public static Date getDateFromString(String dateString) {

    DateFormat inputFormat = null;

    if (dateString == null) {
        return null;
    }/*w  ww.  j a  va 2s.c o m*/

    if (dateString.length() == 19)
        inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    if (dateString.length() == 10)
        inputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

    if (inputFormat == null) {
        return null;
    }

    inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date parsed = null;
    try {
        parsed = inputFormat.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return parsed;
}

From source file:Main.java

@SuppressWarnings("unused")
private static int getTimeZoneRawOffset(String timeZoneId) {
    return TimeZone.getTimeZone(timeZoneId).getRawOffset();
}