Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package si.arnes.etabla.utils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.joda.time.DateTimeComparator; /** * * @author klemen */ public class DateUtils { public static Date getTodaysDateMidnightTime() throws ParseException { //we want today's date with time part set to 00:00:00 DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date todayWithZeroTime = formatter.parse(formatter.format(new Date())); return todayWithZeroTime; } /** * Returns current date plus days, hour rounded to full hours. * * @param day * @param hour * @return * @throws ParseException */ public static Date getDateFullDaysHoursFromNow(int day, int hour) throws ParseException { final Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, day); cal.add(Calendar.HOUR, hour); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); } public static boolean durationDatesOrderValid(Date from, Date date) { return !date.before(from); } public static Integer getDayNumberFromDate(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_WEEK); } /** * * Checks if from-to is colliding with from1-to1 * * @param from1 * @param to1 * @param from * @param to * @return */ public static boolean isTimePartColliding(Date from1, Date date1, Date from, Date to) { //yoda time-only comparator: DateTimeComparator comparator = DateTimeComparator.getTimeOnlyInstance(); //from is between or to is between or from-to is "inside" [fromCurr, toCurr) return comparator.compare(from, from1) >= 0 && comparator.compare(from, date1) < 0 || comparator.compare(to, from1) > 0 && comparator.compare(to, date1) <= 0 || comparator.compare(from1, from) > 0 && comparator.compare(date1, to) < 0; } public static int getDayNumberInMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_MONTH); } /** * Returns hour string in format HH:MM * @param date * @return */ public static String getTime(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return String.format("%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)); } public static boolean isSameDay(Date date1, Date date2) { Calendar cal = Calendar.getInstance(); cal.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); if (cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)) { return true; } else { return false; } } public static int isHourBeforeComparator(Date date1, Date date2) { Calendar cal = Calendar.getInstance(); cal.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); if (cal.get(Calendar.HOUR) < cal2.get(Calendar.HOUR)) { return 1; } if ((cal.get(Calendar.HOUR) == cal2.get(Calendar.HOUR)) && (cal.get(Calendar.MINUTE) < cal2.get(Calendar.MINUTE))) { return 1; } if ((cal.get(Calendar.HOUR) == cal2.get(Calendar.HOUR)) && (cal.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE))) { return 0; } return -1; } /** * 1.1 has value of 1 * @param date * @return */ public static int getDayOfTheYear(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_YEAR); } public static int getHour(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.HOUR_OF_DAY); } public static int getMinute(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.MINUTE); } /** * Returns day of the Week * Monday=0 Sunday=6 */ public static int getDayOfTheWeek(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_WEEK) - 1; } public static Date setDafaultDate(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(2017, 1, 1, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE)); return cal.getTime(); } /** * January=0 December= 11 * @param date * @return */ public static int getMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.MONTH); } public static Date setHourAndMinutes(Date date, int hour, int minute) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); return cal.getTime(); } }