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:org.apache.falcon.util.DateUtil.java

/**
 * Create a Calendar instance using the specified date and Time zone.
 * @param dateString//from   ww w.  j a  v a2  s. c o  m
 * @param tz : TimeZone
 * @return appropriate Calendar object
 * @throws Exception
 */
public static Calendar getCalendar(String dateString, TimeZone tz) throws Exception {
    Date date = DateUtil.parseDateFalconTZ(dateString);
    Calendar calDate = Calendar.getInstance();
    calDate.setTime(date);
    calDate.setTimeZone(tz);
    return calDate;
}

From source file:org.infoscoop.web.KeywordRankingServlet.java

/**
 * From the handed date and days, we calculate an end date.
 * @param date//www  .  jav  a  2 s. co  m
 * @param rankingPeriod
 * @return
 */
private static String getStartDate(String date, int rankingPeriod) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    if (date.length() == 10) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        cal.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
        cal.set(Calendar.MONTH, Integer.parseInt(date.substring(4, 6)) - 1);
        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6, 8)));
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(date.substring(8, 10)));
        cal.add(Calendar.DAY_OF_YEAR, -(rankingPeriod - 1));
        return sdf.format(cal.getTime());
    }
    throw new IllegalArgumentException("Illegal Date format.");
}

From source file:StringUtils.java

/**
 * Converts a long timestamp to a string in the following format:</br>
 *  hh[timeSeparator]mm[timeSeparator]ss
 *
 * @param timeStamp/*  w w  w  .  j  a  v  a 2 s .  c o m*/
 * @param timeSeparator
 * @return A String containing the formatted timestamp
 */
static public String formatTime(long timeStamp, String timeSeparator) {
    if (timeSeparator == null)
        timeSeparator = "";
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getDefault()); //use system time zone
    cal.setTime(new Date(timeStamp)); //set the time      
    //Format the string:
    StringBuffer bff = new StringBuffer();
    //  Time (hh[timeSeparator]mm[timeSeparator]ss)
    bff.append(
            StringUtils.padWithLeadingZeros(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)), 2) + timeSeparator);
    bff.append(StringUtils.padWithLeadingZeros(String.valueOf(cal.get(Calendar.MINUTE)), 2) + timeSeparator);
    bff.append(StringUtils.padWithLeadingZeros(String.valueOf(cal.get(Calendar.SECOND)), 2));
    //return the result
    return bff.toString();
}

From source file:DateUtility.java

/**
 * We convert the date of the W3CDTF form into java.util.Date type.
 * // ww w  .ja  v  a 2 s .c o m
 * @param W3CDTFDate
 * @return
 */
public static Date getDateFromW3CDTF(String w3cDTFDate) {
    if (w3cDTFDate == null || w3cDTFDate.length() == 0)
        return null;

    try {
        int year = Integer.parseInt(w3cDTFDate.substring(0, 4));
        int month = Integer.parseInt(w3cDTFDate.substring(5, 7));
        int day = Integer.parseInt(w3cDTFDate.substring(8, 10));
        int hour = Integer.parseInt(w3cDTFDate.substring(11, 13));
        int minute = Integer.parseInt(w3cDTFDate.substring(14, 16));
        int second = Integer.parseInt(w3cDTFDate.substring(17, 19));
        String zone = "GMT" + w3cDTFDate.substring(19);
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day, hour, minute, second);
        cal.set(Calendar.MILLISECOND, 0);
        cal.setTimeZone(TimeZone.getTimeZone(zone));
        return cal.getTime();
    } catch (Exception e) {
        System.out.println(w3cDTFDate + " is invalid date string.");
    }

    return null;
}

From source file:org.infoscoop.util.DateUtility.java

/**
 * We convert the date of the W3CDTF form into java.util.Date type.
 * //from   www .j a  va  2 s.  c o  m
 * @param W3CDTFDate
 * @return
 */
public static Date getDateFromW3CDTF(String w3cDTFDate) {
    if (w3cDTFDate == null || w3cDTFDate.length() == 0)
        return null;

    try {
        int year = Integer.parseInt(w3cDTFDate.substring(0, 4));
        int month = Integer.parseInt(w3cDTFDate.substring(5, 7));
        int day = Integer.parseInt(w3cDTFDate.substring(8, 10));
        int hour = Integer.parseInt(w3cDTFDate.substring(11, 13));
        int minute = Integer.parseInt(w3cDTFDate.substring(14, 16));
        int second = Integer.parseInt(w3cDTFDate.substring(17, 19));
        String zone = "GMT" + w3cDTFDate.substring(19);
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day, hour, minute, second);
        cal.set(Calendar.MILLISECOND, 0);
        cal.setTimeZone(TimeZone.getTimeZone(zone));
        return cal.getTime();
    } catch (Exception e) {
        log.error(w3cDTFDate + " is invalid date string.", e);
    }

    return null;
}

From source file:StringUtils.java

/**
 * Converts a long timestamp to a string in the following format:</br>
 *  YYYY[dateSeparator]MM[dateSeparator]DD[dateTimeSeparator]hh[timeSeparator]mm[timeSeparator]ss
 *
 * @param timeStamp/*from  w  ww. j a  v a2s  .co m*/
 * @param dateSeparator
 * @param timeSeparator
 * @param dateTimeSeparator
 * @return A String containing the formatted timestamp
 */
