List of usage examples for java.util Calendar YEAR
int YEAR
To view the source code for java.util Calendar YEAR.
Click Source Link
get
and set
indicating the year. From source file:Main.java
public static String genererNomFichierInexistant(String directory, String extensionFichier) { Calendar c = Calendar.getInstance(); File aRetourner;//from w w w. j a v a 2 s . c o m 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 getMonthStringFromDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date);//from ww w . jav a 2 s.c om StringBuilder sb = new StringBuilder(); sb.append(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US)).append(", ") .append(calendar.get(Calendar.YEAR)); return sb.toString(); }
From source file:Main.java
public static java.util.Date getDateStart(java.util.Date d) { Calendar c = new GregorianCalendar(); c.clear();//from ww w. j a va 2s. 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
/** * Get number of month difference with the current month * @param year/*from w ww .j a v a2 s .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
private static boolean sameDay(Date a, Date b) { Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(a);/* ww w . j a v a 2 s .c o m*/ cal2.setTime(b); 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
static int daysBetween(Calendar day1, Calendar day2) { /**// w w w .ja v a2 s . c o m * Saved some effort using the solution described here, * http://stackoverflow.com/a/28865648/1587370 */ Calendar dayOne = (Calendar) day1.clone(), dayTwo = (Calendar) day2.clone(); if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) { return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR)); } else { if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) { //swap them Calendar temp = dayOne; dayOne = dayTwo; dayTwo = temp; } int extraDays = 0; int dayOneOriginalYearDays = dayOne.get(Calendar.DAY_OF_YEAR); while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) { dayOne.add(Calendar.YEAR, -1); // getActualMaximum() important for leap years extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR); } return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays; } }
From source file:Main.java
public static String getCurrentTimeStampYYYY() { Calendar cal = new GregorianCalendar(); return makeTimeString(cal.get(Calendar.YEAR), 4); }
From source file:Main.java
public static String getSubscribeTime(long time) { try {/*from w w w . j a va 2 s. c o m*/ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String formatTime = format.format(new Date(time)); Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); if (formatTime.startsWith(String.valueOf(year))) { return formatTime.substring(5); } else { return formatTime; } } catch (Exception e) { return ""; } }
From source file:Main.java
public static String getNextDateByYear(String s, int i) { SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd"); java.util.Date date = simpledateformat.parse(s, new ParsePosition(0)); Calendar calendar = Calendar.getInstance(); calendar.setTime(date);/*from w ww. ja v a 2 s .c o m*/ calendar.add(Calendar.YEAR, i); date = calendar.getTime(); s = simpledateformat.format(date); return s; }
From source file:Main.java
public static String formatDateTime(Context context, long time) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); Date d = new Date(time); int currentYear = Calendar.getInstance().get(Calendar.YEAR); int year = Integer.parseInt(TextUtils.isDigitsOnly(sdf.format(d)) ? sdf.format(d) : currentYear + ""); if (currentYear == year) { return DateUtils.formatDateTime(context, time, DateUtils.FORMAT_SHOW_DATE //| DateUtils.FORMAT_SHOW_WEEKDAY //| DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_MONTH //| DateUtils.FORMAT_ABBREV_WEEKDAY | DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_SHOW_TIME); } else {/* www .ja va 2 s .c o m*/ return DateUtils.formatDateTime(context, time, DateUtils.FORMAT_SHOW_DATE //| DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_MONTH //| DateUtils.FORMAT_ABBREV_WEEKDAY | DateUtils.FORMAT_ABBREV_TIME | DateUtils.FORMAT_SHOW_TIME); } }