Example usage for java.util Calendar setTimeZone

List of usage examples for java.util Calendar setTimeZone

Introduction

In this page you can find the example usage for java.util Calendar setTimeZone.

Prototype

public void setTimeZone(TimeZone value) 

Source Link

Document

Sets the time zone with the given time zone value.

Usage

From source file:Main.java

public static Calendar parseCalendarString(String dateTimeString) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = null;/*  w w w .  j  a  v a  2s  .c om*/
    try {
        date = dateFormat.parse(dateTimeString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
    cal.setTime(date);
    return cal;
}

From source file:Main.java

public static Long getToday() {
    Calendar today = Calendar.getInstance();
    today.setTimeZone(TimeZone.getDefault());

    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.HOUR_OF_DAY, 1);
    today.set(Calendar.MILLISECOND, 0);

    // FOR DEBUG//  w  w w  .j a v a 2s.  c o  m
    //today.set(Calendar.DAY_OF_YEAR, today.get(Calendar.DAY_OF_YEAR) + 1);

    return today.getTimeInMillis();
}

From source file:Main.java

public static Long getYesterday() {
    Calendar yesterday = Calendar.getInstance();
    yesterday.setTimeZone(TimeZone.getDefault());

    yesterday.set(Calendar.SECOND, 0);
    yesterday.set(Calendar.MINUTE, 0);
    yesterday.set(Calendar.HOUR_OF_DAY, 1);
    yesterday.set(Calendar.MILLISECOND, 0);

    int day = yesterday.get(Calendar.DAY_OF_YEAR);

    yesterday.set(Calendar.DAY_OF_YEAR, day - 1);

    return yesterday.getTimeInMillis();
}

From source file:Main.java

/**
 * Gets Date with UTC time zone/* w w w  .j av  a 2 s .  c  o  m*/
 *
 * @param date is concrete date
 * @return new instance calendar
 */
public static Calendar getCalendarUTC(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    return calendar;
}

From source file:Main.java

public static Date getTimeFrom(int year, int month, int day, int hour, int minute, int second) {
    Calendar calendar = new GregorianCalendar(year, month, day, hour, minute, second);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    return calendar.getTime();
}

From source file:Main.java

/**
 *Converts a dateTimeString and makes a Calendar object
 * @param dateTimeString in format 2012-10-15T08:17:00
 * @return Calendar object/*from   w  w w  .j av a  2s . c o m*/
 * */
public static Calendar parseCalendarString(String dateTimeString) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = null;
    try {
        date = dateFormat.parse(dateTimeString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
    cal.setTime(date);
    return cal;
}

From source file:Main.java

public static final String getStringTimeFromLong(final long timeInMillis) {

    final SimpleDateFormat format = new SimpleDateFormat("dd.MM.yy HH:mm");
    final Calendar c = Calendar.getInstance();
    c.setTimeInMillis(timeInMillis);//from w ww .  j ava  2  s .  c  o  m
    c.setTimeZone(TimeZone.getDefault());
    return format.format(c.getTime());
}

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  .ja  v  a 2s.c  o m*/

    // 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 getMonth(final Date date) {
    if (date != null) {
        Calendar calendar = Calendar.getInstance();
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
        calendar.setTimeZone(timeZone);
        calendar.setTime(date);/*  w ww  . j a v a 2  s  . com*/
        return String.valueOf(calendar.get(Calendar.MONTH) + 1);
    }
    return "0";
}

From source file:Main.java

/**
 * Returns a ISO 8601 representation of the given date. This method 
 * is thread safe and non-blocking./* ww w.  j  a  v  a 2  s  . c o  m*/
 *
 * @see <a href="https://issues.apache.org/jira/browse/TIKA-495">TIKA-495</a>
 * @param date given date
 * @return ISO 8601 date string, including timezone details
 */
public static String formatDate(Calendar date) {
    // Explicitly switch it into UTC before formatting 
    date.setTimeZone(UTC);
    return doFormatDate(date);
}