List of usage examples for java.util Calendar DATE
int DATE
To view the source code for java.util Calendar DATE.
Click Source Link
get
and set
indicating the day of the month. From source file:Main.java
public static String getSpecifiedDayBefore(String specifiedDay) { Calendar c = Calendar.getInstance(); Date date = null;//from w ww . ja v a 2 s . c o m try { date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay); } catch (ParseException e) { e.printStackTrace(); } c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day - 1); String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); return dayBefore; }
From source file:Main.java
public static String getDateFromUTC(long timestamp) { if (timestamp == 0) timestamp = System.currentTimeMillis(); Date date = new Date(timestamp); Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.setTime(date);/* www . j a v a2 s . c om*/ String val = (cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) + (cal.get(Calendar.AM_PM) == 0 ? "AM" : "PM")); // final Date currentTime = new Date(); // final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm:ss a z"); // sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // String ret = "UTC time: " + sdf.format(currentTime); // System.out.println(ret); return val; }
From source file:Main.java
public static String[] getWeekDays() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); String[] weekDays = new String[7]; for (int i = 0; i < 7; i++) { weekDays[i] = WEEK_DAYS_FORMAT.format(calendar.getTime()); calendar.add(Calendar.DATE, 1); }//from www. j a v a 2 s . c o m return weekDays; }
From source file:Main.java
public static String[] getShortWeekDays() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); String[] weekDays = new String[7]; for (int i = 0; i < 7; i++) { weekDays[i] = SHORT_WEEK_DAYS_FORMAT.format(calendar.getTime()); calendar.add(Calendar.DATE, 1); }// w w w . ja va 2 s . c o m return weekDays; }
From source file:Main.java
public static long CompareTimeDelta(String arrivalTime) { Calendar calendarToday = Calendar.getInstance(); Calendar calendarTrainArrival = Calendar.getInstance(); long millisecondsCurrent = calendarToday.getTimeInMillis(); long millisecondsTrainArrival; int nowYear = calendarToday.get(Calendar.YEAR); int nowMonth = calendarToday.get(Calendar.MONTH); int nowDay = calendarToday.get(Calendar.DATE); int evalhr = 0; int evalmin = 0; int evalsec = 0; String delims = "[:]"; String[] timesplit = arrivalTime.split(delims); evalhr = Integer.parseInt(timesplit[0]); //HR evalmin = Integer.parseInt(timesplit[1]); //MIN evalsec = Integer.parseInt(timesplit[2]); //SEC - VRE does not provide a schedule down to seconds, so ignore. calendarTrainArrival.set(nowYear, nowMonth, nowDay, evalhr, evalmin); millisecondsTrainArrival = calendarTrainArrival.getTimeInMillis(); return millisecondsCurrent - millisecondsTrainArrival; }
From source file:Main.java
public static boolean CoversFYStart(Calendar one, Calendar two, Calendar fy_start) throws Exception { if (one == null || two == null) throw new Exception("CoversFYStart: Null dates passed in"); if (fy_start == null) throw new Exception("CoversFYStart: Null FY Start"); int fy_month = fy_start.get(Calendar.MONTH); int fy_day = fy_start.get(Calendar.DAY_OF_MONTH); Calendar cal = (Calendar) one.clone(); while (true) { cal.add(Calendar.DATE, 1); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); if (month == fy_month && day == fy_day) break; }//w w w. j a v a2s . com return cal.before(two); }
From source file:Main.java
private static Calendar prepareCalendar(int hourOfDay, int minute) { Calendar calNow = Calendar.getInstance(); Calendar calSet = (Calendar) calNow.clone(); calSet.set(Calendar.HOUR_OF_DAY, hourOfDay); calSet.set(Calendar.MINUTE, minute); calSet.set(Calendar.SECOND, 0); calSet.set(Calendar.MILLISECOND, 0); if (calSet.compareTo(calNow) <= 0) { // Today Set time passed, count to tomorrow calSet.add(Calendar.DATE, 1); }/* w ww. jav a 2 s .c o m*/ return calSet; }
From source file:Main.java
public static String getCurrentDateString(Calendar calendar) { if (calendar == null) return ""; return calendar.get(Calendar.YEAR) + "-" + String.format("%02d", (calendar.get(Calendar.MONTH) + 1)) + "-" + String.format("%02d", calendar.get(Calendar.DATE)); }
From source file:Main.java
public static boolean areSameDays(long time1, long time2) { Calendar calendar = calendarThreadLocal.get(); if (calendar == null) { calendar = Calendar.getInstance(); calendarThreadLocal.set(calendar); }/* w w w .j a va2 s.c o m*/ calendar.setTimeInMillis(time1 * 1000L); int y1 = calendar.get(Calendar.YEAR); int m1 = calendar.get(Calendar.MONTH); int d1 = calendar.get(Calendar.DATE); calendar.setTimeInMillis(time2 * 1000L); int y2 = calendar.get(Calendar.YEAR); int m2 = calendar.get(Calendar.MONTH); int d2 = calendar.get(Calendar.DATE); return y1 == y2 && m1 == m2 && d1 == d2; // return (time1 / (60 * 60 * 24)) == (time2 / (60 * 60 * 24)); }
From source file:Main.java
/** * @param period : today, this_week, this_month, this_year, none * @return a date filter string for making query. e.g. date >= 2013-1-1 for this_year * return null if period is none.//w w w .j a v a 2 s.c om */ @SuppressWarnings("deprecation") public static String getDateFilterString(String period) { if (period == null) return null; Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; // 1 - 12 int date = calendar.get(Calendar.DATE); // 1 - 31 String dateFilterString = null; if (period.equalsIgnoreCase("today")) { dateFilterString = "date >= " + year + "-" + month + "-" + date; } else if (period.contains("week")) { int offset = calendar.get(Calendar.DAY_OF_WEEK) - calendar.getFirstDayOfWeek(); long timeInMillis = calendar.getTimeInMillis() - offset * 24 * 60 * 60 * 1000; Date firstDay = new Date(timeInMillis); dateFilterString = "date >= " + (firstDay.getYear() + 1900) + "-" + firstDay.getMonth() + "-" + firstDay.getDate(); } else if (period.contains("month")) { dateFilterString = "date >= " + year + "-" + month + "-1"; } else if (period.contains("year")) { dateFilterString = "date >= " + year + "-1-1"; } return dateFilterString; }