Java tutorial
/* * DateUtils.java * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Author: Winter Lau (javayou@gmail.com) * http://dlog4j.sourceforge.net */ package org.cs.basic.test.util; import java.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.apache.commons.lang.StringUtils; import net.sf.json.JSONObject; /** * * @author Winter Lau */ public class DateUtils extends org.apache.commons.lang.time.DateUtils { public static java.text.SimpleDateFormat dd = new java.text.SimpleDateFormat("yyyy-MM-dd"); public static java.text.SimpleDateFormat dd_min = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm"); public static java.text.SimpleDateFormat dd05 = new java.text.SimpleDateFormat("yyyy-MM-05"); /** * ? * @param date * @param time * @return */ public static Calendar mergeDateTime(Date date, Time time) { Calendar cal = Calendar.getInstance(); if (date != null) cal.setTime(date); if (time != null) { Calendar temp = Calendar.getInstance(); temp.setTime(time); cal.set(Calendar.HOUR_OF_DAY, temp.get(Calendar.HOUR_OF_DAY)); cal.set(Calendar.MINUTE, temp.get(Calendar.MINUTE)); cal.set(Calendar.SECOND, temp.get(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, temp.get(Calendar.MILLISECOND)); } return cal; } /** * * @param d1 * @param d2 * @return */ public static int diff_in_date(Date d1, Date d2, String type) { if (type.endsWith("day")) return (int) (d1.getTime() - d2.getTime()) / 86400000; else if (type.endsWith("hour")) return (int) (d1.getTime() - d2.getTime()) / 3600000; else if (type.endsWith("min")) return (int) (d1.getTime() - d2.getTime()) / 60000; else return (int) (d1.getTime() - d2.getTime()) / 86400000; } /** * * @param d1 * @param d2 * @return */ public static int diff_in_date(Date d1, Date d2) { d1 = toDate("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd").format(d1)); d2 = toDate("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd").format(d2)); return (int) ((d1.getTime() - d2.getTime()) / (1000 * 60 * 60 * 24)); //return (int)((d2.getTime()-d1.getTime())/(1000*60*60*24)); //return (int)(d1.getTime() - d2.getTime())/86400000; } /** * ?? * @param year * @param month * @param date * @return */ public static Calendar getDateBegin(int year, int month, int date) { Calendar begin_time = Calendar.getInstance(); begin_time.set(Calendar.YEAR, year); begin_time.set(Calendar.MONTH, month - 1); begin_time.set(Calendar.DATE, date); begin_time.set(Calendar.HOUR_OF_DAY, 0); begin_time.set(Calendar.MINUTE, 0); begin_time.set(Calendar.SECOND, 0); begin_time.set(Calendar.MILLISECOND, 0); return begin_time; } /** * * @param cal */ public static void resetTime(Calendar cal) { cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); } /**? * @param dateTime * @return */ public static Date beforeMonth(String dateTime) { Calendar now = Calendar.getInstance(); SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = simpledate.parse(dateTime); } catch (ParseException ex) { System.out.println("????" + ex.getMessage()); return null; } now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - 1; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); return now.getTime(); } /**? * @param dateTime * @return */ public static Date beforeMonth(Date dateTime) { Calendar now = Calendar.getInstance(); now.setTime(dateTime); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - 1; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); return now.getTime(); } /**? * @param dateTime * @return */ public static Date afterMonth(Date dateTime) { Calendar now = Calendar.getInstance(); now.setTime(dateTime); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) + 1; int day = 1; now.set(year, month, day); return now.getTime(); } public static String spc_deal(String input_date) { if (StringUtils.isBlank(input_date)) { return ""; } int min = getMin(input_date); int turn_to_min = turn_to_min(min); Date bDate = parse("yyyy-MM-dd HH:mm", input_date); Date aDate = todyDays_min(bDate, turn_to_min); return format("yyyy-MM-dd HH:mm", aDate); } /** * min * @param dateTime * @return */ public static int getMin(String dateTime) { Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateTime); } catch (ParseException e) { e.printStackTrace(); } Calendar now = Calendar.getInstance(); now.setTime(date); int min = now.get(Calendar.MINUTE); return min; } /** * hour * @param dateTime * @return */ public static int getHour(String dateTime) { Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateTime); } catch (ParseException e) { e.printStackTrace(); } Calendar now = Calendar.getInstance(); now.setTime(date); int hour = now.get(Calendar.HOUR_OF_DAY); return hour; } /** * day * @param dateTime * @return */ public static int getDay(Date dateTime) { Calendar now = Calendar.getInstance(); now.setTime(dateTime); int day = now.get(Calendar.DAY_OF_MONTH); return day; } /** * Month * @param dateTime * @return */ public static int getMonth(Date dateTime) { Calendar now = Calendar.getInstance(); now.setTime(dateTime); int month = now.get(Calendar.MONTH) + 1; return month; } /** * Year * @param dateTime * @return */ public static int getYear(Date dateTime) { Calendar now = Calendar.getInstance(); now.setTime(dateTime); int year = now.get(Calendar.YEAR); return year; } /** * ? 211? * @param dateTime * @return */ public static String getMD(Date dateTime) { return getMonth(dateTime) + "" + getDay(dateTime) + "?"; } /**? * * @param dateTime * @return */ public static Date beforeMonthFristDay(String dateTime) { java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); Calendar now = Calendar.getInstance(); Date date = null; try { date = df.parse(dateTime); } catch (ParseException ex) { System.out.println("????" + ex.getMessage()); } now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - 1; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); now.set(now.DATE, 1); return now.getTime(); } /**? * * @param date * @return */ public static String beforeMonthFristDay(Date date) { java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); Calendar now = Calendar.getInstance(); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - 1; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); now.set(now.DATE, 1); return df.format(now.getTime()); } /**?? * @param dateTime * @param num * @return */ public static String firstMonthDay(String dateTime, int num) { try { Date date = dd.parse(dateTime); Calendar now = Calendar.getInstance(); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - num; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); now.set(now.DATE, 1); return dd.format(now.getTime()); } catch (ParseException e) { e.printStackTrace(); } return dd.format(new Date()); } /**?? * @param dateTime * @param num * @return */ public static Date firstMonthDay(Date dateTime, int num) { Calendar now = Calendar.getInstance(); now.setTime(dateTime); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - num; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); now.set(now.DATE, 1); return now.getTime(); } /**?? * * @param dateTime * @return */ public static Date beforeMonthLastDay(String dateTime) { Calendar now = Calendar.getInstance(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = df.parse(dateTime); } catch (ParseException ex) { System.out.println("????" + ex.getMessage()); } now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - 1; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); // ?1? now.add(now.MONTH, 1); // 1??zhii now.set(now.DATE, 1); // 1????? now.add(now.DATE, -1); return now.getTime(); } /**?? * * @param date * @return */ public static String beforeMonthLastDay(Date date) { Calendar now = Calendar.getInstance(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) - 1; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); // ?1? now.add(now.MONTH, 1); // 1??zhii now.set(now.DATE, 1); // 1????? now.add(now.DATE, -1); return df.format(now.getTime()); } /**?? * @param date * @param m * @return */ public static Date monthLastDay(Date date, int m) { Calendar now = Calendar.getInstance(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) + m; int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); // ?1? now.add(now.MONTH, 1); // 1??zhii now.set(now.DATE, 1); // 1????? now.add(now.DATE, -1); return now.getTime(); } /** * @param date * @return */ public static int intOfDate(Date date, String famat) { SimpleDateFormat d = new SimpleDateFormat(famat); return Integer.valueOf(d.format(date)); } /**string * @param date * @return */ public static String stringOfDate(Date date) { if (date == null) { return null; } return dd.format(date); } /**?? * @param date * @param n * @return */ public static Date todyDays(Date date, int n) { Calendar now = Calendar.getInstance(); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH) + n; now.set(year, month, day); return now.getTime(); } /**?? * @param date * @param n * @return */ public static String todyDays(String date, int n) { return dd.format(todyDays(date(date), n)); } /** * 0?n */ public static Date todyDays_min(Date date, int n) { Calendar now = Calendar.getInstance(); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH); int hour = now.get(Calendar.HOUR_OF_DAY); now.set(year, month, day, hour, 0); now.add(Calendar.MINUTE, n); return now.getTime(); } // /** // * 0?n // */ // public static String todyDays_min(String date,int n){ // return dd_min.format(todyDays(date(date),n)); // } /**?n? */ public static Date hourDays(Date date, int n) { Calendar now = Calendar.getInstance(); now.setTime(date); /*int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day);*/ now.set(Calendar.HOUR, n); return now.getTime(); } /**date0==date1&&date2==date3 * @param date0 * @param date1 * @param date2 * @param date3 * @return */ public static boolean equalDate(Date date0, Date date1, Date date2, Date date3) { date0 = DateUtils.date(dd.format(date0)); date1 = DateUtils.date(dd.format(date1)); date2 = DateUtils.date(dd.format(date2)); date3 = DateUtils.date(dd.format(date3)); if (date0.getTime() == date1.getTime() && date2.getTime() == date3.getTime()) { return true; } return false; } /**date0?date1date2 * @param date0 * @param date1 * @param date2 * @return */ public static boolean equalDate(Date date0, Date date1, Date date2) { date0 = DateUtils.date(dd.format(date0)); date1 = DateUtils.date(dd.format(date1)); date2 = DateUtils.date(dd.format(date2)); if (date0.getTime() >= date1.getTime() && date0.getTime() <= date2.getTime()) { return true; } return false; } /**date0<=date1() * @param date0 * @param date1 * @return */ public static boolean equalDate(Date date0, Date date1) { date0 = DateUtils.date(dd.format(date0)); date1 = DateUtils.date(dd.format(date1)); if (date0.getTime() <= date1.getTime()) { return true; } return false; } /**date0<=date1 * @param date0 * @param date1 * @return */ public static boolean equalDateHms(Date date0, Date date1) { if (date0.getTime() <= date1.getTime()) { return true; } return false; } /**date0>date1() * @param date0 * @param date1 * @return */ public static boolean equalDateHms1(Date date0, Date date1) { if (date0.getTime() > date1.getTime()) { return true; } return false; } /**date0==date1 * @param date0 * @param date1 * @return */ public static boolean equalDateHm(Date date0, Date date1) { if (date0 == null || date1 == null) { return false; } if (date0.getTime() == date1.getTime()) { return true; } return false; } /**??date?5? * @param date * @return */ public static boolean equalDate05(Date date0) { date0 = DateUtils.date(dd05.format(date0)); Date date = new Date(); Date date1 = DateUtils.date(dd.format(date)); if (date0.getTime() < date1.getTime()) { return true; } return false; } /**date0=date1() * @param date0 * @param date1 * @return */ public static boolean equalDate1(Date date0, Date date1) { date0 = DateUtils.date(dd.format(date0)); date1 = DateUtils.date(dd.format(date1)); if (date0.getTime() == date1.getTime()) { return true; } return false; } /**?? * @param dateTime * @return */ public static Date monthLastDay(String dateTime) { Calendar now = Calendar.getInstance(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = df.parse(dateTime); } catch (ParseException ex) { System.out.println("????" + ex.getMessage()); } now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); // ?1? now.add(now.MONTH, 1); // 1??zhii now.set(now.DATE, 1); // 1????? now.add(now.DATE, -1); return now.getTime(); } /**?? * @param date * @return */ public static Date monthLastDay(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); // ?1? now.add(now.MONTH, 1); // 1??zhii now.set(now.DATE, 1); // 1????? now.add(now.DATE, -1); return now.getTime(); } /**,, * @param d * @return */ public static Date date(Date d) { String str_d = dd.format(d); try { return dd.parse(str_d); } catch (ParseException e) { e.printStackTrace(); return d; } } /**,, * @param d * @return */ public static Date date(String d) { Calendar now = Calendar.getInstance(); Date date = null; try { date = dd.parse(d); } catch (ParseException e) { System.out.println("????" + e.getMessage()); } now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); return now.getTime(); } /** * @param startDate * @param endDate * @return */ public static int getCompareDate(String startDate, String endDate) { try { Date date1 = dd.parse(startDate); Date date2 = dd.parse(endDate); long l = date2.getTime() - date1.getTime(); long d = l / (24 * 60 * 60 * 1000); return (int) d; } catch (ParseException e) { e.printStackTrace(); return 0; } } /** * @param startDate * @param endDate * @return */ public static int getCompareDate(Date startDate, Date endDate) { if (startDate == null || endDate == null) { return 0; } long l = endDate.getTime() - startDate.getTime(); long d = l / (24 * 60 * 60 * 1000); return (int) d; } /** * @param startDate * @param endDate * @return */ public static List listBetweenDate(String startDate, String endDate) { int i = 0, len = getCompareDate(startDate, endDate) + 1; List<String> list = new ArrayList<String>(); for (i = 0; i < len; i++) { Date date = date(startDate); date = todyDays(date(startDate), i); list.add(dd.format(date)); } return list; } /**? * @param d1 * @param d2 * @return */ public static long secondOfDate(Date d1, Date d2) { SimpleDateFormat fDate = new java.text.SimpleDateFormat("yyyyMMddHHmmss"); long l_d1 = Long.valueOf(fDate.format(d1)); long l_d2 = Long.valueOf(fDate.format(d2)); // System.out.println("l_d1="+l_d1+" l_d2="+l_d2); return l_d1 - l_d2; } /**? * @param date * @return */ public static int weekOfDay(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(Calendar.MONDAY); //1 c.setMinimalDaysInFirstWeek(4); //?4??1 c.setTime(date); int weeknum = c.get(Calendar.WEEK_OF_YEAR); int vyear = c.get(Calendar.YEAR); int vmonth = c.get(Calendar.MONTH) + 1; //1????1 if (vmonth == 1 && weeknum > 6) { vyear--; } //????1 if (vmonth == 12 && weeknum == 1) { vyear++; } /*DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(format.format(date)+",weeknum:"+weeknum+",year:"+vyear);*/ return weeknum; } /** * @param d * @return * @throws ParseException */ public static int getWeek(Date d) { Calendar cd = Calendar.getInstance(); cd.setTime(d); int mydate = cd.get(Calendar.DAY_OF_WEEK); String showDate = ""; int date_num = 0; switch (mydate) { case 1: showDate = ""; date_num = 7; break; case 2: showDate = ""; date_num = 1; break; case 3: showDate = ""; date_num = 2; break; case 4: showDate = ""; date_num = 3; break; case 5: showDate = ""; date_num = 4; break; case 6: showDate = ""; date_num = 5; break; default: showDate = ""; date_num = 6; break; } return date_num; } /** * @param d * @return * @throws ParseException */ public static String getWeekName(Date d) { Calendar cd = Calendar.getInstance(); cd.setTime(d); int mydate = cd.get(Calendar.DAY_OF_WEEK); String showDate = ""; int date_num = 0; switch (mydate) { case 1: showDate = ""; date_num = 7; break; case 2: showDate = ""; date_num = 1; break; case 3: showDate = ""; date_num = 2; break; case 4: showDate = ""; date_num = 3; break; case 5: showDate = ""; date_num = 4; break; case 6: showDate = ""; date_num = 5; break; default: showDate = ""; date_num = 6; break; } return showDate; } /** * @param d * @return * @throws ParseException */ public static String getWeekNameOther(Date d) { Calendar cd = Calendar.getInstance(); cd.setTime(d); int mydate = cd.get(Calendar.DAY_OF_WEEK); String showDate = ""; int date_num = 0; switch (mydate) { case 1: showDate = ""; date_num = 7; break; case 2: showDate = ""; date_num = 1; break; case 3: showDate = ""; date_num = 2; break; case 4: showDate = ""; date_num = 3; break; case 5: showDate = ""; date_num = 4; break; case 6: showDate = ""; date_num = 5; break; default: showDate = ""; date_num = 6; break; } return showDate; } /** * ??NUM 1 7 * @param weekName * @return */ public static int getNumByWeekName(String weekName) { int date_num = 0; if (weekName.equals("") || weekName.equals("")) date_num = 7; else if (weekName.equals("") || weekName.equals("")) date_num = 1; else if (weekName.equals("") || weekName.equals("")) date_num = 2; else if (weekName.equals("") || weekName.equals("")) date_num = 3; else if (weekName.equals("") || weekName.equals("")) date_num = 4; else if (weekName.equals("") || weekName.equals("")) date_num = 5; else if (weekName.equals("") || weekName.equals("")) date_num = 6; return date_num; } /**? * @param startDate * @param endDate * @return */ public static List<String> listDays(int orderBy, String startDate, String endDate) { List<String> list = new ArrayList<String>(); if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) { Date dateStart = date(startDate); Date dateEnd = date(endDate); if (orderBy == 1) { dateStart = date(endDate); dateEnd = date(startDate); } Date d = dateStart; list.add(dd.format(dateStart)); while (true) { d = todyDays(d, 1); if (d.getTime() > dateEnd.getTime()) { break; } list.add(dd.format(d)); } } return list; } /** * ?? * @param start ? 1 ?1990-01-01 12:00:00 * @param end ? 2 ?2009-01-01 12:00:00 * @return long[] {, , , } */ public static long[] differTimes(Date start, Date end) { long day = 0; long hour = 0; long min = 0; long sec = 0; if (start != null && end != null) { long time1 = start.getTime(); long time2 = end.getTime(); long diff; if (time1 < time2) { diff = time2 - time1; } else { diff = time1 - time2; } day = diff / (24 * 60 * 60 * 1000); hour = (diff / (60 * 60 * 1000) - day * 24); min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); } long[] times = { day, hour, min, sec }; return times; } /**?? * @param format * @param d * @return */ public static Date parse(String format, String d) { try { return new java.text.SimpleDateFormat(format).parse(d); } catch (ParseException e) { e.printStackTrace(); } return null; } /**? * @param format * @param d * @return */ public static String format(String format, Date d) { if (d == null) { return ""; } return new java.text.SimpleDateFormat(format).format(d); } /** * datestart,end * -1datestart? * 0datestartend * 1dateend? * @param date * @param start * @param end * @return */ public static int in_time_zone(Date date, Date start, Date end) { if (date == null || start == null || end == null) return 0; if (start.after(end)) {//start09:00:00,end02:00:00end if (start.after(date)) return -1; else return 0; } else { if (start.after(date)) return -1; else if (end.after(date)) return 0; } return 1; } /** * @param date * @param num * @return */ public static Date minuteDate(Date date, int num) { Calendar now = Calendar.getInstance(); now.setTime(date); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH); int day = now.get(Calendar.DAY_OF_MONTH); now.set(year, month, day); now.add(Calendar.MINUTE, num); return now.getTime(); } /** * @param start * @param end * @return */ public static long timeDiffer(Date start, Date end) { return end.getTime() - start.getTime(); } /**date0?date1date2 * @param date0 * @param date1 * @param date2 * @return */ public static boolean betweenDate(Date date0, Date date1, Date date2, String format) { date0 = DateUtils.parse(format, format(format, date0)); date1 = DateUtils.parse(format, format(format, date1)); date2 = DateUtils.parse(format, format(format, date2)); if (date0.getTime() >= date1.getTime() && date0.getTime() <= date2.getTime()) { return true; } return false; } /**? * @param time1 * @param time2 * @return */ public static boolean isDateDiff_2(String pattern, String time2, String time1) { Date d1 = parse(pattern, time1); Date d2 = parse(pattern, time2); long diff = d2.getTime() - d1.getTime(); if (diff > 0) { return true; } return false; } /**? * @param time1 * @param time2 * @return time1>time2 true */ public static boolean isDateDiff(Date d1, Date d2) { long diff = d1.getTime() - d2.getTime(); //Log.i(TAG, "diff="+diff); if (diff > 0) { return true; } return false; } //----------------------??StartDate,EndDate ------------------------------- /**? * @param pattern * @param date * @return */ public static Date toDate(String pattern, String date) { try { return new SimpleDateFormat(pattern).parse(date); } catch (ParseException e) { e.printStackTrace(); } return null; } // public static boolean betweenDateByTime(String StartDate, String EndDate) { Date startDate = toDate("yyyy-MM-dd HH:mm:ss", StartDate); Date endDate = toDate("yyyy-MM-dd HH:mm:ss", EndDate); Date newDate = new Date(); if (newDate.getTime() >= startDate.getTime() && newDate.getTime() <= endDate.getTime()) { return true; } return false; } //----------------------??StartDate,EndDate ------------------------------- /** * 1578 */ public static int formatMinBy15(int min) { int return_min = min; if (min > 0) { int remainder = min % 15; if (remainder >= 8) { return_min = min - remainder + 15; } else if (remainder <= 7) { return_min = min - remainder; } } return return_min; } //? public static int turn_to_min(int min) { if (min >= 0 && min <= 14) { return 0; } else if (min >= 15 && min <= 44) { return 30; } else if (min >= 45 && min <= 59) { return 60; } return 0; } /** * * @param early_week ?? * @param week_day * @return */ public static String getDateByWeekDay(int early_week, int week_day) { Calendar time = Calendar.getInstance(); if (week_day > -1 && week_day < 7) time.set(Calendar.DAY_OF_WEEK, week_day); if (early_week > -1 && early_week < 54) time.set(Calendar.WEEK_OF_YEAR, time.get(Calendar.WEEK_OF_YEAR) - early_week); SimpleDateFormat fDate = new java.text.SimpleDateFormat("yyyy-MM-dd"); return fDate.format(time.getTime()); } /** * ? */ public static JSONObject formatQuerryDate(String startTime, String endTime) { JSONObject date_info = new JSONObject(); String start_time = "";// String end_time = "";//? String start_time_his = "";//? String end_time_his = "";//?? if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) { Date today = new Date();//? Date lastDay = DateUtils.todyDays(today, -60);// Date inputStartTime = DateUtils.parse("yyyy-MM-dd", startTime);// Date inputEndTime = DateUtils.parse("yyyy-MM-dd", endTime);//? // if (DateUtils.equalDate(lastDay, inputStartTime)) { start_time = DateUtils.format("yyyy-MM-dd", inputStartTime); end_time = DateUtils.format("yyyy-MM-dd", inputEndTime); } //?? else if (DateUtils.equalDate(inputEndTime, lastDay)) { start_time_his = DateUtils.format("yyyy-MM-dd", inputStartTime); end_time_his = DateUtils.format("yyyy-MM-dd", inputEndTime); } // else { start_time = DateUtils.format("yyyy-MM-dd", lastDay); end_time = DateUtils.format("yyyy-MM-dd", inputEndTime); start_time_his = DateUtils.format("yyyy-MM-dd", inputStartTime); end_time_his = DateUtils.format("yyyy-MM-dd", DateUtils.todyDays(lastDay, -1)); } } date_info.put("start_time", start_time); date_info.put("end_time", end_time); date_info.put("start_time_his", start_time_his); date_info.put("end_time_his", end_time_his); return date_info; } }