最新的工具DateHelper

实用程序类,。的天数来获得一个给定的月份。过了几天去习惯或、周、一个月、日期等。。

代码例如以下:

import android.annotation.SuppressLint;
import android.text.TextUtils; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; @SuppressLint("SimpleDateFormat")
public class DateHelper { private static DateHelper util; public static DateHelper getInstance() { if (util == null) {
util = new DateHelper();
}
return util; }
private DateHelper() {
super();
} public SimpleDateFormat date_Formater_1 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"); public SimpleDateFormat date_Formater_2 = new SimpleDateFormat("yyyy-MM-dd"); public Date getDate(String dateStr) {
Date date = new Date();
if (TextUtils.isEmpty(dateStr)) {
return date;
}
try {
date = date_Formater_1.parse(dateStr);
return date;
} catch (ParseException e) {
e.printStackTrace(); }
return date; } public String getDataString_1(Date date) {
if (date == null) {
date = new Date();
}
String str = date_Formater_1.format(date);
return str; } public String getDataString_2(Date date) {
if (date == null) {
date = new Date();
}
String str = date_Formater_2.format(date);
return str; } /**
* 将日期变成常见中文格式
*
* @param date
* @return
*/
public String getRencentTime(String date) {
Date time = getDate(date);
if (time == null) {
return "一个月前";
}
String ftime = "";
Calendar cal = Calendar.getInstance(); String curDate = date_Formater_2.format(cal.getTime());
String paramDate = date_Formater_2.format(time);
if (curDate.equals(paramDate)) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
return ftime;
} long lt = time.getTime() / 86400000;
long ct = cal.getTimeInMillis() / 86400000;
int days = (int) (ct - lt);
if (days == 0) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
} else if (days == 1) {
ftime = "昨天";
} else if (days == 2) {
ftime = "前天";
} else if (days > 2 && days <= 10) {
ftime = days + "天前";
} else if (days > 10) {
ftime = "一个月前";
} else {
ftime = date_Formater_2.format(time);
}
return ftime;
} /**
* 日期时间格式转换
*
* @param typeFrom
* 原格式
* @param typeTo
* 转为格式
* @param value
* 传入的要转换的參数
* @return
*/
public String stringDateToStringData(String typeFrom, String typeTo,
String value) {
String re = value;
SimpleDateFormat sdfFrom = new SimpleDateFormat(typeFrom);
SimpleDateFormat sdfTo = new SimpleDateFormat(typeTo); try {
re = sdfTo.format(sdfFrom.parse(re));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return re;
} /**
* 得到这个月有多少天
*
* @param year
* @param month
* @return
*/
public int getMonthLastDay(int year, int month) {
if (month == 0) {
return 0;
}
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
} /**
* 得到年份
*
* @return
*/
public String getCurrentYear() {
Calendar c = Calendar.getInstance();
return c.get(Calendar.YEAR) + "";
} /**
* 得到月份
*
* @return
*/
public String getCurrentMonth() {
Calendar c = Calendar.getInstance();
return (c.get(Calendar.MONTH) + 1) + "";
} /**
* 获得当天的日期
*
* @return
*/
public String getCurrDay() {
Calendar c = Calendar.getInstance();
return c.get(Calendar.DAY_OF_MONTH) + "";
} /**
* 得到几天/周/月/年后的日期,整数往后推,负数往前移动
*
* @param calendar
* @param calendarType
* Calendar.DATE,Calendar.WEEK_OF_YEAR,Calendar.MONTH,Calendar.
* YEAR
* @param next
* @return
*/
public String getDayByDate(Calendar calendar, int calendarType, int next) { calendar.add(calendarType, next);
Date date = calendar.getTime();
String dateString = date_Formater_1.format(date);
return dateString; } }

用法

String dataStr = DateHelper.getInstance().getDataString_1(null);

        String toStringData = DateHelper.getInstance().stringDateToStringData("yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", dataStr);
String date = DateHelper.getInstance().getDayByDate(
Calendar.getInstance(), Calendar.DATE, 1);
String week = DateHelper.getInstance().getDayByDate(
Calendar.getInstance(), Calendar.WEEK_OF_YEAR, 1);
String month = DateHelper.getInstance().getDayByDate(
Calendar.getInstance(), Calendar.MONTH, 1);
String year = DateHelper.getInstance().getDayByDate(
Calendar.getInstance(), Calendar.YEAR, 1);
int lastDay = DateHelper.getInstance().getMonthLastDay(2015, 2);
System.out.println(dataStr);
System.out.println(toStringData);
System.out.println(date);
System.out.println(week);
System.out.println(month);
System.out.println(year);
System.out.println("2月有"+lastDay+"天");

打印结果

03-12 15:02:07.102: I/System.out(11457): 2015-03-12 15:02:07
03-12 15:02:07.102: I/System.out(11457): 2015-03-12
03-12 15:02:07.102: I/System.out(11457): 2015-03-13 15:02:07
03-12 15:02:07.102: I/System.out(11457): 2015-03-19 15:02:07
03-12 15:02:07.102: I/System.out(11457): 2015-04-12 15:02:07
03-12 15:02:07.102: I/System.out(11457): 2016-03-12 15:02:07
03-12 15:02:07.102: I/System.out(11457): 2月有28天

其他也可实现获取一段时间之前之后的日期的方法

非常久曾经找到别人写的代码