static public String formatDateTime(long timeStamp, String dateSeparator, String timeSeparator,
        String dateTimeSeparator) {
    if (dateSeparator == null)
        dateSeparator = "";
    if (dateTimeSeparator == null)
        dateTimeSeparator = "";
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getDefault()); //use system time zone
    cal.setTime(new Date(timeStamp)); //set the time      
    //Format the string:
    StringBuffer bff = new StringBuffer();
    //   Date (YYYY[dateSeparator]MM[dateSeparator]DD)
    bff.append(StringUtils.padWithLeadingZeros(String.valueOf(cal.get(Calendar.YEAR)), 4) + dateSeparator);
    bff.append(StringUtils.padWithLeadingZeros(String.valueOf(cal.get(Calendar.MONTH) + 1), 2) + dateSeparator);
    bff.append(StringUtils.padWithLeadingZeros(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)), 2));
    //  [dateTimeSeparator]
    bff.append(dateTimeSeparator);
    //   Time (hh[timeSeparator]mm[timeSeparator]ss)
    bff.append(formatTime(timeStamp, timeSeparator));
    //return the result
    return bff.toString();
}

From source file:api.util.JsonUtil.java

public static Date asDate(Object val) {
    if (val == JSONObject.NULL)
        return null;
    if (val instanceof String) {
        String stringValue = (String) val;
        try {/*from   w  ww.j a  v a2 s . c  om*/
            // try ISO 8601
            Calendar cal = DatatypeConverter.parseDateTime(stringValue);
            if (!iso8601Timezone.matcher(stringValue).matches()) {
                // timezone is absent from input string. Assume UTC
                cal.setTimeZone(TimeZone.getTimeZone("UTC"));
            }
            return cal.getTime();
        } catch (IllegalArgumentException iae) {
            // fallback to RFC 2822
            for (String format : new String[] { "EEE, dd MMM yyyy HH:mm:ss Z", "dd MMM yyyy HH:mm:ss Z",
                    "EEE, dd MMM yyyy HH:mm Z", "dd MMM yyyy HH:mm Z", "EEE, dd MMM yyyy HH:mm:ss",
                    "dd MMM yyyy HH:mm:ss", "EEE, dd MMM yyyy HH:mm", "dd MMM yyyy HH:mm" }) {
                SimpleDateFormat rfc2822Fmt = new SimpleDateFormat(format, Locale.ENGLISH);
                rfc2822Fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
                try {
                    return rfc2822Fmt.parse(stringValue);
                } catch (ParseException e) {
                }
            }
        }
    } else if (val instanceof Number) {
        return new Date(((Number) val).longValue() * 1000);
    }
    throw new IllegalArgumentException(
            "Value is expected to be a unix timestamp or a string in either ISO 8601 or RFC 2822 format: "
                    + val);
}

From source file:org.castor.jdo.engine.SQLTypeInfos.java

/**
 * Get a Calendar instance for current thread.
 * //from   w w  w.j  a v a2  s .  c om
 * @return A Calendar instance for current thread.
 */
private static Calendar getCalendar() {
    // We have to reset the time zone each time in case the result set
    // implementation changes it.
    Calendar calendar = THREAD_SAFE_CALENDAR.get();
    calendar.setTimeZone(TIME_ZONE);
    return calendar;
}

From source file:com.mycompany.craftdemo.utility.java

public static boolean isValidTime() {
    Calendar calendar = Calendar.getInstance();
    TimeZone fromTimeZone = calendar.getTimeZone();
    TimeZone toTimeZone = TimeZone.getTimeZone("EST");

    calendar.setTimeZone(fromTimeZone);
    calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
    if (fromTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
    }/*  w ww.j av  a 2  s.  co  m*/

    calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
    if (toTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
    }

    int hour = calendar.get(Calendar.HOUR_OF_DAY);

    return hour >= 9 && hour <= 16;
}

From source file:Main.java

/**
 * Takes a calendar object representing a date/time, ignores its current time zone (which should be the default time zone)
 * applies that date/time to the sourceTimeZone and returns the relative date/time in the current time zone. 
 * // ww  w .ja  va2s.c  o  m
 * For example, given an input of 13:00 EST and source time zone PST, it will return 16:00 EST 
 * 13:00 EST = 18:00 GMT = 10:00 PST
 *  
 * @param calendar
 * @param sourceTimeZone
 * @return
 */
public static Calendar convertToTimeZone(Calendar calendar, TimeZone sourceTimeZone) {
    Calendar result = Calendar.getInstance();
    // i.e., 13:00 EST becomes 08:00 GMT
    long originalTimeInUtc = calendar.getTimeInMillis()
            + calendar.getTimeZone().getOffset(calendar.getTimeInMillis());
    // 08:00 GMT becomes 16:00 PST
    long sourceTime = originalTimeInUtc - sourceTimeZone.getOffset(originalTimeInUtc);
    result.setTimeZone(sourceTimeZone);
    result.setTimeInMillis(sourceTime);
    /*
    Log.d(TAG, "Converting "+DEBUG_CALENDAR_OUTPUT.format(new Date(calendar.getTimeInMillis()))
        +" in ["+sourceTimeZone.getDisplayName()+"] to ["+TimeZone.getDefault().getDisplayName()
        +"] resulting in "+DEBUG_CALENDAR_OUTPUT_WITH_TIMEZONE.format(new Date(result.getTimeInMillis())));
    Log.d(TAG, "Original time in UTC = "+DEBUG_CALENDAR_OUTPUT.format(new Date(originalTimeInUtc)));
    Log.d(TAG, "Original time in source time zone = "+DEBUG_CALENDAR_OUTPUT.format(new Date(sourceTime)));
     */
    return result;
}