private String getLocalTimeZone() { TimeZone tz = Calendar.getInstance().getTimeZone(); return tz.getID(); } },
private static ZonedDateTime calendarToZonedDateTime(Calendar source) { if (source instanceof GregorianCalendar) { return ((GregorianCalendar) source).toZonedDateTime(); } else { return ZonedDateTime.ofInstant(Instant.ofEpochMilli(source.getTimeInMillis()), source.getTimeZone().toZoneId()); } }
Calendar cal = new GregorianCalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); dateFormat.setTimeZone(cal.getTimeZone()); System.out.println(dateFormat.format(cal.getTime()));
Calendar cal = Calendar.getInstance(); TimeZone tz = cal.getTimeZone(); int offset = tz.getOffset(cal.get(Calendar.ERA), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_WEEK), return DATE_HEADER_FORMAT_INT.format(cal.getTime()) + tzMarker.toString();
public static Calendar convertToGmt(Calendar cal) { Date date = cal.getTime(); TimeZone tz = cal.getTimeZone(); log.debug("input calendar has date [" + date + "]"); //Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT long msFromEpochGmt = date.getTime(); //gives you the current offset in ms from GMT at the current date int offsetFromUTC = tz.getOffset(msFromEpochGmt); log.debug("offset is " + offsetFromUTC); //create a new calendar in GMT timezone, set to this date and add the offset Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); gmtCal.setTime(date); gmtCal.add(Calendar.MILLISECOND, offsetFromUTC); log.debug("Created GMT cal with date [" + gmtCal.getTime() + "]"); return gmtCal; }
private static boolean isThisYear(Calendar c) { Calendar now = Calendar.getInstance(c.getTimeZone()); return c.get(Calendar.YEAR) == now.get(Calendar.YEAR); }
Date endDate = metaA.getDate( dataA ); Calendar stDateCal = Calendar.getInstance(); Calendar endDateCal = Calendar.getInstance(); stDateCal.setTime( startDate ); endDateCal.setTime( endDate ); long endL = endDateCal.getTimeInMillis() + endDateCal.getTimeZone().getOffset( endDateCal.getTimeInMillis() ); long startL = stDateCal.getTimeInMillis() + stDateCal.getTimeZone().getOffset( stDateCal.getTimeInMillis() ); long diff = endL - startL;
/** * {@inheritDoc} */ @Override public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { final TimeZone zone = calendar.getTimeZone(); if (calendar.get(Calendar.DST_OFFSET) == 0) { buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale)); } else { buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale)); } } }
TimeZone tz = calendar.getTimeZone(); try { calendar.setTimeZone(UTC); || dayName == calendar.get(Calendar.DAY_OF_WEEK)) { calendar.add(Calendar.MINUTE, zone); return calendar.getTime(); } else { throw new IllegalArgumentException("Inconsistent day-name");
@Override public void writeObject(ObjectOutput output, Calendar calendar) throws IOException { output.writeUTF(calendar.getCalendarType()); output.writeLong(calendar.getTimeInMillis()); output.writeBoolean(calendar.isLenient()); output.writeUTF(calendar.getTimeZone().getID()); output.writeInt(calendar.getFirstDayOfWeek()); output.writeInt(calendar.getMinimalDaysInFirstWeek()); }
/** * Returns the last date of the month represented by the passed <code>cal</code> * * @param calendar The {@link java.util.Calendar} whose {@link java.util.Calendar#MONTH} field will be used * as the current month * @return */ public static int getLastDateOfMonth(Calendar calendar) { Calendar tmpCal = new GregorianCalendar(calendar.getTimeZone()); tmpCal.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); tmpCal.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); tmpCal.set(Calendar.DAY_OF_MONTH, 1); return tmpCal.getActualMaximum(Calendar.DAY_OF_MONTH); }
@Override public String toString() { Calendar expirationTime = Calendar.getInstance(); expirationTime.setTimeInMillis(getExpiration()); long remainingTime = expirationTime.getTimeInMillis() - Calendar.getInstance().getTimeInMillis(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS"); dateFormat.setTimeZone(expirationTime.getTimeZone()); String expirationTimeString = dateFormat.format(expirationTime.getTime()); return new StringBuilder("LoginAuthenticationToken for ") .append(getName()) .append(" issued by ") .append(getIssuer()) .append(" expiring at ") .append(expirationTimeString) .append(" [") .append(getExpiration()) .append(" ms, ") .append(remainingTime) .append(" ms remaining]") .toString(); }
Calendar c = Calendar.getInstance(); System.out.println("current: "+c.getTime()); TimeZone z = c.getTimeZone(); int offset = z.getRawOffset(); if(z.inDaylightTime(new Date())){ offset = offset + z.getDSTSavings(); } int offsetHrs = offset / 1000 / 60 / 60; int offsetMins = offset / 1000 / 60 % 60; System.out.println("offset: " + offsetHrs); System.out.println("offset: " + offsetMins); c.add(Calendar.HOUR_OF_DAY, (-offsetHrs)); c.add(Calendar.MINUTE, (-offsetMins)); System.out.println("GMT Time: "+c.getTime());
@Override protected TimeZone initialValue() { return Calendar.getInstance().getTimeZone(); } };
@Deprecated public static String format(Calendar self, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(self.getTimeZone()); return sdf.format(self.getTime()); }
/** * Report locale information * @param out stream to print to */ private static void doReportLocale(PrintStream out) { //calendar stuff. Calendar cal = Calendar.getInstance(); TimeZone tz = cal.getTimeZone(); out.println("Timezone " + tz.getDisplayName() + " offset=" + tz.getOffset(cal.get(Calendar.ERA), cal.get(Calendar.YEAR), cal .get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal .get(Calendar.DAY_OF_WEEK), ((cal.get(Calendar.HOUR_OF_DAY) * MINUTES_PER_HOUR + cal.get(Calendar.MINUTE)) * SECONDS_PER_MINUTE + cal.get(Calendar.SECOND)) * SECONDS_PER_MILLISECOND + cal.get(Calendar.MILLISECOND))); }
ContentResolver cr = context.getContentResolver(); ContentValues values = new ContentValues(); values.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis()); values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000); values.put(CalendarContract.Events.TITLE, this._title); values.put(CalendarContract.Events.CALENDAR_ID, 1); values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance() .getTimeZone().getID()); System.out.println(Calendar.getInstance().getTimeZone().getID()); Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); event.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis()); event.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);
private static int getFirstDateInMonthForDayOfWeek(Calendar cal, final int dayOfWeek) { Calendar tmpCal = new GregorianCalendar(cal.getTimeZone()); tmpCal.set(Calendar.YEAR, cal.get(Calendar.YEAR)); tmpCal.set(Calendar.MONTH, cal.get(Calendar.MONTH)); tmpCal.set(Calendar.DATE, 1); int day = tmpCal.get(Calendar.DAY_OF_WEEK); if (day == dayOfWeek) { return tmpCal.get(Calendar.DATE); } while (day != dayOfWeek) { tmpCal.add(Calendar.DATE, 1); day = tmpCal.get(Calendar.DAY_OF_WEEK); } return tmpCal.get(Calendar.DATE); }
private static ZonedDateTime calendarToZonedDateTime(Calendar source) { if (source instanceof GregorianCalendar) { return ((GregorianCalendar) source).toZonedDateTime(); } else { return ZonedDateTime.ofInstant(Instant.ofEpochMilli(source.getTimeInMillis()), source.getTimeZone().toZoneId()); } }
/** * Compares the given object to this {@code Calendar} and returns whether they are * equal. The object must be an instance of {@code Calendar} and have the same * properties. * * @return {@code true} if the given object is equal to this {@code Calendar}, {@code false} * otherwise. */ @Override public boolean equals(Object object) { if (this == object) { return true; } if (!(object instanceof Calendar)) { return false; } Calendar cal = (Calendar) object; return getTimeInMillis() == cal.getTimeInMillis() && isLenient() == cal.isLenient() && getFirstDayOfWeek() == cal.getFirstDayOfWeek() && getMinimalDaysInFirstWeek() == cal.getMinimalDaysInFirstWeek() && getTimeZone().equals(cal.getTimeZone()); }