List of usage examples for java.util Calendar getTimeZone
public TimeZone getTimeZone()
From source file:org.ow2.aspirerfid.queryandcapture.ui.MasterDataQueryAndCaptureGui.java
/** * Returns the time zone designator in a ISO6601-compliant format from the * given <code>Calendar</code> value. * /*from w ww . ja v a2s . c o m*/ * @param cal * The Calendar to be formatted. * @return The time zone designator from the given Calendar. */ private static String getTimeZone(final Calendar cal) { StringBuilder buf = new StringBuilder(); TimeZone tz = cal.getTimeZone(); // determine offset of timezone from UTC (incl. daylight saving) int offset = tz.getOffset(cal.getTimeInMillis()); int hours = Math.abs((offset / (60 * 1000)) / 60); int minutes = Math.abs((offset / (60 * 1000)) % 60); buf.append(offset < 0 ? '-' : '+'); buf.append(XX_FORMAT.format(hours)); buf.append(':'); buf.append(XX_FORMAT.format(minutes)); return buf.toString(); }
From source file:ezbake.services.provenance.graph.Utils.java
public static ezbake.base.thrift.DateTime convertDate2DateTime(final java.util.Date theDate) { final Calendar cal = new GregorianCalendar(); cal.setTime(theDate);/* w w w.ja va 2 s . c om*/ // get calendar parts final int year = cal.get(Calendar.YEAR); final int month = cal.get(Calendar.MONTH) + 1; final int day = cal.get(Calendar.DAY_OF_MONTH); final int hour = cal.get(Calendar.HOUR_OF_DAY); final int minute = cal.get(Calendar.MINUTE); final int second = cal.get(Calendar.SECOND); int offsetMinutes = (cal.getTimeZone().getOffset(cal.getTimeInMillis())) / (1000 * 60); // set thrift DateTime propertiesd final ezbake.base.thrift.DateTime dt = new ezbake.base.thrift.DateTime(); // Date final ezbake.base.thrift.Date date = new ezbake.base.thrift.Date(); date.setMonth((short) month).setDay((short) day).setYear((short) year); dt.setDate(date); // Time with TimeZone final ezbake.base.thrift.Time t = new ezbake.base.thrift.Time(); boolean afterUtc = offsetMinutes > 0; offsetMinutes = Math.abs(offsetMinutes); final ezbake.base.thrift.TimeZone tz = new ezbake.base.thrift.TimeZone((short) (offsetMinutes / 60), (short) (offsetMinutes % 60), afterUtc); t.setHour((short) hour).setMinute((short) minute).setSecond((short) second).setTz(tz); dt.setTime(t); return dt; }
From source file:com.krawler.common.timezone.Timezone.java
private static String getDSTSpecificTz(String tzid) { Calendar calTZ = Calendar.getInstance(TimeZone.getTimeZone(tzid)); TimeZone tz = calTZ.getTimeZone(); int miliseconds = tz.getOffset(calTZ.getTimeInMillis()); String time = ""; String signNum = "+"; if (miliseconds < 0) { miliseconds = miliseconds * (-1); signNum = "-"; }/* w w w . j av a 2 s .c o m*/ int seconds = miliseconds / 1000; String min = ""; // Calculating minutes if (seconds > 60) { min = String.valueOf(seconds / 60 % 60); if ((seconds / 60 % 60) < 10) { min = "0" + String.valueOf(seconds / 60 % 60); } if (min.compareTo("0") != 0 && min.compareTo("30") != 0) { // Checking whether the minimum unit of minutes is 30 mins. if (min.compareTo("45") == 0) { min = "30"; } else { min = "00"; } } } else { min = "00"; } String hours = ""; // Calculating hours if (seconds / 60 > 60) { hours = String.valueOf(seconds / 60 / 60); if ((seconds / 60 / 60) < 10) { hours = "0" + String.valueOf(seconds / 60 / 60); } } else { hours = "00"; } time = signNum + hours + ":" + min; return time; }
From source file:com.krawler.common.timezone.Timezone.java
public static String[] getGmtDate(Connection conn, String date1, String userid) throws ServiceException, ParseException { String tzUser = getTimeZone(conn, userid); Calendar calInstance = Calendar.getInstance(); calInstance.setTimeZone(java.util.TimeZone.getTimeZone("GMT" + tzUser)); TimeZone tz = calInstance.getTimeZone(); int temp = tz.getRawOffset(); java.text.SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss"); java.text.SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss"); java.util.Calendar cal0 = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); java.util.Calendar cal1 = Calendar.getInstance(new SimpleTimeZone(temp, tzUser)); format0.setCalendar(cal0);/*w w w .jav a 2s . c om*/ format1.setCalendar(cal1); String dateArr[] = date1.split(" "); if (dateArr.length == 1 || dateArr[1].equals("00:00:00")) { date1 = dateArr[0] + " " + "00:00:01"; } java.util.Date date = format1.parse(date1); String result = format0.format(date); String[] results = result.split(" "); return results; }
From source file:com.krawler.common.timezone.Timezone.java
public static String[] getTzonetoGmt(String date1, String tzUser) throws ServiceException, ParseException { Calendar calInstance = Calendar.getInstance(); calInstance.setTimeZone(java.util.TimeZone.getTimeZone("GMT" + tzUser)); TimeZone tz = calInstance.getTimeZone(); int temp = tz.getRawOffset(); java.text.SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss"); java.text.SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss"); java.util.Calendar cal0 = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); java.util.Calendar cal1 = Calendar.getInstance(new SimpleTimeZone(temp, tzUser)); format0.setCalendar(cal0);/* w w w .j a v a 2s. c om*/ format1.setCalendar(cal1); String dateArr[] = date1.split(" "); if (dateArr[1].equals("00:00:00")) { date1 = dateArr[0] + " " + "00:00:01"; } java.util.Date date = format1.parse(date1); String result = format0.format(date); String[] results = result.split(" "); if (results[1].equals("00:00:00")) { results[1] = "00:00:01"; } return results; }
From source file:org.nuxeo.ecm.core.storage.State.java
@SuppressWarnings("boxing") protected static void toString(StringBuilder buf, Object value) { if (value instanceof String) { String v = (String) value; if (v.length() > DEBUG_MAX_STRING) { v = v.substring(0, DEBUG_MAX_STRING) + "...(" + v.length() + " chars)..."; }/*w w w . jav a 2 s . c o m*/ buf.append(v); } else if (value instanceof Calendar) { Calendar cal = (Calendar) value; char sign; int offset = cal.getTimeZone().getOffset(cal.getTimeInMillis()) / 60000; if (offset < 0) { offset = -offset; sign = '-'; } else { sign = '+'; } buf.append( String.format("Calendar(%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d:%02d)", cal.get(Calendar.YEAR), // cal.get(Calendar.MONTH) + 1, // cal.get(Calendar.DAY_OF_MONTH), // cal.get(Calendar.HOUR_OF_DAY), // cal.get(Calendar.MINUTE), // cal.get(Calendar.SECOND), // cal.get(Calendar.MILLISECOND), // sign, offset / 60, offset % 60)); } else if (value instanceof Object[]) { Object[] v = (Object[]) value; buf.append('['); for (int i = 0; i < v.length; i++) { if (i > 0) { buf.append(','); if (i > DEBUG_MAX_ARRAY) { buf.append("...(" + v.length + " items)..."); break; } } toString(buf, v[i]); } buf.append(']'); } else { buf.append(value); } }
From source file:org.istsms.util.Javabean2JSON.java
private static JSONObject fromJavaCalendar(Calendar obj) { if (obj == null) return null; try {/* w ww .j a v a 2 s . c om*/ JSONObject jo = new JSONObject(); jo.put("__class", Calendar.class.getCanonicalName()); jo.put("__value", "" + obj.getTime().getTime()); jo.put("__timezn", obj.getTimeZone().getID()); return jo; } catch (JSONException e) { e.printStackTrace(); return null; } }
From source file:com.taobao.adfs.database.tdhsocket.client.util.ConvertUtil.java
public static Long getTimeFromString(String stringVal, @Nullable Calendar cal) throws SQLException { if (stringVal == null) { return null; }/* w ww .j ava 2 s . c o m*/ String val = stringVal.trim(); if (val.length() == 0) { return null; } if (val.equals("0") || val.equals("0000-00-00") || val.equals("0000-00-00 00:00:00") || val.equals("00000000000000") || val.equals("0")) { Calendar calendar = null; if (cal != null) { calendar = Calendar.getInstance(cal.getTimeZone()); } else { calendar = Calendar.getInstance(); } calendar.set(Calendar.YEAR, 1); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTimeInMillis(); } DateFormat dateFormat = DateFormat.getDateTimeInstance(); if (cal != null) { TimeZone timeZone = cal.getTimeZone(); dateFormat.setTimeZone(timeZone); } try { return dateFormat.parse(val).getTime(); } catch (ParseException e) { throw new SQLException("Parse date failure:" + val); } }
From source file:com.taobao.tdhs.client.util.ConvertUtil.java
public static Long getTimeFromString(String stringVal, @Nullable Calendar cal) throws SQLException { if (stringVal == null) { return null; }//from w ww . java 2s . c o m String val = stringVal.trim(); if (val.length() == 0) { return null; } if (val.equals("0") || val.equals("0000-00-00") || val.equals("0000-00-00 00:00:00") || val.equals("00000000000000") || val.equals("0")) { Calendar calendar = null; if (cal != null) { calendar = Calendar.getInstance(cal.getTimeZone()); } else { calendar = Calendar.getInstance(); } calendar.set(Calendar.YEAR, 1); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTimeInMillis(); } DateFormat dateFormat; if (val.length() == "yyyy-MM-dd".length()) { dateFormat = new SimpleDateFormat("yyyy-MM-dd"); } else { dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } if (cal != null) { TimeZone timeZone = cal.getTimeZone(); dateFormat.setTimeZone(timeZone); } try { return dateFormat.parse(val).getTime(); } catch (ParseException e) { throw new SQLException("Parse date failure:" + val); } }
From source file:org.forgerock.openam.cts.TokenTestUtils.java
/** * Logic for comparing two tokens. It might be useful to move this to a .equals method at some point. * * @param result Non null.// w ww.j a v a 2 s . c o m * @param expected Non null. */ public static void assertTokenEquals(Token result, Token expected) { assertEquals(result.getAttributeNames().size(), expected.getAttributeNames().size()); for (CoreTokenField field : result.getAttributeNames()) { if (CoreTokenFieldTypes.isCalendar(field)) { Calendar resultCal = result.getValue(field); Calendar expectedCal = expected.getValue(field); if (resultCal.getTimeInMillis() != expectedCal.getTimeInMillis()) { throw new AssertionError(MessageFormat.format( "Milliseconds did not match for date field {0}:\n" + "Expected: {1}\n" + " Result: {2}", field.toString(), expectedCal.getTimeInMillis(), resultCal.getTimeInMillis())); } int resultOffset = getTotalTimeZoneOffset(resultCal.getTimeZone()); int expectedOffset = getTotalTimeZoneOffset(expectedCal.getTimeZone()); if (resultOffset != expectedOffset) { throw new AssertionError(MessageFormat .format("TimeZone offset did not match for date field {0}:\n" + "Expected: {1}\n" + " Result: {2}", field.toString(), expectedOffset, resultOffset)); } } else if (CoreTokenFieldTypes.isByteArray(field)) { byte[] resultValue = result.getValue(field); byte[] expectedValue = expected.getValue(field); if (!ArrayUtils.isEquals(resultValue, expectedValue)) { throw new AssertionError(MessageFormat.format( "Value did not match for byte[] field {0}:\n" + "Expected: {1} bytes\n" + " Result: {2} bytes", field.toString(), expectedValue.length, resultValue.length)); } } else { Object resultValue = result.getValue(field); Object expectedValue = expected.getValue(field); if (!compareValue(resultValue, expectedValue)) { throw new AssertionError(MessageFormat.format( "Value did not match for field {0}:\n" + "Expected: {1}\n" + " Result: {2}", field.toString(), expectedValue, resultValue)); } } } }