Android開発でよく使われるツール類の整理
10881 ワード
個人労働の成果を尊重してください.出所を明記して転載してください.ありがとうございます.http://blog.csdn.net/xiaxiazaizai01/article/details/52102747
開発の過程でコードを簡単に再利用するために、いつも使っている方法を簡単に包装して、次の利用に便利です.開発でよく使われているツール類の整理を共有します.
時間ツールクラス
後は引き続き更新します.
開発の過程でコードを簡単に再利用するために、いつも使っている方法を簡単に包装して、次の利用に便利です.開発でよく使われているツール類の整理を共有します.
時間ツールクラス
/**
*
*/
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;
}
}
最後に後は引き続き更新します.