Back to project page FrameLite.
The source code is released under:
GNU General Public License
If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.miku.framelite.utils; /*from w ww.j a va 2s.c o m*/ import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import android.text.TextUtils; /** * @author Administrator * DateUtils????? * ?????? */ public class DateUtils { public static final String YMD = "yyyyMMdd"; public static final String YMD_SLASH = "yyyy/MM/dd"; public static final String YMD_DASH = "yyyy-MM-dd"; public static final String YMD_DASH_WITH_TIME = "yyyy-MM-dd HH:mm:ss"; public static final String YDM_SLASH = "yyyy/dd/MM"; public static final String YDM_DASH = "yyyy-dd-MM"; public static final String HM = "HHmm"; public static final String HM_COLON = "HH:mm"; public static final long DAY = 24 * 60 * 60 * 1000L; public static final long SECOND= 24 * 60 * 60 * 24 * 60 * 1000L; public static final long ND = 1000 * 24 * 60 * 60;// ?????? public static final long NH = 1000 * 60 * 60;// ???????? public static final long NM = 1000 * 60;// ??????? public static final long NS = 1000;// ??????? private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>(); private DateUtils() { } /**?????? * @param pattern ?????????yyyy-MM-dd HH:mm:ss * @return DateFormat */ public static DateFormat getFormat(String pattern) { DateFormat format = DFS.get(pattern); if (format == null) { format = new SimpleDateFormat(pattern); DFS.put(pattern, format); } return format; } /** * @param source * @param pattern * @return Date */ public static Date parse(String source, String pattern) { if (source == null) { return null; } Date date; try { date = getFormat(pattern).parse(source); } catch (ParseException e) { return null; } return date; } /**????????? * @param date * @param pattern * @return ????????? */ public static String format(Date date, String pattern) { if (date == null) { return null; } return getFormat(pattern).format(date); } /** ????Timestamp * @param tt * @param pattern * @return */ public static String format(Timestamp tt,String pattern) { if(tt==null) { return null; } Date date=new Date(tt.getTime()); return getFormat(pattern).format(date); } /** * @param year * ? * @param month * ?(1-12) * @param day * ?(1-31) * @return ??????????????????? */ public static boolean isValid(int year, int month, int day) { if (month > 0 && month < 13 && day > 0 && day < 32) { // month of calendar is 0-based int mon = month - 1; Calendar calendar = new GregorianCalendar(year, mon, day); if (calendar.get(Calendar.YEAR) == year && calendar.get(Calendar.MONTH) == mon && calendar.get(Calendar.DAY_OF_MONTH) == day) { return true; } } return false; } /** Date????????Calendar?? * @param date * @return */ private static Calendar convert(Date date) { Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar; } /** * ??????????????? */ public static Date yearOffset(Date date, int offset) { return offsetDate(date, Calendar.YEAR, offset); } /** * ??????????????? */ public static Date monthOffset(Date date, int offset) { return offsetDate(date, Calendar.MONTH, offset); } /** * ??????????????? */ public static Date dayOffset(Date date, int offset) { return offsetDate(date, Calendar.DATE, offset); } /** * ????????????????? * * @param date * ?????? * @param field * ??????????? {@link Calendar} * @param offset * ????????????????????????????? * @return ????????? */ public static Date offsetDate(Date date, int field, int offset) { Calendar calendar = convert(date); calendar.add(field, offset); return calendar.getTime(); } /** * ?????????? */ public static Date firstDay(Date date) { Calendar calendar = convert(date); calendar.set(Calendar.DATE, 1); return calendar.getTime(); } /** * ????????????? */ public static Date lastDay(Date date) { Calendar calendar = convert(date); calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); return calendar.getTime(); } /** * ???????????? * * @param date1 * ?????? * @param date2 * ???? * @return ???????????????????????????????????????0????????????????????????????? */ public static int dayDiff(Date date1, Date date2) { long diff = date1.getTime() - date2.getTime(); return (int) (diff / DAY); } /** ????????????? * @param date1 * @param date2 * @return */ public static int secondDiff(Date date1, Date date2) { long diff = date1.getTime() - date2.getTime(); long day = diff / ND;// ?????? long min = diff % ND % NH / NM + day * 24 * 60;// ??????? return (int) min; } /** ????????????? * @param date1 * @param date2 * @return */ public static int secondDiff(String startTime, String endTime) { SimpleDateFormat sd = new SimpleDateFormat(YMD_DASH_WITH_TIME); long diff=0; long min=0; try { diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime(); long day = diff / ND;// ?????? min = diff % ND % NH / NM + day * 24 * 60;// ??????? } catch (ParseException e) { e.printStackTrace(); } return (int) min; } public static Long dateDiff(String startTime, String endTime,String format, String str) { // ?????????????simpledateformate?? SimpleDateFormat sd = new SimpleDateFormat(format); long nd = 1000 * 24 * 60 * 60;// ?????? long nh = 1000 * 60 * 60;// ???????? long nm = 1000 * 60;// ??????? long ns = 1000;// ??????? long diff; long day = 0; long hour = 0; long min = 0; long sec = 0; // ????????????? try { diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime(); day = diff / nd;// ?????? hour = diff % nd / nh + day * 24;// ???????? min = diff % nd % nh / nm + day * 24 * 60;// ??????? sec = diff % nd % nh % nm / ns;// ?????? // ???? //System.out.println("?????" + day + "?" + (hour - day * 24) + "???"+ (min - day * 24 * 60) + "??" + sec + "??"); //System.out.println("hour=" + hour + ",min=" + min); if (str.equalsIgnoreCase("h")) { return hour; } else { return min; } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (str.equalsIgnoreCase("h")) { return hour; } else { return min; } } public static String dateDiff(String startTime, String endTime) { String result=""; // ?????????????simpledateformate?? SimpleDateFormat sd = new SimpleDateFormat(YMD_DASH_WITH_TIME); long nd = 1000 * 24 * 60 * 60;// ?????? long nh = 1000 * 60 * 60;// ???????? long nm = 1000 * 60;// ??????? long ns = 1000;// ??????? long diff; long day = 0; long hour = 0; long min = 0; @SuppressWarnings("unused") long sec = 0; // ????????????? try { diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime(); day = diff / nd;// ?????? hour = diff % nd / nh + day * 24;// ???????? min = (hour*60)+(diff % nd % nh / nm + day * 24 * 60);// ??????? sec = diff % nd % nh % nm / ns;// ?????? // ???? if(min>60) { if(hour>60) { result=day+"?"; }else { result=hour+"???"; } }else{ result=min+"?"; } } catch (ParseException e) { e.printStackTrace(); } return result; } /** * ??2???date0?date1?????date0>date1 ??1???0?????-1 * @param date0 * @param date1 * @param format * @return */ public static int compare(String date0,String date1,String format){ SimpleDateFormat sd = new SimpleDateFormat(format); try{ long diff=sd.parse(date0).getTime()-sd.parse(date1).getTime(); if(diff>0){ return 1; }else if(diff<0){ return -1; }else{ return 0; } }catch(Exception e){ e.printStackTrace(); } boolean b1=false,b2=false; if(TextUtils.isEmpty(date0)){ b1=true; } if(TextUtils.isEmpty(date1)){ b2=true; } if(b1&&b2){ return 0; }else if(b1){ return -1; }else{ return 1; } } }