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

/**
 * uses format "yyyy-MM-dd:hh'h'mm'm'ss's'SSS'Z'"
 * @param cal// w w  w . ja v 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

/**
 *Converts a dateTimeString and makes a Calendar object
 * @param dateTimeString in format 2012-10-15T08:17:00
 * @return Calendar object//from www  . j  a  v  a 2  s .  c om
 * */
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:com.intuit.tank.util.TimeFormatUtil.java

public static String formatTime(int numSeconds) {
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    c.setTimeInMillis(0);//w w w .  j a v a 2 s .com
    c.add(Calendar.SECOND, numSeconds);
    return DF.format(c);
}

From source file:io.lavagna.common.Json.java

public static String formatDate(Date date) {
    return DateFormatUtils.format(date, Constants.DATE_FORMAT, TimeZone.getTimeZone("Z"));
}

From source file:Main.java

/**
 * Converts an XMLGregorianCalendar to a Date.
 *
 * @param xmlDate/* w ww. j a v  a 2  s  .c  o  m*/
 *            XMLGregorianCalendar to convert.
 * @return corresponding date object.
 */
public static Date getDate(final XMLGregorianCalendar xmlDate) {
    // TODO: is this equivalent to getDate(String) processing above??

    // start with UTC, i.e. no daylight savings time.
    TimeZone timezone = TimeZone.getTimeZone("GMT");

    // adjust timezone to match xmldate
    int offsetMinutes = xmlDate.getTimezone();
    if (offsetMinutes != DatatypeConstants.FIELD_UNDEFINED) {
        timezone.setRawOffset(
                // convert minutes to milliseconds
                offsetMinutes * 60 // seconds per minute
                        * 1000 // milliseconds per second
        );
    }

    // use calendar so parsed date will be UTC
    Calendar calendar = Calendar.getInstance(timezone);
    calendar.clear();
    calendar.set(xmlDate.getYear(),
            // xmlcalendar is 1 based, calender is 0 based
            xmlDate.getMonth() - 1, xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(),
            xmlDate.getSecond());
    Date date = calendar.getTime();
    int millis = xmlDate.getMillisecond();
    if (millis != DatatypeConstants.FIELD_UNDEFINED) {
        calendar.setTimeInMillis(calendar.getTimeInMillis() + millis);
    }

    return date;
}

From source file:Main.java

public static String calcFrom(String fromDate, String granularity) throws ParseException {
    String pattern = "";
    if (granularity.equals("YYYY-MM-DDThh:mm:ssZ")) {
        pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    } else {/*www  .  j a v a  2  s. c  o  m*/
        pattern = "yyyy-MM-dd";
    }

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

    return sdf.format(sdf.parse(fromDate));

}

From source file:edu.illinois.cs.cogcomp.temporal.normalizer.main.timex2interval.TimeOfDay.java

/**
 * This function convert a phrase with time zone
 * @param phrase//  w  w  w . j  a  v  a 2 s  . com
 * @return
 */
public static String converter(String phrase) {
    Set<String> zoneIds = DateTimeZone.getAvailableIDs();

    for (String zoneId : zoneIds) {
        String longName = TimeZone.getTimeZone(zoneId).getDisplayName();
        phrase = phrase.replace(longName.toLowerCase(), "");
        String[] zoneList = zoneId.split("/");
        // Because we have zone like UTC/GMT+1
        for (String zone : zoneList) {
            phrase = phrase.replace(zone.toLowerCase(), "");
        }
    }
    // EST wasn't in the canonical ID list in Joda time
    phrase = phrase.replace("est", "");
    phrase = phrase.trim().replaceAll(" +", " ");
    return phrase;
}

From source file:DateParser.java

