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:Util.java
public static Date getMonday(Date today) { Calendar cal = Calendar.getInstance(); cal.setTime(today);/* w ww . j a v a 2s .c om*/ int dow = cal.get(Calendar.DAY_OF_WEEK); while (dow != Calendar.MONDAY) { int date = cal.get(Calendar.DATE); if (date == 1) { int month = cal.get(Calendar.MONTH); if (month == Calendar.JANUARY) { month = Calendar.DECEMBER; cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1); } else { month--; } cal.set(Calendar.MONTH, month); date = getMonthLastDate(month, cal.get(Calendar.YEAR)); } else { date--; } cal.set(Calendar.DATE, date); dow = cal.get(Calendar.DAY_OF_WEEK); } return cal.getTime(); }
From source file:Main.java
public static String formatExpirationDate(String text) { try {/* w w w.ja v a2 s . c o m*/ switch (text.length()) { case 1: int digit = Integer.parseInt(text); if (digit < 2) { return text; } else { return "0" + text + "/"; } case 2: int month = Integer.parseInt(text); if (month > 12 || month < 1) { // Invalid digit return text.substring(0, 1); } else { return text + "/"; } case 3: if (text.substring(2, 3).equalsIgnoreCase("/")) { return text; } else { text = text.substring(0, 2) + "/" + text.substring(2, 3); } case 4: int yearDigit = Integer.parseInt(text.substring(3, 4)); String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); int currentYearDigit = Integer.parseInt(year.substring(2, 3)); if (yearDigit < currentYearDigit) { // Less than current year invalid return text.substring(0, 3); } else { return text; } case 5: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/yy"); simpleDateFormat.setLenient(false); Date expiry = simpleDateFormat.parse(text); if (expiry.before(new Date())) { // Invalid exp date return text.substring(0, 4); } else { return text; } default: if (text.length() > 5) { return text.substring(0, 5); } else { return text; } } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } // If an exception is thrown we clear out the text return ""; }
From source file:Main.java
public static int[] getTime(long time, String timeZone) { Calendar c = Calendar.getInstance(); if (timeZone != null && timeZone.length() != 0) { TimeZone tz = TimeZone.getTimeZone(timeZone); c = Calendar.getInstance(tz); }// w ww . j ava2 s .c o m c.setTimeInMillis(time); int y = c.get(Calendar.YEAR); int m = c.get(Calendar.MONTH); int d = c.get(Calendar.DAY_OF_MONTH); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); return new int[] { y, m, d, hour, minute, second }; }
From source file:Main.java
private static final Calendar getUtcDate(int year, int month, int dayOfMonth) { final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE, Locale.US); calendar.clear();/* w w w .j a va 2 s.c o m*/ calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); return calendar; }
From source file:Main.java
public static int compareDates(java.util.Date startDate, java.util.Date endDate) { int interval = 0; calendarStartDate = Calendar.getInstance(); calendarStartDate.set(Calendar.YEAR, startDate.getYear()); calendarStartDate.set(Calendar.MONTH, startDate.getMonth()); calendarStartDate.set(Calendar.DAY_OF_MONTH, startDate.getDate()); calendarEndDate.set(Calendar.YEAR, endDate.getYear()); calendarEndDate.set(Calendar.MONTH, endDate.getMonth()); calendarEndDate.set(Calendar.DAY_OF_MONTH, endDate.getDate()); long diff = calendarEndDate.getTimeInMillis() - calendarStartDate.getTimeInMillis(); interval = (int) (diff / (24 * 60 * 60 * 1000) + 1); // plus one day return interval; }
From source file:Main.java
public static String formatExpirationDate(String text) { try {/*from w ww .j a v a2s.co m*/ switch (text.length()) { case 1: int digit = Integer.parseInt(text); if (digit < 2) { return text; } else { return "0" + text + "/"; } case 2: int month = Integer.parseInt(text); if (month > 12 || month < 1) { // Invalid digit return text.substring(0, 1); } else { return text + "/"; } case 3: if (text.substring(2, 3).equalsIgnoreCase("/")) { return text; } else { text = text.substring(0, 2) + "/" + text.substring(2, 3); } case 4: Calendar now = getCurrentExpDate(); String currentYearStr = String.valueOf(now.get(Calendar.YEAR)); int yearDigit = Integer.parseInt(text.substring(3, 4)); int currentYearDigit = Integer.parseInt(currentYearStr.substring(2, 3)); if (yearDigit < currentYearDigit) { // Less than current year invalid return text.substring(0, 3); } else { return text; } case 5: // always make the year in the current century Calendar now2 = getCurrentExpDate(); String currentYearStr2 = String.valueOf(now2.get(Calendar.YEAR)); String yearStr = text.substring(0, 3) + currentYearStr2.substring(0, 2) + text.substring(3, 5); Date expiry = simpleDateFormat.parse(yearStr); if (expiry.before(now2.getTime())) { // Invalid exp date return text.substring(0, 4); } else { return text; } default: if (text.length() > 5) { return text.substring(0, 5); } else { return text; } } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } // If an exception is thrown we clear out the text return ""; }
From source file:DateUtils.java
/** * A method to get the current date as an array of components * 0 = year, 1 = month, 2 = day/*w w w . j av a2 s. c o m*/ * * @return an array of strings representing the current date */ public static String[] getCurrentDateAsArray() { GregorianCalendar calendar = new GregorianCalendar(); String[] fields = new String[3]; fields[0] = Integer.toString(calendar.get(Calendar.YEAR)); fields[1] = String.format("%02d", calendar.get(Calendar.MONTH) + 1); fields[2] = String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH)); return fields; }
From source file:DateUtils.java
public static String getDate() { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); return "" + year + "-" + month + "-" + day; }
From source file:Util.java
public static byte[] packDate(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date);// w w w . j a v a2s. c o m // Byte bits: 00000000 11111111 22222222 33333333 44444444 // Contents : 00YYYYYY YYYYYYMM MMDDDDDH HHHHMMMM MMSSSSSS byte[] bytes = new byte[5]; int s = c.get(Calendar.SECOND); int m = c.get(Calendar.MINUTE); int h = c.get(Calendar.HOUR_OF_DAY); int d = c.get(Calendar.DATE); int mm = c.get(Calendar.MONTH) + 1; int y = c.get(Calendar.YEAR); bytes[4] = (byte) ((m << 6) | s); bytes[3] = (byte) ((m >> 2) | (h << 4)); bytes[2] = (byte) ((h >> 4) | (d << 1) | (mm << 6)); bytes[1] = (byte) ((mm >> 2) | (y << 2)); bytes[0] = (byte) (y >> 6); return bytes; }
From source file:Main.java
public static Date getDateFromHexString(String hexString) { Calendar c = Calendar.getInstance(); int year = Integer.parseInt(hexString.substring(0, 2)) + 2000; int month = Integer.parseInt(hexString.substring(2, 4)); int day = Integer.parseInt(hexString.substring(4, 6)); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DAY_OF_MONTH, day); return c.getTime(); }