編輯:關於Android編程
提取了一些在開發過程中可能會用到的日期相關的函數作為工具類,供大家參考:
/** * 日期操作工具類. * * @author shimiso */ public class DateUtil { private static final String FORMAT = "yyyy-MM-dd HH:mm:ss"; public static Date str2Date(String str) { return str2Date(str, null); } public static Date str2Date(String str, String format) { if (str == null || str.length() == 0) { return null; } if (format == null || format.length() == 0) { format = FORMAT; } Date date = null; try { SimpleDateFormat sdf = new SimpleDateFormat(format); date = sdf.parse(str); } catch (Exception e) { e.printStackTrace(); } return date; } public static Calendar str2Calendar(String str) { return str2Calendar(str, null); } public static Calendar str2Calendar(String str, String format) { Date date = str2Date(str, format); if (date == null) { return null; } Calendar c = Calendar.getInstance(); c.setTime(date); return c; } public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss return date2Str(c, null); } public static String date2Str(Calendar c, String format) { if (c == null) { return null; } return date2Str(c.getTime(), format); } public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss return date2Str(d, null); } public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss if (d == null) { return null; } if (format == null || format.length() == 0) { format = FORMAT; } SimpleDateFormat sdf = new SimpleDateFormat(format); String s = sdf.format(d); return s; } public static String getCurDateStr() { Calendar c = Calendar.getInstance(); c.setTime(new Date()); return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND); } /** * 獲得當前日期的字符串格式 * * @param format * @return */ public static String getCurDateStr(String format) { Calendar c = Calendar.getInstance(); return date2Str(c, format); } // 格式到秒 public static String getMillon(long time) { return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time); } // 格式到天 public static String getDay(long time) { return new SimpleDateFormat("yyyy-MM-dd").format(time); } // 格式到毫秒 public static String getSMillon(long time) { return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time); } /* 輸入的是String,格式諸如20120102,實現加一天的功能,返回的格式為String,諸如20120103 */ public static String stringDatePlus(String row) throws ParseException{ String year=row.substring(0, 4); String month=row.substring(4,6); String day=row.substring(6); String date1=year+"-"+month+"-"+day; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date startDate=sdf.parse(date1); Calendar cd = Calendar.getInstance(); cd.setTime(startDate); cd.add(Calendar.DATE, 1); String dateStr =sdf.format(cd.getTime()); String year1=dateStr.substring(0,4); String month1=dateStr.substring(5,7); String day1=dateStr.substring(8); return year1+month1+day1; } /* 輸入的是String,格式諸如20120102,實現減一天的功能,返回的格式為String,諸如20120101 */ public static String stringDateDecrease(String row) throws ParseException{ String year=row.substring(0, 4); String month=row.substring(4,6); String day=row.substring(6); String date1=year+"-"+month+"-"+day; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date startDate=sdf.parse(date1); Calendar cd = Calendar.getInstance(); cd.setTime(startDate); cd.add(Calendar.DATE, -1); String dateStr =sdf.format(cd.getTime()); String year1=dateStr.substring(0,4); String month1=dateStr.substring(5,7); String day1=dateStr.substring(8); return year1+month1+day1; } /* 輸入的格式為String,諸如20120101,返回的格式為String,諸如2012-01-01 */ public static String stringDateChange(String date) { if(date.length()=="20120101".length()){ String year=date.substring(0, 4); String month=date.substring(4,6); String day=date.substring(6); return year+"-"+month+"-"+day; }else{ return date; } } /** * 日期向後推一天 * @param date 格式:20120101 * @return 20120102 */ public static String tonextday(String date){ int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day+1); Date newdate = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String da = format.format(newdate); return da; } public static void main(String[] args) { new StringHandle().previousWeekEndDayByDate("20130109"); } /** * 獲取當前日期上一周的開始日期 (周日) */ public static String previousWeekByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//設置一個星期的第一天,按中國的習慣一個星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根據日歷的規則,給當前日期減往星期幾與一個星期第一天的差值 cal.add(Calendar.WEEK_OF_YEAR, -1); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println("所在周星期日的日期:"+imptimeBegin); return imptimeBegin; } /** * 獲取當前日期上一周的結束日期 (周六) */ public static String previousWeekEndDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//設置一個星期的第一天,按中國的習慣一個星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s)); cal.add(Calendar.WEEK_OF_YEAR, -1); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println("星期六的日期:"+imptimeBegin); return imptimeBegin; } /** * 獲取當前日期當前一周的開始日期 (周日) */ public static String getCurrentWeekFirstDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//設置一個星期的第一天,按中國的習慣一個星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根據日歷的規則,給當前日期減往星期幾與一個星期第一天的差值 String imptimeBegin = sdf.format(cal.getTime()); // System.out.println("所在周星期日的日期:"+imptimeBegin); return imptimeBegin; } /** * 獲取當前日期當前一周的結束日期 (周六) */ public static String getCurrentWeekEndDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//設置一個星期的第一天,按中國的習慣一個星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s)); String imptimeBegin = sdf.format(cal.getTime()); return imptimeBegin; } /** * 返回上一個月的第一天 * @param date20120304 * @return 20120201 */ public static String previousMonthByDate(String date) { // TODO Auto-generated method stub int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-2, 1); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println(imptimeBegin); return imptimeBegin; } /** * 返回下一個月的第一天 * @param date20120304 * @return 20120401 */ public static String nextMonthByDate(String date) { // TODO Auto-generated method stub int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month, 1); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println(imptimeBegin); return imptimeBegin; } /** * 返回當前月的第一天 * @param date20120103 * @return 20120101 */ public static String getCurrentMonthFirstDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, 1); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); String imptimeBegin = sdf.format(cal.getTime()); return imptimeBegin; } }
一這是本人第一次寫博客 請多多關照第一次接觸到Android NDK 真是蒙圈 因為需要和Bluetooth下位機接觸 所以要在項目中鑲嵌Cpp 算法 然後總結了andr
我們首先來說一下傳統的Linux基於uid,gid的權限管理機制:1、用戶的uid gid gids:Ubuntu操作系統當前登陸的用戶是jltxgcy,那麼該用戶的ui
上一篇文章中我們講解了如何在android studio中進行單元測試。實際開發過程中有一些功能性的需求,比如測試工具類,測試數據存儲等測試工作,如果還是通過重復執行ap
本文講述Android自定義時間軸的實現過程,供大家參考,具體內容如下 相關視頻鏈接: Android自定義控件系列 http://edu.csdn.net/course