private static Calendar getCalendar(String isodate) {
    // YYYY-MM-DDThh:mm:ss.sTZD
    StringTokenizer st = new StringTokenizer(isodate, "-T:.+Z", true);

    Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.clear();//from w  w  w .  j a  v a2 s.  c  om
    try {
        // Year
        if (st.hasMoreTokens()) {
            int year = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.YEAR, year);
        } else {
            return calendar;
        }
        // Month
        if (check(st, "-") && (st.hasMoreTokens())) {
            int month = Integer.parseInt(st.nextToken()) - 1;
            calendar.set(Calendar.MONTH, month);
        } else {
            return calendar;
        }
        // Day
        if (check(st, "-") && (st.hasMoreTokens())) {
            int day = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.DAY_OF_MONTH, day);
        } else {
            return calendar;
        }
        // Hour
        if (check(st, "T") && (st.hasMoreTokens())) {
            int hour = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.HOUR_OF_DAY, hour);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }
        // Minutes
        if (check(st, ":") && (st.hasMoreTokens())) {
            int minutes = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.MINUTE, minutes);
        } else {
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }

        //
        // Not mandatory now
        //

        // Secondes
        if (!st.hasMoreTokens()) {
            return calendar;
        }
        String tok = st.nextToken();
        if (tok.equals(":")) { // secondes
            if (st.hasMoreTokens()) {
                int secondes = Integer.parseInt(st.nextToken());
                calendar.set(Calendar.SECOND, secondes);
                if (!st.hasMoreTokens()) {
                    return calendar;
                }
                // frac sec
                tok = st.nextToken();
                if (tok.equals(".")) {
                    // bug fixed, thx to Martin Bottcher
                    String nt = st.nextToken();
                    while (nt.length() < 3) {
                        nt += "0";
                    }
                    nt = nt.substring(0, 3); // Cut trailing chars..
                    int millisec = Integer.parseInt(nt);
                    // int millisec = Integer.parseInt(st.nextToken()) * 10;
                    calendar.set(Calendar.MILLISECOND, millisec);
                    if (!st.hasMoreTokens()) {
                        return calendar;
                    }
                    tok = st.nextToken();
                } else {
                    calendar.set(Calendar.MILLISECOND, 0);
                }
            } else {
                throw new RuntimeException("No secondes specified");
            }
        } else {
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
        // Timezone
        if (!tok.equals("Z")) { // UTC
            if (!(tok.equals("+") || tok.equals("-"))) {
                throw new RuntimeException("only Z, + or - allowed");
            }
            boolean plus = tok.equals("+");
            if (!st.hasMoreTokens()) {
                throw new RuntimeException("Missing hour field");
            }
            int tzhour = Integer.parseInt(st.nextToken());
            int tzmin = 0;
            if (check(st, ":") && (st.hasMoreTokens())) {
                tzmin = Integer.parseInt(st.nextToken());
            } else {
                throw new RuntimeException("Missing minute field");
            }
            if (plus) {
                calendar.add(Calendar.HOUR, -tzhour);
                calendar.add(Calendar.MINUTE, -tzmin);
            } else {
                calendar.add(Calendar.HOUR, tzhour);
                calendar.add(Calendar.MINUTE, tzmin);
            }
        }
    } catch (NumberFormatException ex) {
        throw new RuntimeException("[" + ex.getMessage() + "] is not an integer");
    }
    return calendar;
}

From source file:Main.java

public static final String dateToString(Date paramDate, String paramString) {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString, Locale.FRANCE);
    localSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
    return localSimpleDateFormat.format(paramDate);
}

From source file:Main.java

/**
 * Returns date before given days//  ww w . j  a v a2  s . co m
 *
 * @param days days to before
 * @return string date string before days
 */
public static String getDateBefore(int days) {
    Date today = new Date();
    Calendar cal = new GregorianCalendar();
    cal.setTime(today);
    cal.add(Calendar.DAY_OF_MONTH, days * -1);
    Date date = cal.getTime();
    SimpleDateFormat gmtFormat = new SimpleDateFormat();
    gmtFormat.applyPattern("yyyy-MM-dd 00:00:00");
    TimeZone gmtTime = TimeZone.getTimeZone("GMT");
    gmtFormat.setTimeZone(gmtTime);
    return gmtFormat.format(date);
}