編輯:關於Android編程
在開發過程中我們為了代碼簡單重用,常常會對經常用的方法進行一下簡單封裝,方便下次使用。下面就分享一下開發中常用的一些工具類的整理。
/**
* 日期操作的工具類
*/
public class DateUtil {
private DateUtil(){
}
/**
* 初始化一些時間設置格式
*/
public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
public static final SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
public static final SimpleDateFormat FORMAT_TIME = new SimpleDateFormat("HH:mm",Locale.CHINA);
private static final Calendar INSTANCE = Calendar.getInstance();
public static int getYear(){
return INSTANCE.get(Calendar.YEAR);
}
public static int getMonth(){
return INSTANCE.get(Calendar.MONTH);
}
public static int getDay(){
return INSTANCE.get(Calendar.DAY_OF_MONTH);
}
public static int getHours(){
return INSTANCE.get(Calendar.HOUR_OF_DAY);
}
public static int getMinutes(){
return INSTANCE.get(Calendar.MINUTE);
}
public static int getSeconds(){
return INSTANCE.get(Calendar.SECOND);
}
/**
* 獲取N天前、N天後的 日期
*
* @param nowDate
* 當前日期;
* @param dayAddNum
* 正數:N天前,負數:N天後;
* @return
*/
public static Date getAddDay(Date nowDate, int dayAddNum) {
Calendar tday = new GregorianCalendar();
tday.setTime(nowDate);
tday.add(Calendar.DAY_OF_MONTH, dayAddNum);
Date preDate = tday.getTime();
return preDate;
}
/**
* 獲取N天前、N天後的 日期
* @param nowDate 當前時間戳;
* @param dayAddNum 正數:N天前,負數:N天後;
* @return
*/
public static Date getAddDay(long nowDate, int dayAddNum){
return getAddDay(new Date(nowDate), dayAddNum);
}
/**
* 時間格式化
* @param format
* @param time
* @return
*/
public static String formatDate(String format,Long time) {
return formatDate(new SimpleDateFormat(format, Locale.CHINA), time);
}
/**
* 時間格式化
* @param format
* @param time
* @return
*/
public static String formatDate(SimpleDateFormat format,Long time) {
if(null==time || time<=0) return "";
return format.format(new Date(String.valueOf(time).length() == 13 ? time : time * 1000));
}
/**
* 將秒轉換為( 分:秒 格式)
* @param time 單位:秒
* @return
*/
public static String getTimeFromInt(int time) {
if (time <= 0) return "00:00";
int secondnd = time / 60;
int million = time % 60;
String f = secondnd >= 10 ? String.valueOf(secondnd) : "0" + String.valueOf(secondnd);
String m = million >= 10 ? String.valueOf(million) : "0" + String.valueOf(million);
return f + ":" + m;
}
/**
* 根據月份獲得最大天數
* @param year 年
* @param month 月
* @return 最大天數
*/
public static int getMaxDayByMonth(int year,int month){
Calendar time=Calendar.getInstance();//使用默認時區和語言環境獲得一個日歷
//注意:在使用set方法之前,必須先調用clear(),否則很多信息會繼承自系統當前的時間
time.clear();
time.set(Calendar.YEAR,year);
time.set(Calendar.MONTH,month);//注意Calendar對象默認一月是為零的
int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//獲得本月份的天數
return day;
}
/**
* 根據日期獲得星期
*/
public static String getWeek(Date d) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" };
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0)
dayOfWeek = 0;
return (dayNames[dayOfWeek]);
}
/**
* 時間格式化(剛剛、幾分鐘前、幾小時前、昨天、前天、年-月-日)
* @param time
* @return
*/
public static String getShortTime(long time) {
String shortstring = "";
if (time == 0)
return shortstring;
long now = Calendar.getInstance().getTimeInMillis();
long datetime = (now - time) / 1000;
if (datetime > 365 * 24 * 60 * 60) {
shortstring = FORMAT_DATE.format(new Date(time));
} else if (datetime > 24 * 60 * 60 && ((int) (datetime / (24 * 60 * 60)))==2){
shortstring = "前天";
} else if (datetime > 24 * 60 * 60 && ((int) (datetime / (24 * 60 * 60)))==1){
shortstring = "昨天";
} else if (datetime > 60 * 60) {
shortstring = (int) (datetime / (60 * 60)) + "小時前";
} else if (datetime > 60) {
shortstring = (int) (datetime / (60)) + "分鐘前";
} else {
shortstring = "剛剛";
}
return shortstring;
}
/**
* 時間格式化(秒,分,小時,天)
* @param time
* @return
*/
public static String getShortTime2(long time) {
String shortstring = "";
if (time == 0)
return shortstring;
long now = Calendar.getInstance().getTimeInMillis();
long datetime = (now - time) / 1000; // 秒
if (datetime > 24 * 60 * 60) {
shortstring = (int) (datetime / (24 * 60 * 60)) + "天";
} else if (datetime > 60 * 60) {
shortstring = (int) (datetime / (60 * 60)) + "小時";
} else if (datetime > 60) {
shortstring = (int) (datetime / (60)) + "分鐘";
} else {
shortstring = (int) datetime + "秒";
}
return shortstring;
}
/**
* 獲取當前日期上一周的開始日期 (周日)
*/
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());
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());
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());
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;
}
/**
* 返回上一個月的第一天
*
* @return 20120201
*/
public static String previousMonthByDate(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 - 2, 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;
}
/**
* 返回下一個月的第一天
*
* @return 20120401
*/
public static String nextMonthByDate(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);
Date newdate = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
String imptimeBegin = sdf.format(cal.getTime());
return imptimeBegin;
}
/**
* 返回當前月的第一天
*
* @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;
}
}
IntentService定義IntentService繼承與Service,用來處理異步請求。客戶端可以通過startService(Intent)方法傳遞請求給Int
Android通用流行框架大全1. 緩存DiskLruCacheJava實現基於LRU的磁盤緩存2.圖片加載Android Universal Image Loader一
一、Android Camera可以做哪些?1、功能拍攝相片 視頻錄制 取景器(掃描類應用,如人臉識別,名片識別,條形碼識別)2、根據Camera API實現自己的拍照程
TabLayout的默認樣式: app:theme=@style/Widget.Design.TabLayout從系統定義的該樣式繼續深入: <style name