Android 日常开发过程中日期时间处理

开发中,难免要对一些日期数据进行处理,下面一些方法仅供参考

一、常用日期常量

1.一天的时间  毫秒数表示  public static final long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;

2.一个小时    毫秒数表示   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;

}

总结: 对日期进行格式这里传染的是时间的毫秒值 然后对毫秒值进行格式化  根据不同需要输入对应格式化字符串即可

eg:"yyyy-MM-dd"、"yyyy/MM/dd HH:mm" 、"yyyy/MM/dd HH:mm" etc.另外需要注意的是 时间的显示问题HH是获取24小时制的  hh是获取12小时制的

B.常用方法工具

a.获取当前年份

//先获取当前系统时间 然后格式化成年份  得到年份的string类型的字符串  转换成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.获取当前系统时间

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.这个方法厉害了 (哈哈 )

//对传入时间进行处理 根据对应的情况返回不同的需要的用来展示的字符串 方法不难 有详细的注释

public static String formatConversationDate(long time) {

// 系统当前时间

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工具类  可以替换成自己的 或者改用系统的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, "转换后的毫秒值:" + 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, "转换后的毫秒值:" + millionSeconds);

return millionSeconds;

} catch (ParseException e) {

e.printStackTrace();

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

}// 毫秒

}

return 0;

}

f.结束时间是否大于开始时间

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;

}

总结 :这是本人项目开发中用到的方法 归类总结,有些方法不是最优,有不足之处  还望指正。

PS: 遗留一个讨论的问题吧  这个方法你 觉得对吗 与上面的方法Be2方法相比 有什么优劣?

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());

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容