Android 日常開發(fā)過程中日期時間處理

開發(fā)中,難免要對一些日期數(shù)據(jù)進行處理,下面一些方法僅供參考

一、常用日期常量

1.一天的時間 ?毫秒數(shù)表示 ?public static final long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;

2.一個小時 ? ?毫秒數(shù)表示 ??public static final long ONE_HOUR_MILLIS = 60 * 60 * 1000;

3.一分鐘 ? ? ? ?毫秒表示 ? ? ??public static final long ONE_MINUTE_MILLIS = 60 * 1000;

二、常用格式方法

A.日期格式化問題

1.格式成xx月xx日

public static String formatDayDate(long time) {

SimpleDateFormat formatDay = new SimpleDateFormat("MM月dd日",

Locale.getDefault());

Date date = new Date(time);

return formatDay.format(date);

}

2.格式化成xxxx年-xx月xx日

public static String formatYYMMDD(long time) {

Date date = new Date(time);

SimpleDateFormat formatDay = new SimpleDateFormat("yyyy-MM-dd",

Locale.getDefault());

String day = formatDay.format(date);

return day;

}

總結(jié): 對日期進行格式這里傳染的是時間的毫秒值 然后對毫秒值進行格式化 ?根據(jù)不同需要輸入對應格式化字符串即可

eg:"yyyy-MM-dd"、"yyyy/MM/dd HH:mm" 、"yyyy/MM/dd HH:mm" etc.另外需要注意的是 時間的顯示問題HH是獲取24小時制的 ?hh是獲取12小時制的

B.常用方法工具

a.獲取當前年份

//先獲取當前系統(tǒng)時間 然后格式化成年份 ?得到年份的string類型的字符串 ?轉(zhuǎn)換成int即可(你想要String 更改下返回值類型)

public static int getCurrentYear() {

long time = System.currentTimeMillis();

Date date = new Date(time);

//獲取當前年份

SimpleDateFormat formatDay = new SimpleDateFormat("yyyy",Locale.getDefault());

//獲取當前月份

//SimpleDateFormat formatDay = new SimpleDateFormat("MM",Locale.getDefault());

//獲取當天

//SimpleDateFormat formatDay = new SimpleDateFormat("dd", Locale.getDefault());

String year = formatDay.format(date);

int result = Integer.parseInt(year);

return result;

}

b.獲取當前系統(tǒng)時間

1.public static String getCurrentTime(){

long currentTimeMillis = System.currentTimeMillis();

return?formatYYMMDD(currentTimeMillis);//A 2方法

}

2. public static String getNowDate() {

long time = new Date().getTime();

return String.valueOf(time);

}

c.這個方法厲害了 (哈哈 )

//對傳入時間進行處理 根據(jù)對應的情況返回不同的需要的用來展示的字符串 方法不難 有詳細的注釋

public static String formatConversationDate(long time) {

// 系統(tǒng)當前時間

Date currentTime = new Date();

Date targetTime = new Date(time);

// 返回的時間

String formatTime = "";

// 按照年月日格式化

SimpleDateFormat formatDay = new SimpleDateFormat("yyyy-MM-dd",

Locale.getDefault());

// 年

SimpleDateFormat formatYear = new SimpleDateFormat("yyyy",

Locale.getDefault());

// 計算在一年中的第幾天

SimpleDateFormat formatDayOfYear = new SimpleDateFormat("DD",

Locale.getDefault());

String currentDay = formatDay.format(currentTime);

String targetDay = formatDay.format(targetTime);

String currentYear = formatYear.format(currentTime);

String targetYear = formatYear.format(targetTime);

if (currentDay.equals(targetDay)) {

// 判斷是否當天

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm",

Locale.getDefault());

formatTime = sdf.format(targetTime);

return "今天 " + formatTime;

} else if (currentYear.equals(targetYear)) {

// 同年

int currentDayOfYear = Integer.parseInt(formatDayOfYear

.format(currentTime));

int targetDayOfYear = Integer.parseInt(formatDayOfYear

.format(targetTime));

// 判斷是否昨天

if (currentDayOfYear - 1 == targetDayOfYear) {

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm",

Locale.getDefault());

formatTime = sdf.format(targetTime);

return "昨天 " + formatTime;

} else {

// 同年同月不同天,返回月日

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd ? HH:mm",

Locale.getDefault());

formatTime = sdf.format(targetTime);

}

} else {

// 隔年

SimpleDateFormat format = new SimpleDateFormat(

"yyyy-MM-dd ? HH:mm", Locale.getDefault());

formatTime = format.format(targetTime);

}

return formatTime;

}

d.獲取當天為今年的第多少天

