@Override public Calendar convert(Date source) { Calendar calendar = Calendar.getInstance(); calendar.setTime(source); return calendar; } }
String dt = "2008-01-01"; // Start date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(sdf.parse(dt)); c.add(Calendar.DATE, 1); // number of days to add dt = sdf.format(c.getTime()); // dt is now the new date
Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
java.util.Date date= new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); int month = cal.get(Calendar.MONTH);
String myDateString = "13:24:40"; //SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss"); //the above commented line was changed to the one below, as per Grodriguez's pertinent comment: SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); Date date = sdf.parse(myDateString); Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance calendar.setTime(date); // assigns calendar to given date int hour = calendar.get(Calendar.HOUR); int minute; /... similar methods for minutes and seconds
@Test public void testCalendar() { Date d = new Date(0); Calendar cal = Calendar.getInstance(); cal.setTime(d); assertThat(jdkTypeToJson(cal), containsString(new SimpleDateFormat("yyyy-MM-dd").format(d))); }
private Calendar calExpiredTime() { Calendar calendar = Calendar.getInstance(); calendar.setTime(DateUtils.addHours(new Date(), portalConfig.survivalDuration())); return calendar; }
Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH); cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
String computeTimeStampString(long now) { synchronized (this) { // Since the formatted output is only precise to the second, we can use the same cached string if the // current // second is the same (stripping off the milliseconds). if ((now / 1000) != lastTimestamp) { lastTimestamp = now / 1000; Date nowDate = new Date(now); calendar.setTime(nowDate); timesmapStr = String.format("%s %2d %s", simpleMonthFormat.format(nowDate), calendar.get(Calendar.DAY_OF_MONTH), simpleTimeFormat.format(nowDate)); } return timesmapStr; } } }
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date convertedDate = dateFormat.parse(date); Calendar c = Calendar.getInstance(); c.setTime(convertedDate); c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
/** * Returns true if the given calendar matches */ boolean check(Calendar cal) { Calendar checkCal = cal; if(specTimezone != null && !specTimezone.isEmpty()) { Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone(specTimezone)); tzCal.setTime(cal.getTime()); checkCal = tzCal; } if(!checkBits(bits[0],checkCal.get(MINUTE))) return false; if(!checkBits(bits[1],checkCal.get(HOUR_OF_DAY))) return false; if(!checkBits(bits[2],checkCal.get(DAY_OF_MONTH))) return false; if(!checkBits(bits[3],checkCal.get(MONTH)+1)) return false; if(!checkBits(dayOfWeek,checkCal.get(Calendar.DAY_OF_WEEK)-1)) return false; return true; }
private static int getWithMondayFirst(final Date date, int field) { Validate.notNull(date, "The date must not be null"); Calendar cal = Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.MONDAY); cal.setTime(date); return cal.get(field); }
protected Date calculateDueDate(CommandContext commandContext, int waitTimeInSeconds, Date oldDate) { Calendar newDateCal = new GregorianCalendar(); if (oldDate != null) { newDateCal.setTime(oldDate); } else { newDateCal.setTime(commandContext.getProcessEngineConfiguration().getClock().getCurrentTime()); } newDateCal.add(Calendar.SECOND, waitTimeInSeconds); return newDateCal.getTime(); }
Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.DATE, 1); dt = c.getTime();
Date date = new Date(); // given date Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance calendar.setTime(date); // assigns calendar to given date calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format calendar.get(Calendar.HOUR); // gets hour in 12h format calendar.get(Calendar.MONTH); // gets month number, NOTE this is zero based!
Date date; // your date Calendar cal = Calendar.getInstance(); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); // etc.
public static void main(String[] args) throws ParseException { DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date lastDec2010 = sdf.parse("31/12/2010"); Calendar calUs = Calendar.getInstance(Locale.US); calUs.setTime(lastDec2010); Calendar calDe = Calendar.getInstance(Locale.GERMAN); calDe.setTime(lastDec2010); System.out.println( "us: " + calUs.get( Calendar.WEEK_OF_YEAR ) ); System.out.println( "de: " + calDe.get( Calendar.WEEK_OF_YEAR ) ); }
@Test public void deepCopy_calendar() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); assertThat(copy(calendar), is(equalTo(calendar))); }
String strThatDay = "1985/08/25"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); Date d = null; try { d = formatter.parse(strThatDay);//catch exception } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar thatDay = Calendar.getInstance(); thatDay.setTime(d); //rest is the same....