android最新的工具DateHelper
最新的工具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的更多相关文章
- Xamarin Mono For Android 4.6.07004 完整离线安装破解版(C#开发Android、IOS工具)
Xamarin是由Miguel de Icaza成立的一家新的独立公司,目的是给Mono一个继续奋斗的机会.Mono for Android (原名:MonoDroid)可以让开发人员使用 Mic ...
- Android 反编译工具简介
Android 反编译工具: 所需工具:1 apktool : 用于获取资源文件 2 dex2Jar : 用于将classes.dex转化成jar文件 2 jd-gui: 将jar文件转化成java文 ...
- 正确使用Android性能分析工具——TraceView
http://blog.jobbole.com/78995/ 首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - Ja ...
- 【转】在Ubuntu上下载、编译和安装Android最新源代码
原文网址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Linux Kernel和Android有一定 ...
- 在Ubuntu上下载、编译和安装Android最新源码
看完了前面说的几本书之后,对Linux Kernel和Android有一定的认识了,是不是心里蠢蠢欲动,想小试牛刀自己编译一把Android源码了呢?一直习惯使用Windows系统,而Android源 ...
- 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...
- 在Ubuntu上下载、编译和安装Android最新源代码
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Lin ...
- Android最新源码4.3下载-教程 2013-11
Android最新源码4.3下载-教程 有的下载会出现问题: 需要 修改manifest.xml中的fetch: "git://Android.git.linaro.org/" ...
- Android Device Monitor工具的使用
在最新的Android Studio3.x版本中,已经去掉了Android Device Monitor工具,但是不代表Android Device Monitor工具就不能用了,找到sdk的目录: ...
随机推荐
- Eclipse插件引入jar包的方法
搞了两天,终于找到解决办法了.原来 Eclipse 插件项目引入外面的jar包不能用 build path---->add external jars的方法. 先说明两个概念:类加载器,O ...
- hdu 1542 Atlantis(段树&扫描线&面积和)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- selenium + firefox/chrome/phantomjs登陆之模拟点击
登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...
- Ubuntu--有关VMware Tools安装问题
虚拟机中找不到VMware Tools选项 在虚拟机上安装了ubuntu系统后,是不可以进行系统间数据共享的,也就是说我win7系统里的文件,不能拷贝到虚拟机的ubuntu系统. 解决方案:我们需要安 ...
- zoj - 3209 - Treasure Map(精确覆盖DLX)
题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...
- MYSQL经常使用命令列表
MYSQL经常使用命令列表 1.系统管理 mysql -h主机地址 -uusername -p 连接MYSQL(在mysql/bin) exit 退出MYSQL命令 mysqladmin -uuser ...
- Unity3D入门(二):碰撞检測
碰撞器由来 1.系统默认会给每一个对象(GameObject)加入一个碰撞组件(ColliderComponent),一些背景对象则能够取消该组件. 2.在unity3d中,能检測碰撞发生的方式有两种 ...
- Cacti监控Tomcatserver实现过程
1 首先去官网上面下载通用的监控模板 一般使用TomcatStats-0.1.zip 模板居多,下载地址: http://forums.cacti.net/download/file.php?id=1 ...
- CSharp Oracle 登陆
=======后台Oracle存储过程================ 1.创建表 --判读表存在先删除begin EXECUTE IMMEDIATE 'DROP TABLE student'; ...
- WPF点滴
1 设置窗体的最大化,而且无边框 <Style x:Key="WindowsStyle" TargetType="Window"> <Sett ...