public void button(View v) { // カレンダー型変数を取得 calendar = Calendar.getInstance(); // 一分五秒後に設定 int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); // TimePicker TimePickerDialog dialog = new TimePickerDialog( this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Log.d("test", String.format(" % 02d:%02d", hourOfDay, minute)); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); } }, hour, minute, true); dialog.show(); }
public static int getDayOfWeek(String date) { int year; int month; int day; if (StringUtils.isEmpty(date)) return -1; String input = date.replace("-", ""); int len = input.length(); if (len != 4 && len != 8) return -1; year = 0; month = 0; day = 0; if (len == 8) { year = Integer.parseInt(input.substring(0, 4)); input = input.substring(4); } month = Integer.parseInt(input.substring(0, 2)); day = Integer.parseInt(input.substring(2, 4)); int week; Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); if (year > 0) calendar.set(1, year); if (month > 0) calendar.set(2, month - 1); if (day > 0) calendar.set(5, day); week = calendar.get(7); return week; }
public int obtenerDiasHabiles(Date fecha, Date fecha2) { Calendar calendario = Calendar.getInstance(); calendario.setTimeZone(TimeZone.getTimeZone("GMT-4:00")); calendario.setTime(fecha2); calendario.add(Calendar.DAY_OF_YEAR, +1); calendario.set(Calendar.HOUR, 0); calendario.set(Calendar.HOUR_OF_DAY, 0); calendario.set(Calendar.SECOND, 0); calendario.set(Calendar.MILLISECOND, 0); calendario.set(Calendar.MINUTE, 0); fecha2 = calendario.getTime(); calendario.setTime(fecha); String fija = formatoFecha.format(fecha2); String hoy = ""; int contador = 0; do { calendario.setTime(fecha); if (calendario.get(Calendar.DAY_OF_WEEK) != 1 && calendario.get(Calendar.DAY_OF_WEEK) != 7) contador++; calendario.add(Calendar.DAY_OF_YEAR, +1); fecha = calendario.getTime(); hoy = formatoFecha.format(fecha); } while (!hoy.equals(fija)); return contador; }
@Test public void testBadPattern() { final Calendar cal = Calendar.getInstance(); cal.set(2001, Calendar.FEBRUARY, 3, 4, 5, 6); cal.set(Calendar.MILLISECOND, 789); final long timestamp = cal.getTimeInMillis(); final List<PatternFormatter> formatters = parser.parse(badPattern); assertNotNull(formatters); final Throwable t = new Throwable(); final StackTraceElement[] elements = t.getStackTrace(); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("a.b.c") // .setLoggerFqcn(Logger.class.getName()) // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world")) // .setThreadName("Thread1") // .setSource(elements[0]) // .setTimeMillis(timestamp) // .build(); final StringBuilder buf = new StringBuilder(); for (final PatternFormatter formatter : formatters) { formatter.format(event, buf); } final String str = buf.toString(); // eats all characters until the closing '}' character final String expected = "[2001-02-03 04:05:06,789] - Hello, world"; assertTrue( "Expected to start with: " + expected + ". Actual: " + str, str.startsWith(expected)); }
@Test public void testOrderClientInvalid() throws Exception { NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); Calendar cal = Calendar.getInstance(Locale.US); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.APRIL); cal.set(Calendar.DAY_OF_MONTH, 20); cal.set(Calendar.HOUR_OF_DAY, 7); cal.set(Calendar.MINUTE, 47); cal.set(Calendar.SECOND, 58); Date date = cal.getTime(); OrderClient client = new OrderClient("tcp://localhost:61616"); // when using customer id 999 we force an invalid order client.sendOrder(999, date, "5555", "2222"); boolean matches = notify.matches(5, TimeUnit.SECONDS); assertTrue(matches); // should be one message in confirm queue BrowsableEndpoint be = context.getEndpoint("activemq:queue:invalid", BrowsableEndpoint.class); List<Exchange> list = be.getExchanges(); assertEquals(1, list.size()); assertEquals("999,2010-04-20T07:47:58,5555,2222", list.get(0).getIn().getBody(String.class)); }
// helper method for DayHelper. Resets the time on the calendar to 00:00:00 am private static void setCalendarToDefaultTime(Calendar cal) { cal.set(Calendar.HOUR, 0); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.AM_PM, Calendar.AM); }
private void trim(Calendar calendar) { int hours = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); calendar.setTimeInMillis(0); calendar.set(Calendar.HOUR_OF_DAY, hours); calendar.set(Calendar.MINUTE, minutes); }
public weekView(ProgramFrame frame) throws SQLException { this.frame = frame; initDesign(); cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day ! cal.clear(Calendar.MINUTE); cal.clear(Calendar.SECOND); cal.clear(Calendar.MILLISECOND); // get start of this week in milliseconds cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date firstDay = cal.getTime(); String fDay = df.format(firstDay); String[] dates = fDay.split("/"); int date = Integer.parseInt(dates[0]); // date of this weeks monday int month = Integer.parseInt(dates[1]); // current month int year = Integer.parseInt(dates[2]); // current year generateThisWeek(date, month, year); }
public static void resetToDate(Calendar cal) { cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); }
/** @return Returns the triggerEvents. */ public List<TriggerEvent> getTriggerEvents() { if (isViewAllSelected) { return eventManager.getTriggerEvents(); } else { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); Date midnightToday = new Date(cal.getTimeInMillis()); return eventManager.getTriggerEvents(midnightToday, null, null, null, null); /* for (Iterator<TriggerEventImpl> i = triggerEvents.iterator(); i.hasNext();) { TriggerEventImpl te = (TriggerEventImpl) i.next(); if (te.getTime().after(midnightToday)) { todaysTriggerEvents.add(te); } } return todaysTriggerEvents; */ } }
/** * 得到当天的0时0分0秒的 时间 * * @return long */ public static long getTodayBeginTime() { Calendar nowTime = Calendar.getInstance(); nowTime.set(Calendar.HOUR_OF_DAY, 0); nowTime.set(Calendar.MINUTE, 0); nowTime.set(Calendar.SECOND, 0); return nowTime.getTimeInMillis() / 1000; }
private static StartTimeDetails createStartTimeDetails( final UUID devUUID, long timestamp, MieleDeviceHomeBusDataREST dev) { StartTimeDetails startDetails = new StartTimeDetails(devUUID, timestamp); startDetails.setStartTime(-1); if (dev.getDeviceDetails() != null) { MieleDuration mieleStartTime = dev.getDeviceDetails().getStartTime(); if (mieleStartTime != null) { int starttime = mieleStartTime.duration(); if (starttime >= 0) { Calendar calNow = Calendar.getInstance(); Calendar calStartTime = (Calendar) calNow.clone(); calStartTime.set(Calendar.MINUTE, starttime % 60); calStartTime.set(Calendar.HOUR_OF_DAY, starttime / 60); if (calStartTime.before(calNow)) { calStartTime.add(Calendar.DAY_OF_YEAR, 1); } startDetails.setStartTime(calStartTime.getTimeInMillis() / 1000L); } } } return startDetails; }
private List<Long> getPayUsers(Calendar calendar) { List<Long> payUserIds = new ArrayList<Long>(); Calendar calendar1 = Calendar.getInstance(); calendar1.setTime(calendar.getTime()); calendar1.set(Calendar.HOUR_OF_DAY, 0); calendar1.set(Calendar.MINUTE, 0); calendar1.set(Calendar.SECOND, 0); Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(calendar.getTime()); calendar2.set(Calendar.HOUR_OF_DAY, 23); calendar2.set(Calendar.MINUTE, 59); calendar2.set(Calendar.SECOND, 59); try { PreparedStatement pstmt = orderCon.prepareStatement( "select distinct(uid) from ordersqq where time between ? and ? and cmd=7"); pstmt.setLong(1, calendar1.getTimeInMillis()); pstmt.setLong(2, calendar2.getTimeInMillis()); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { payUserIds.add(rs.getLong(1)); } } catch (Exception e) { logger.error("", e); } return payUserIds; }
public static Calendar nowNoSecond() { final Calendar now = Calendar.getInstance(); now.set(Calendar.SECOND, 0); now.set(Calendar.MILLISECOND, 0); return now; }
@Override public List<MaintenanceSpendBySupplier> getMaintenanceSpendBetweenTwoDates(Date from, Date to) { Date fromDate = dateTimeFormatHelper.resetTimeAndMonthStart(from); Date toDate = dateTimeFormatHelper.resetTimeAndMonthEnd(to); Calendar calendar = Calendar.getInstance(); calendar.setTime(toDate); // Set time fields to last hour:minute:second:millisecond calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); Query maintenanceSpendListQuery = new Query(); maintenanceSpendListQuery.addCriteria( Criteria.where("transactionDate") .exists(true) .andOperator( Criteria.where("transactionDate").gte(fromDate), Criteria.where("transactionDate").lte(calendar.getTime()))); /* List<MaintenanceSpendBySupplier> maintenanceSpendList = mongoOperation.find(maintenanceSpendListQuery, MaintenanceSpendBySupplier.class); System.out.println(" MaintenanceSpendBySupplierRepository - GENERAL QUERY - Start= " + to + " | To= " + to); if (maintenanceSpendList.isEmpty()) { System.out.println("MaintenanceSpendBySupplierRepository - GENERAL QUERY - NO MATCHING RECORDS FOUND"); } for (MaintenanceSpendBySupplier maintenanceSpend : maintenanceSpendList) { System.out.println(" MaintenanceSpendBySupplierRepository - GENERAL QUERY - Date= " + maintenanceSpend.getTransactionDate() + " | Cost= " + maintenanceSpend.getMaintenanceCost() + " | Truck= " + maintenanceSpend.getTruckId() + " | Supplier" + maintenanceSpend.getSupplierId()); } System.out.println("--==--"); */ return mongoOperation.find(maintenanceSpendListQuery, MaintenanceSpendBySupplier.class); }
public static void resetToTime(Calendar cal) { cal.set(Calendar.YEAR, 1970); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DATE, 1); cal.set(Calendar.MILLISECOND, 0); }
private long TimeDifference() { // need to check the alaram time and the triggring action time around 12 Calendar cal = Calendar.getInstance(); Log.d("settings.java ", " time before setting :" + cal.getTime()); Log.d("settings.java ", " calendertime in millisec: " + cal.getTimeInMillis()); Log.d("settings.java ", " system time in millisec :" + System.currentTimeMillis()); // cal.setTime(); cal.set(Calendar.SECOND, 0); cal.set(Calendar.HOUR_OF_DAY, 0); // cal.set(Calendar.HOUR, 23); cal.set(Calendar.MINUTE, 0); cal.add(Calendar.DAY_OF_MONTH, 1); Log.d("settings.java", " time After setting day+1 and 00 hours :" + cal.getTime()); Log.d("settings.java", " set time in millis :" + cal.getTimeInMillis()); Log.d( "settings.java", " time of the system in millis" + Calendar.getInstance().getTimeInMillis()); long timediff = cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis(); // +1000; Log.d( "settings.java", " current time: " + Calendar.getInstance() + " setted time in alaram should be 23.59.59 but is : " + cal.getTime() + "time diff in millisec :" + timediff); return timediff; }
public Prestamo crearPrestamo( Cliente cliente, String comentario, Date fecha, FormaPago formaPago, double monto, float tasa) throws PrestamoException { if (monto < 0) throw new PrestamoException("Valor del 'monto' es menor que cero (0)"); Prestamo prestamo = new Prestamo(); prestamo.setCliente(cliente); prestamo.setComentario(comentario); Calendar c = Calendar.getInstance(); // hora int hora = c.get(Calendar.HOUR_OF_DAY); int minuto = c.get(Calendar.MINUTE); c.setTime(fecha); c.set(Calendar.HOUR_OF_DAY, hora); c.set(Calendar.MINUTE, minuto); prestamo.setFecha(c.getTime()); prestamo.setFormaPago(formaPago); prestamo.setMonto(monto); prestamo.setTasa(tasa); persist(prestamo); if (prestamo.getId() != null) return prestamo; return null; }
@Override protected void merge(Calendar calendar) { calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, second); calendar.set(Calendar.MILLISECOND, millisecond); }
private static void setAlarm(final Context context, final String reminder_time) { AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, DevotionReminderReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // always cancel current (if any) am.cancel(pi); if (TextUtils.isEmpty(reminder_time)) { return; } int hour = Integer.parseInt(reminder_time.substring(0, 2)); int minute = Integer.parseInt(reminder_time.substring(2, 4)); Calendar c = GregorianCalendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); if (c.getTimeInMillis() < System.currentTimeMillis()) { c.add(Calendar.DAY_OF_YEAR, 1); } am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); }
private String getDate(int day) { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); cal.add(Calendar.DATE, day); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); return DateTools.dateToString(cal.getTime(), DateTools.Resolution.MINUTE); }
@Test public void shouldParseAndyWarholArtistInfobox() throws MalformedURLException { Calendar birthDateCalendar = Calendar.getInstance(); birthDateCalendar.set(1928, Calendar.AUGUST, 6, 0, 0, 0); birthDateCalendar.set(Calendar.MILLISECOND, 0); Calendar deathDateCalendar = Calendar.getInstance(); deathDateCalendar.set(1987, Calendar.FEBRUARY, 22, 0, 0, 0); deathDateCalendar.set(Calendar.MILLISECOND, 0); Artist artist = WikipediaArtistInfoboxParser.parse(INFOBOX); Assert.assertEquals("Andy Warhol", artist.getName()); Assert.assertEquals( new URL("https://en.wikipedia.org/wiki/File:Andy_Warhol_by_Jack_Mitchell.jpg"), artist.getImage()); Assert.assertEquals(birthDateCalendar.getTimeInMillis(), artist.getBirthDate().getTime()); Assert.assertEquals("Pittsburgh", artist.getBirthPlace().getName()); Assert.assertEquals("Pennsylvania", artist.getBirthPlace().getState()); Assert.assertEquals("United States", artist.getBirthPlace().getCountry()); Assert.assertEquals(deathDateCalendar.getTimeInMillis(), artist.getDeathDate().getTime()); Assert.assertEquals("New York City", artist.getDeathPlace().getName()); Assert.assertEquals("New York", artist.getDeathPlace().getState()); Assert.assertEquals("United States", artist.getDeathPlace().getCountry()); Assert.assertEquals("American", artist.getNationality()); Assert.assertEquals("Pop art", artist.getArtMovements().get(0).getName()); }
@Test public void testOrderClientValid() throws Exception { // notify when one message is done NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); // setup order information Calendar cal = Calendar.getInstance(Locale.US); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.APRIL); cal.set(Calendar.DAY_OF_MONTH, 20); cal.set(Calendar.HOUR_OF_DAY, 7); cal.set(Calendar.MINUTE, 47); cal.set(Calendar.SECOND, 58); Date date = cal.getTime(); // send an order using the client OrderClient client = new OrderClient("tcp://localhost:61616"); client.sendOrder(123, date, "4444", "5555"); // use the notifier to wait for Camel to process the message // wait at most 5 seconds to avoid blocking forever if something goes wrong boolean matches = notify.matches(5, TimeUnit.SECONDS); // true means the notifier condition matched (= 1 message is done) assertTrue(matches); // should be one message in confirm queue BrowsableEndpoint be = context.getEndpoint("activemq:queue:confirm", BrowsableEndpoint.class); List<Exchange> list = be.getExchanges(); assertEquals(1, list.size()); assertEquals("OK,123,2010-04-20T07:47:58,4444,5555", list.get(0).getIn().getBody(String.class)); }
/** * For the contracts whose volume shifts from the front contract to the back contract on the day * preceding the 2nd Friday of expiration month of the front contract. */ public static String getMostLiquid(Calendar asOfDate) { int monthNow = asOfDate.get(Calendar.MONTH); int yearNow = asOfDate.get(Calendar.YEAR); int mostLiquidYear = yearNow; int mostLiquidMonth = frontMonths.get(monthNow); // special case with December if (monthNow == Calendar.DECEMBER) { mostLiquidYear = yearNow + 1; } if (keepCurrent(asOfDate)) { mostLiquidMonth = monthNow; mostLiquidYear = yearNow; } Calendar mostLiquidDate = Calendar.getInstance(); mostLiquidDate.set(Calendar.DAY_OF_MONTH, 1); mostLiquidDate.set(Calendar.MONTH, mostLiquidMonth); mostLiquidDate.set(Calendar.YEAR, mostLiquidYear); SimpleDateFormat df = new SimpleDateFormat("yyyyMM"); return df.format(mostLiquidDate.getTime()); }
public static CoveringInfo getWeeklyCoveringInfo(Date from, Date to) { int dayDiff = 0; Date tmpFrom = from; while (tmpFrom.before(to)) { tmpFrom = DateUtils.addDays(tmpFrom, 1); dayDiff++; } if (dayDiff < 7) { return new CoveringInfo(0, false); } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromWeek = cal.get(Calendar.WEEK_OF_YEAR); int fromDay = cal.get(Calendar.DAY_OF_WEEK); int fromYear = cal.get(YEAR); cal.clear(); cal.set(YEAR, fromYear); cal.set(Calendar.WEEK_OF_YEAR, fromWeek); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); int maxDayInWeek = cal.getActualMaximum(Calendar.DAY_OF_WEEK); Date fromWeekStartDate = cal.getTime(); boolean coverable = dayDiff % 7 == 0; if (fromWeekStartDate.before(from)) { // Count from the start of next week dayDiff -= (maxDayInWeek - (fromDay - Calendar.SUNDAY)); coverable = false; } return new CoveringInfo(dayDiff / 7, coverable); }
/** * 获取智能生日 * * @param birthday * @param needYear * @return */ public static String getSmartBirthday(long birthday, boolean needYear) { int[] birthdayDetails = getDateDetail(birthday); int[] nowDetails = getDateDetail(System.currentTimeMillis()); Calendar birthdayCalendar = Calendar.getInstance(); birthdayCalendar.set(birthdayDetails[0], birthdayDetails[1], birthdayDetails[2]); Calendar nowCalendar = Calendar.getInstance(); nowCalendar.set(nowDetails[0], nowDetails[1], nowDetails[2]); int days = birthdayCalendar.get(Calendar.DAY_OF_YEAR) - nowCalendar.get(Calendar.DAY_OF_YEAR); if (days < 8) { if (days >= 3) { return days + "天后"; } if (days >= 2) { return TimeUtil.Day.NAME_THE_DAY_AFTER_TOMORROW; } if (days >= 1) { return TimeUtil.Day.NAME_TOMORROW; } if (days >= 0) { return TimeUtil.Day.NAME_TODAY; } } if (needYear) { return birthdayDetails[0] + "年" + birthdayDetails[1] + "月" + birthdayDetails[2] + "日"; } return birthdayDetails[1] + "月" + birthdayDetails[2] + "日"; }
private static void zeroDate(Calendar date) { date.set(Calendar.HOUR, 0); date.set(Calendar.MINUTE, 0); date.set(Calendar.SECOND, 0); date.set(Calendar.MILLISECOND, 0); date.set(Calendar.AM_PM, Calendar.AM); }
/** * 生成查询参数Map * * @return Map<String, Object> */ private Map<String, Object> genParamMap(String condition) { Map<String, Object> params = new HashMap<String, Object>(); if ("default".equals(flag)) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1); String eDate = dateFormat.format(cal.getTime()); cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 6); String sDate = dateFormat.format(cal.getTime()); params.put("beginDate", sDate); params.put("endDate", eDate); // 设置日期过滤框默认值 statParams.put("beginDate", sDate); statParams.put("endDate", eDate); } if (StringUtils.isNotBlank(statParams.get("processType"))) { params.put("processType", statParams.get("processType")); } if (StringUtils.isNotBlank(statParams.get("beginDate"))) { params.put("beginDate", statParams.get("beginDate")); } if (StringUtils.isNotBlank(statParams.get("endDate"))) { params.put("endDate", statParams.get("endDate")); } return params; }
private static Date createDate(final int year, final int month, final int day) { final Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("GMT")); instance.setLenient(true); instance.set(year, month, day, 0, 0, 0); instance.set(Calendar.MILLISECOND, 0); return instance.getTime(); }
@Test public void dateParsingTest() throws Exception { final String s1 = f1.format(date); TimePositionType tp = new TimePositionType(s1); assertEquals(date, tp.getDate()); final String s2 = f2.format(date); tp = new TimePositionType(s2); assertEquals(date, tp.getDate()); final String s3 = f3.format(date); tp = new TimePositionType(s3); final Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); final Date dateNoTime = cal.getTime(); assertEquals(dateNoTime, tp.getDate()); }