/**
* 得到一周前的日期
*
* @return
*/
public String lastWeek() {
Date date = new Date(); int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));
int day = Integer.parseInt(new SimpleDateFormat("dd").format(date)) - 6; if (day < 1) {
month -= 1;
if (month == 0) {
year -= 1;
month = 12;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
day = 30 + day;
} else if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
day = 31 + day;
} else if (month == 2) {
if (year == 0 || (year % 4 == 0 && year != 0))
day = 29 + day;
else
day = 28 + day;
}
}
String y = year + "";
String m = "";
String d = "";
if (month < 10)
m = "0" + month;
else
m = month + "";
if (day < 10)
d = "0" + day;
else
d = day + ""; return y + "-" + m + "-" + d;
} /**
* 获得一月前的日期
*
* @return
*/
public String lastMonth() {
Date date = new Date(); int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - 1;
int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
if (month == 0) {
year -= 1;
month = 12;
} else if (day > 28) {
if (month == 2) {
if (year == 0 || (year % 4 == 0 && year != 0)) {
day = 29;
} else
day = 28;
} else if ((month == 4 || month == 6 || month == 9 || month == 11)
&& day == 31) {
day = 30;
}
}
String y = year + "";
String m = "";
String d = "";
if (month < 10)
m = "0" + month;
else
m = month + "";
if (day < 10)
d = "0" + day;
else
d = day + ""; return y + "-" + m + "-" + d;
} /**
* 获得一年前的日期
*
* @return
*/
public String lastYear() {
Date date = new Date();
int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date)) - 1;
int month = Integer.parseInt(new SimpleDateFormat("MM").format(date));
int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
if (month == 0) {
year -= 1;
month = 12;
} else if (day > 28) {
if (month == 2) {
if (year == 0 || (year % 4 == 0 && year != 0)) {
day = 29;
} else
day = 28;
}
}
String y = year + "";
String m = "";
String d = "";
if (month < 10)
m = "0" + month;
else
m = month + "";
if (day < 10)
d = "0" + day;
else
d = day + ""; return y + "-" + m + "-" + d;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

android最新的工具DateHelper的更多相关文章

  1. Xamarin Mono For Android 4.6.07004 完整离线安装破解版(C#开发Android、IOS工具)

      Xamarin是由Miguel de Icaza成立的一家新的独立公司,目的是给Mono一个继续奋斗的机会.Mono for Android (原名:MonoDroid)可以让开发人员使用 Mic ...

  2. Android 反编译工具简介

    Android 反编译工具: 所需工具:1 apktool : 用于获取资源文件 2 dex2Jar : 用于将classes.dex转化成jar文件 2 jd-gui: 将jar文件转化成java文 ...

  3. 正确使用Android性能分析工具——TraceView

    http://blog.jobbole.com/78995/     首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - Ja ...

  4. 【转】在Ubuntu上下载、编译和安装Android最新源代码

    原文网址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Linux Kernel和Android有一定 ...

  5. 在Ubuntu上下载、编译和安装Android最新源码

    看完了前面说的几本书之后,对Linux Kernel和Android有一定的认识了,是不是心里蠢蠢欲动,想小试牛刀自己编译一把Android源码了呢?一直习惯使用Windows系统,而Android源 ...

  6. 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...

  7. 在Ubuntu上下载、编译和安装Android最新源代码

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Lin ...

  8. Android最新源码4.3下载-教程 2013-11

    Android最新源码4.3下载-教程 有的下载会出现问题: 需要 修改manifest.xml中的fetch: "git://Android.git.linaro.org/"  ...

  9. Android Device Monitor工具的使用

    在最新的Android Studio3.x版本中,已经去掉了Android Device Monitor工具,但是不代表Android Device Monitor工具就不能用了,找到sdk的目录: ...

随机推荐

  1. oracle 密码文件文件

    密码文件作用: 密码文件用于dba用户的登录认证. dba用户:具备sysdba和sysoper权限的用户,即oracle的sys和system用户. 本地登录: 1)操作系统认证: [oracle@ ...

  2. nohup sort -k1 -n -t$'\t' ./bigfile.16 -o./test/bigfile.16.ok &

    nohup sort -k1 -n -t$'\t' ./bigfile.16 -o./test/bigfile.16.ok &

  3. linux查找文件或字符串的命令

    1. linux下面用于查到的命令有哪些? 是不是有很多呀,这个我还没做过统计和调查,不过这篇博客只介绍grep与find的最基本应用. grep和find功能都是相当的强大,这里也只是介绍这两个命令 ...

  4. SVN最有效的方法打基线

    笔者:张克强    在微博上:张克强-敏捷307 2014/7/6 方法一来自于我的一条微博: 组织级scm建一个名为controlled的文件夹,当项目某文档通过评审后,组织级scm从项目文件夹下找 ...

  5. Spark SQL Catalyst源代码分析Optimizer

    /** Spark SQL源代码分析系列*/ 前几篇文章介绍了Spark SQL的Catalyst的核心运行流程.SqlParser,和Analyzer 以及核心类库TreeNode,本文将具体解说S ...

  6. 每日一小练——高速Fibonacci数算法

    上得厅堂,下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:高速Fibonacci数算法 内容:先说说Fibonacci数列,它的定义是数列:f1,f2....fn有例如以下规律: ...

  7. ZOJ 3635 Cinema in Akiba(线段树)

    Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is ful ...

  8. 再读TCP/IP网络7层协议

    随着工作的深入,每次读这7层协议,每次都有不同的理解. 分层名                                                               分层号   ...

  9. HTC one/M7电信802d 毒蛇ViperOne2.1.0/高级毒蛇工具/完美root,精简/更多自定义,稳定,流畅ROM

    ROM版本 HTC One/M7 802d ROM作者 雪狼团队·大盛 http://weibo.com/DaShengdd Android版本 Android 4.2.2 创建日期 2013.09. ...

  10. POJ 1018 【枚举+剪枝】.cpp

    题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...