List of usage examples for java.util Calendar MONTH
int MONTH
To view the source code for java.util Calendar MONTH.
Click Source Link
get
and set
indicating the month. From source file:Main.java
public static int getUserAge(Date birthDay) { Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) { return 0; }/*w w w .ja v a 2s.c om*/ int yearNow = cal.get(Calendar.YEAR); int monthNow = cal.get(Calendar.MONTH); int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); if (null == birthDay) { return 0; } else { cal.setTime(birthDay); int yearBirth = cal.get(Calendar.YEAR); int monthBirth = cal.get(Calendar.MONTH); int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); int age = yearNow - yearBirth; if (monthNow <= monthBirth) { if (monthNow == monthBirth) { // monthNow==monthBirth if (dayOfMonthNow < dayOfMonthBirth) { age--; } else { // do nothing } } else { // monthNow>monthBirth age--; } } else { // monthNow<monthBirth // donothing } return age; } }
From source file:Main.java
public static java.util.Date getDateStart(java.util.Date d) { Calendar c = new GregorianCalendar(); c.clear();// w ww .ja va 2 s . c om Calendar co = new GregorianCalendar(); co.setTime(d); c.set(Calendar.DAY_OF_MONTH, co.get(Calendar.DAY_OF_MONTH)); c.set(Calendar.MONTH, co.get(Calendar.MONTH)); c.set(Calendar.YEAR, co.get(Calendar.YEAR)); // c.add(Calendar.DAY_OF_MONTH,1); // c.add(Calendar.MILLISECOND,-1); return c.getTime(); }
From source file:Main.java
/** * * @param c1/* ww w . ja va2 s. co m*/ * @param c2 * @return */ public static boolean isSameMonth(Calendar c1, Calendar c2) { return !(c1 == null || c2 == null) && (c1.get(Calendar.ERA) == c2.get(Calendar.ERA) && (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))); }
From source file:Main.java
public static Date beginOfTheDay(@NonNull Date date) { Calendar inputDate = Calendar.getInstance(); inputDate.setTime(date);/*from w w w . ja v a2s . c o m*/ Calendar resultDate = Calendar.getInstance(); resultDate.set(inputDate.get(Calendar.YEAR), inputDate.get(Calendar.MONTH), inputDate.get(Calendar.DAY_OF_YEAR)); return resultDate.getTime(); }
From source file:Main.java
/** * Get number of month difference with the current month * @param year/*w w w.ja v a 2s .c o m*/ * @param month * @return */ public static int getMonthDifference(int year, int month) { Calendar cal = Calendar.getInstance(); int currentMonth = cal.get(Calendar.MONTH); int currentYear = cal.get(Calendar.YEAR); return (currentYear - year) * 12 + currentMonth - month; }
From source file:Main.java
public static boolean isSameMonth(Date date1, Date date2) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1);/*from w ww . j a v a2 s . c o m*/ Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH); }
From source file:Main.java
/** * the first day of month//w w w . j a v a 2s .c om * @param year * @param month * @return week position */ public static int getDayOfWeekInMonth(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DATE, 1); return calendar.get(Calendar.DAY_OF_WEEK); }
From source file:Main.java
public static long endOfThisMonth() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); cal.add(Calendar.MONTH, 1); cal.add(Calendar.DATE, -1);/* www. j a v a 2 s .c om*/ Date date = cal.getTime(); return date.getTime(); }
From source file:Main.java
public static String genererNomFichierInexistant(String directory, String extensionFichier) { Calendar c = Calendar.getInstance(); File aRetourner;/*from w ww. j av a2s . com*/ if (extensionFichier.equals("")) { aRetourner = new File( c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND)); } else if (directory.equals("")) { aRetourner = new File(c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND) + "." + extensionFichier); } else { aRetourner = new File(directory + "/" + c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND) + "." + extensionFichier); } return aRetourner.getPath(); }
From source file:Main.java
public static String formatSimpleDateStr(int year, int monthOfYear, int dayOfMonth) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, monthOfYear); cal.set(Calendar.DATE, dayOfMonth); return SIMPLE_DATE_FORMAT.format(cal.getTime()); }