List of usage examples for java.util TimeZone getTimeZone
public static TimeZone getTimeZone(ZoneId zoneId)
From source file:Main.java
/** * Returns current unix date in GMT as long value * * @return current date as long//ww w . j a v a2s . c o m */ public static long getCurrentLongDate() { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); return cal.getTime().getTime(); }
From source file:Main.java
/** * Convert to String a date that is received as parameter. * * @param date the date/*from ww w . jav a 2 s. co m*/ * @return String */ public static String toStringDate(Date date) { String formatoFecha = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; SimpleDateFormat sdf = new SimpleDateFormat(formatoFecha); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); return date == null ? null : URLEncoder.encode(sdf.format(date)); }
From source file:Main.java
/** * Convert the given Julian Day to Gregorian Date (in UT time zone). * Based on the formula given in the Explanitory Supplement to the * Astronomical Almanac, pg 604.//w ww . j a v a2 s . c o m */ public static Date calculateGregorianDate(double jd) { int l = (int) jd + 68569; int n = (4 * l) / 146097; l = l - (146097 * n + 3) / 4; int i = (4000 * (l + 1)) / 1461001; l = l - (1461 * i) / 4 + 31; int j = (80 * l) / 2447; int d = l - (2447 * j) / 80; l = j / 11; int m = j + 2 - 12 * l; int y = 100 * (n - 49) + i + l; double fraction = jd - Math.floor(jd); double dHours = fraction * 24.0; int hours = (int) dHours; double dMinutes = (dHours - hours) * 60.0; int minutes = (int) dMinutes; int seconds = (int) ((dMinutes - minutes) * 60.0); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UT")); cal.set(y, m - 1, d, hours + 12, minutes, seconds); return cal.getTime(); }
From source file:Main.java
public static String StampToString(long stamp) { /*//from w w w . j a va 2s.co 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 = STAMP_TO_STRING; SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_PRC)); return sdf.format(date); }
From source file:Main.java
/** * To date string.// ww w . j ava 2s . c o m * * @param date the date * @return the date */ @SuppressLint("SimpleDateFormat") public static Date toDateString(String date) { String formatoFecha = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; SimpleDateFormat sdf = new SimpleDateFormat(formatoFecha); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); try { return date == null ? null : sdf.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
public static Calendar getCurrentTime() { Calendar utc = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC")); return utc; }
From source file:Main.java
/** * getDateAsString// w w w. j ava 2 s .co m * Convert a date into a string * * @param date the date * @param format the format in which to return the string * @return the new formatted date string */ public static String getDateAsString(Date date, String format, String timezone) { DateFormat formatter = new SimpleDateFormat(format, Locale.getDefault()); if (timezone == null) { formatter.setTimeZone(TimeZone.getDefault()); } else { formatter.setTimeZone(TimeZone.getTimeZone(timezone)); } return formatter.format(date); }
From source file:Main.java
public static int[] getTime(long time, String timeZone) { Calendar c = Calendar.getInstance(); if (timeZone != null && timeZone.length() != 0) { TimeZone tz = TimeZone.getTimeZone(timeZone); c = Calendar.getInstance(tz); }//from w w w .jav a 2 s . co m c.setTimeInMillis(time); int y = c.get(Calendar.YEAR); int m = c.get(Calendar.MONTH); int d = c.get(Calendar.DAY_OF_MONTH); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); return new int[] { y, m, d, hour, minute, second }; }
From source file:Main.java
/** * Format a time in seconds to a HH:MM:SS string. * @param seconds the time in seconds// w w w . jav a 2s.c o m * @return the formatted time */ private static String formatSecondsToHMS(long seconds) { if (null == mHourMinSecFormat) { mHourMinSecFormat = new SimpleDateFormat("HH:mm:ss"); mHourMinSecFormat.setTimeZone(TimeZone.getTimeZone("UTC")); mMinSecFormat = new SimpleDateFormat("mm:ss"); mMinSecFormat.setTimeZone(TimeZone.getTimeZone("UTC")); } if (seconds < 3600) { return mMinSecFormat.format(new Date(seconds * 1000)); } else { return mHourMinSecFormat.format(new Date(seconds * 1000)); } }
From source file:Main.java
public static String getLocalTimeString(String strGMTTime) { String strLocalTime = null;// w w w . java 2 s .com try { GMTTimeFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); Date date = GMTTimeFormatter.parse(strGMTTime); LocalTimeFormatter.setTimeZone(TimeZone.getDefault()); strLocalTime = LocalTimeFormatter.format(date); } catch (ParseException e) { e.printStackTrace(); } return strLocalTime; }