List of usage examples for java.util Calendar setTime
public final void setTime(Date date)
Date
. From source file:Main.java
/** * Copy the year, month and day from a date to another * @param dateOrigin Date origin where to get the data * @param dateDestiny Date destiny where to set the data * @return Date with year month and day from dateOrigin and rest from dateDestiny *///from w w w. j ava 2s .c o m public static Date copyYearMonthDay(Date dateOrigin, Date dateDestiny) { //check null values if (dateOrigin == null && dateDestiny == null) { return Calendar.getInstance().getTime(); } else if (dateOrigin == null) { return dateDestiny; } else if (dateDestiny == null) { return dateOrigin; } //convert to calendars Calendar calOrigin = Calendar.getInstance(); calOrigin.setTime(dateOrigin); Calendar calDestiny = Calendar.getInstance(); calDestiny.setTime(dateDestiny); //return the time of destiny return copyYearMonthDay(calOrigin, calDestiny).getTime(); }
From source file:Main.java
public static Date getFirstDayOfMonth(Date date) { if (date == null) { return null; }/* w w w . ja v a 2 s .c o m*/ Calendar calendar = Calendar.getInstance(Locale.getDefault()); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTime(); }
From source file:Main.java
public static boolean sameDate(@NonNull Date date1, @NonNull Date date2) { Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2);// www .ja v a 2 s. co m return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR); }
From source file:Main.java
/** * @param startDateTime//from w w w .j a v a 2 s . co m * @param endDateTime * @return the difference in days between the second timestamp and first */ public static double getDifferenceInDays(Timestamp startDateTime, Timestamp endDateTime) { int difference = 0; Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(startDateTime); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDateTime); // First, get difference in whole days Calendar startCompare = Calendar.getInstance(); startCompare.setTime(startDateTime); startCompare.set(Calendar.HOUR_OF_DAY, 0); startCompare.set(Calendar.MINUTE, 0); startCompare.set(Calendar.SECOND, 0); startCompare.set(Calendar.MILLISECOND, 0); Calendar endCompare = Calendar.getInstance(); endCompare.setTime(endDateTime); endCompare.set(Calendar.HOUR_OF_DAY, 0); endCompare.set(Calendar.MINUTE, 0); endCompare.set(Calendar.SECOND, 0); endCompare.set(Calendar.MILLISECOND, 0); return (endCompare.getTimeInMillis() - startCompare.getTimeInMillis()) / (24 * 60 * 60 * 1000); }
From source file:Main.java
/** * Try to parse input with SimpleDateFormat * * @param input input.//from w ww . j ava 2 s .c o m * @param format SimpleDateFormat * @param setYear1700 When true the age will be not displayed in brackets * @param locale locale. * @return Date object if successful, otherwise null */ private static Date parseStringWithSimpleDateFormat(String input, String format, boolean setYear1700, Locale locale) { SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale); dateFormat.setTimeZone(TimeZone.getDefault()); try { Date parsedDate = dateFormat.parse(input); /* * Because no year is defined in address book, set year to 1700 * * When year < 1800, the age will be not displayed in brackets */ if (setYear1700) { Calendar cal = Calendar.getInstance(); cal.setTime(parsedDate); cal.set(Calendar.YEAR, 1700); } return parsedDate; } catch (ParseException ignored) { return null; } }
From source file:Main.java
/** * Adds to a date returning a new object. * The original date object is unchanged. * * @param date the date, not null// w w w . j a v a 2 s .c o m * @param calendarField the calendar field to add to * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws IllegalArgumentException if the date is null */ private static Date add(Date date, int calendarField, int amount) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); return c.getTime(); }
From source file:Main.java
public static Calendar string2Calendar(String date) { date = getDateNoWeek(date);//from w w w . j a v a 2 s. co m final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date d; try { d = format.parse(date); } catch (ParseException e) { d = new Date(); } final Calendar mCalendar = (Calendar) Calendar.getInstance().clone(); mCalendar.setTime(d); return mCalendar; }
From source file:AIR.Common.Utilities.Dates.java
/** * @param date//from w w w.j ava2 s .c o m * @return returns the midnight time for this date e.g. if date is * "10/11/2012 12:10 pm" then this will return "10/11/2012 00:00 am" */ public static Date getStartOfDayDate(Date date) { Calendar calendarInstance = Calendar.getInstance(); calendarInstance.setTime(date); calendarInstance.set(Calendar.HOUR_OF_DAY, 0); calendarInstance.set(Calendar.MINUTE, 0); calendarInstance.set(Calendar.SECOND, 0); return calendarInstance.getTime(); }
From source file:AIR.Common.Utilities.Dates.java
/** * @param date//from ww w .j a va2 s . co m * @return returns the midnight time for before date ends e.g. if date is * "10/11/2012 12:10 pm" then this will return "10/11/2012 11:59 pm" */ public static Date getEndOfDayDate(Date date) { Calendar calendarInstance = Calendar.getInstance(); calendarInstance.setTime(date); calendarInstance.set(Calendar.HOUR_OF_DAY, 23); calendarInstance.set(Calendar.MINUTE, 59); calendarInstance.set(Calendar.SECOND, 0); return calendarInstance.getTime(); }
From source file:Main.java
public static Date getDate(String t, String pattern) { SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault()); try {// w w w . j a v a 2 s . c o m Date date = dateFormat.parse(t); Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTime(date); return date; } catch (ParseException e) { return null; } }