//DBug 是我自己封裝的一個log工具類 ?可以替換成自己的 或者改用系統(tǒng)的Log

public static int getCurrentDayOfYear(String time) {

Calendar calendar = getCalenar(time);

DBug.i(TAG,

"月:" + calendar.get(Calendar.YEAR) + "*** 日 = "

+ calendar.get(Calendar.DAY_OF_YEAR));

return calendar.get(Calendar.DAY_OF_YEAR);

}

e.獲取指定日期的時間毫秒值

//獲取?2016-08-24 的毫秒值方法

1.public static long getDateLongMils(String timeStr) {

String moth = "";

String d = "";

if (!TextUtils.isEmpty(timeStr)) {

try {

int year = Integer.parseInt(timeStr.split("-")[0]);

int month = Integer.parseInt(timeStr.split("-")[1]);

int day = Integer.parseInt(timeStr.split("-")[2]);

if (month < 10) {

moth = "0" + month;

} else {

moth = "" + month;

}

if (day < 10) {

d = "0" + day;

} else {

d = day + "";

}

String date = year + "" + moth + "" + d + "";

DBug.i(TAG, date);

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

long millionSeconds = sdf.parse(date).getTime();

DBug.i(TAG, "轉(zhuǎn)換后的毫秒值:" + millionSeconds);

return millionSeconds;

} catch (ParseException e) {

e.printStackTrace();

DBug.e(TAG, "ParseException:" + e.toString());

}// 毫秒

}

return 0;

}

//獲取2017年1月24日 的毫秒值

2.public static long getTimeLongMils(String timeStr) {

String moth = "";

String d = "";

if (!TextUtils.isEmpty(timeStr)) {

try {

// 將初始日期時間2012年07月02日 16:45 拆分成年 月 日 時 分 秒

String dates = CommonUtilty.spliteString(timeStr, "日", "index",

"front"); // 日期

String yearStr = CommonUtilty.spliteString(dates, "年", "index",

"front"); // 年份

String monthAndDay = CommonUtilty.spliteString(dates, "年",

"index", "back"); // 月日

String monthStr = CommonUtilty.spliteString(monthAndDay, "月",

"index", "front"); // 月

String dayStr = CommonUtilty.spliteString(monthAndDay, "月",

"index", "back"); // 日

DBug.i(TAG, "yearStr == " + yearStr + "***monthStr == "

+ monthStr + "***dayStr == " + dayStr);

int year = Integer.parseInt(yearStr);

int month = Integer.parseInt(monthStr);

int day = Integer.parseInt(dayStr);

if (month < 10) {

moth = "0" + month;

} else {

moth = "" + month;

}

if (day < 10) {

d = "0" + day;

} else {

d = day + "";

}

String date = year + "" + moth + "" + d + "";

DBug.i(TAG, date);

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

long millionSeconds = sdf.parse(date).getTime();

DBug.i(TAG, "轉(zhuǎn)換后的毫秒值:" + millionSeconds);

return millionSeconds;

} catch (ParseException e) {

e.printStackTrace();

DBug.e(TAG, "ParseException:" + e.toString());

}// 毫秒

}

return 0;

}

f.結(jié)束時間是否大于開始時間

public static boolean isRightDate(String endDate, String startDate) {

try {

if (!TextUtils.isEmpty(endDate) && !TextUtils.isEmpty(startDate)) {

long end = getTimeLongMils(endDate);

long start = getTimeLongMils(startDate);

if ((end - start) > 0) {

return true;

} else {

return false;

}

} else {

}

} catch (Exception e) {

}

return false;

}

總結(jié) :這是本人項目開發(fā)中用到的方法 歸類總結(jié),有些方法不是最優(yōu),有不足之處 ?還望指正。

PS: 遺留一個討論的問題吧 ?這個方法你 覺得對嗎 與上面的方法Be2方法相比 有什么優(yōu)劣?

public static long getTimeLongMils(Strings) {

//String s="2016年12月9日";

int n_pos=s.indexOf('u5e74');

int y_pos=s.indexOf('u6708');

int r_pos=s.indexOf('u65e5');

int n_int=Integer.valueOf(s.substring(0,n_pos)).intValue();

int y_int=Integer.valueOf(s.substring(n_pos+1,y_pos)).intValue();

int r_int=Integer.valueOf(s.substring(y_pos+1,r_pos)).intValue();

java.text.DateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

java.sql.Timestamp stime=new java.sql.Timestamp(n_int-1900,y_int-1,r_int,0,0,0,0);

System.out.println("timestamp = " +stime);

System.out.println("format time= " + formatter.format(stime));

System.out.println("format time millis= " + formatter.format(stime.getTime()));

System.out.println("millis = " + stime.getTime());

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容