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 Calendar getCalendar() {
    return Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"), Locale.CHINA);
}

From source file:Main.java

private static void initFormatter() {
    if (mIso8601WithMillisFormat == null) {
        mIso8601WithMillisFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        mIso8601WithMillisFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        mIso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        mIso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
    }/*from  w w  w .  java2s. c  o m*/
}

From source file:Main.java

public static Date parse(String dateFormatted, SimpleDateFormat dateFormat, boolean useUtc) {
    Date date = null;/*from w ww  . j  a v a 2 s. c o m*/
    if (!dateFormatted.isEmpty()) {
        try {
            if (useUtc) {
                dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            }
            date = dateFormat.parse(dateFormatted);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Error parsing the dateFormatted: " + dateFormatted + " pattern: " + dateFormat.toPattern(),
                    e);
        }
    }
    return date;
}

From source file:Main.java

private static DateTime getDateFromISO(String ISOString) {
    //DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
    DateTimeFormatter parser = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
    try {/*  ww w . j  a  v a2s  .  c om*/
        return parser.parseDateTime(ISOString);
    } catch (Throwable e) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
            df.setTimeZone(TimeZone.getTimeZone("UTC"));
            return new DateTime(df.parse(ISOString));
        } catch (Throwable ignored) {
            Log.d("ERROR", "Failed parsing string into date");
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Calculate the Julian Day for a given date using the following formula:
 * JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9)
 *      + D + 1721013.5 + UT/24//from  www .  ja  v a 2 s .c  om
 *
 * Note that this is only valid for the year range 1900 - 2099.
 */
public static double calculateJulianDay(Date date) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);

    double hour = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) / 60.0f
            + cal.get(Calendar.SECOND) / 3600.0f;

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);

    double jd = 367.0 * year - Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0)
            + Math.floor(275.0 * month / 9.0) + day + 1721013.5 + hour / 24.0;
    return jd;
}

From source file:Main.java

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

From source file:Main.java

public static TimeZone getSpTimeZone() {
    TimeZone result = TimeZone.getTimeZone("Europe/Paris");
    return result;
}

From source file:Main.java

public static String StampToString(long stamp) {
    /*/*from ww w  .java 2s. c o m*/
     String[] formats = new String[] {  
     "yyyy-MM-dd",   
     "yyyy-MM-dd HH:mm",  
     "yyyy-MM-dd HH:mmZ",   
     "yyyy-MM-dd HH:mm:ss.SSSZ",
     "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
     }; 
     */
    Date date = null;
    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.SSSZ";
    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 java.util.Calendar getCalendarFromStringDate(String dateString) {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    format.setTimeZone(TimeZone.getTimeZone("ETC/UTC"));

    try {//from ww w . java 2  s.c  o  m
        Date date = format.parse(dateString);
        // System.out.println("Date ->" + date);
        calendar.setTime(date);
        return calendar;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("unused")
public static String iso08601ToString(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz", Locale.getDefault());
    TimeZone timeZone = TimeZone.getTimeZone("UTC");
    sdf.setTimeZone(timeZone);/* ww  w .ja  va 2  s . c om*/
    String output = sdf.format(date);
    int insetFirst = 9;
    int insetLast = 6;
    String retval = output.substring(0, output.length() - insetFirst)
            + output.substring(output.length() - insetLast, output.length());
    return retval.replaceAll("UTC", "+00:00");
}