/** * Sets the specified field to a date returning a new object. * This does not use a lenient calendar. * The original date object is unchanged. * * @param date the date, not null * @param calendarField the calendar field to set the amount to * @param amount the amount to set * @return a new Date object set with the specified value * @throws IllegalArgumentException if the date is null * @since 2.4 */ private static Date set(Date date, int calendarField, int amount) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } // getInstance() returns a new object, so this method is thread safe. Calendar c = Calendar.getInstance(); c.setLenient(false); c.setTime(date); c.set(calendarField, amount); return c.getTime(); }
/** * Parse the input string and return the corresponding calendar field * number. */ private static double getNumber(String in, String[] formats, int calField) throws ParseException { Calendar cal = Calendar.getInstance(); cal.setLenient(false); // Try the allowed formats, from longest to shortest. Date date = testFormats(in, formats); if (date == null) return Double.NaN; cal.setTime(date); return cal.get(calField); }
final Locale lcl = locale==null ?Locale.getDefault() : locale; final ParsePosition pos = new ParsePosition(0); final Calendar calendar = Calendar.getInstance(tz, lcl); calendar.setLenient(lenient); try { if (fdp.parse(str, pos, calendar) && pos.getIndex()==str.length()) { return calendar.getTime();
String time = timeFraction[0]; Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(0); calendar.setLenient(false); calendar.set(Calendar.YEAR, Integer.parseInt(time.substring(0, 4))); calendar.set(Calendar.MONTH, Integer.parseInt(time.substring(4, 6)) - 1); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(time.substring(6, 8))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.substring(8, 10))); if (time.length() >= 12) calendar.set(Calendar.MINUTE, Integer.parseInt(time.substring(10, 12))); return calendar.getTime();
public static java.util.Date secondIntoDate(long millisec) { Calendar cal = Calendar.getInstance(); cal.setLenient(true); cal.setTimeInMillis(millisec); return cal.getTime(); }
public static long dateIntoSecond(java.util.Date date) { if (date == null ) return 0; Calendar cal = Calendar.getInstance(); cal.setLenient(true); cal.setTime(date); return cal.getTimeInMillis(); }
public static boolean isvalidDate (int day, int month, int year) Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.set(year, month, day, 0, 0, 0); try { date.getTime(); return true; } catch (Exception e) { return false; } }
public static Date futureDate (final int interval, final EIntervalUnit unit) { final Calendar c = PDTFactory.createCalendar (); c.setTime (new Date ()); c.setLenient (true); c.add (_translate (unit), interval); return c.getTime (); }
private Calendar getDate(Map<String, String> args) { int year = Integer.parseInt(getRequired("year", args)); int month = getMonthFromArguments(args); int dayOfMonth = getDayOfMonthFromArguments(args); Calendar calendar = getCalendar(); calendar.setLenient(false); // be strict about validity of dates //noinspection MagicConstant calendar.set(year, month, dayOfMonth, 0, 0, 0); return calendar; }
/** * Sets the specified field to a date returning a new object. * This does not use a lenient calendar. * The original {@code Date} is unchanged. * * @param date the date, not null * @param calendarField the {@code Calendar} field to set the amount to * @param amount the amount to set * @return a new {@code Date} set with the specified value * @throws IllegalArgumentException if the date is null * @since 2.4 */ private static Date set(final Date date, final int calendarField, final int amount) { validateDateNotNull(date); // getInstance() returns a new object, so this method is thread safe. final Calendar c = Calendar.getInstance(); c.setLenient(false); c.setTime(date); c.set(calendarField, amount); return c.getTime(); }
Calendar calendar = null; if (locale == null && timeZone == null) { calendar = Calendar.getInstance(); } else if (locale == null) { calendar = Calendar.getInstance(timeZone); } else if (timeZone == null) { calendar = Calendar.getInstance(locale); } else { calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(new Date(value)); calendar.setLenient(false); return type.cast(calendar);
private static long getTimeTry(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute, int second, int millis) { Calendar c; if (tz == null) { c = getCalendar(); } else { c = Calendar.getInstance(tz); } synchronized (c) { c.clear(); c.setLenient(lenient); setCalendarFields(c, year, month, day, hour, minute, second, millis); return c.getTime().getTime(); } }
public static java.util.Date secondIntoDate(long millisec, TimeZone tz) { Calendar cal = Calendar.getInstance(tz); cal.setLenient(true); cal.setTimeInMillis(millisec); return cal.getTime(); }
public static long dateIntoSecond(java.util.Date date, TimeZone tz) { if (date == null ) return 0; Calendar cal = Calendar.getInstance(tz); cal.setLenient(true); cal.setTime(date); return cal.getTimeInMillis(); }
Calendar calendar = Calendar.getInstance(); calendar.setLenient(true); calendar.set(Calendar.DATE, 1); calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
private Calendar getDate(Map<String, String> args) { int year = Integer.parseInt(getRequired("year", args)); int month = getMonthFromArguments(args); int dayOfMonth = getDayOfMonthFromArguments(args); Calendar calendar = getCalendar(); calendar.setLenient(false); // be strict about validity of dates //noinspection MagicConstant calendar.set(year, month, dayOfMonth, 0, 0, 0); return calendar; }