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的目录: ...
随机推荐
- VC生成的DLL给QT的EXE调用时lib路径问题小结
VC生成的DLL给QT调用,有两种方式,一种是隐式调用调用(使用.lib文件方式): ① 在*.pro工程文件中添加VC生成的lib文件路径时,或者使用一个绝对路径,如: LIBS += " ...
- heapq
heapq-Guest-ChinaUnix博客 假设你需要维护一个列表,这个列表不断有新的元素加入,你需要在任何时候很方便的得到列表中的最大(小)值,因此要求列表始终处于排序完毕状态,怎么办呢 最简单 ...
- NavigationBar 隐藏底部边线,阴影
NavigationBar 底部默认有一条边线 假设项目中须要隐藏何以採用这个库 https://github.com/samwize/UINavigationBar-Addition/
- 三框架:使用数据源dbcp注意
使用spring整合hibernate时间,需要使用该数据源,数据源使用apache的dbcp,使用dbcp当需要依靠pool的jar包.选择dbcp和pool当你需要注意. DBCP 2 compi ...
- Erlangserver紧内存优化解决方案
提出的问题:server100万人在线,16G内存快被吃光. 玩家进程占用内存偏高 解决方法: 第一步: erlang:system_info(process_count). 查看进程数目是否正常,是 ...
- A Game of Thrones(5) - Jon
There were times—not many, but a few—when Jon Snow was glad he was a bastard. As he filled his wine ...
- hdu2089(数位dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间[a,b]内不含有62或4的数的个数. 分析:数位dp,dp[pos][0]表示到第 ...
- 从mina中学习超时程序编写
从mina中学习超时程序编写 在很多情况下,程序需要使用计时器定,在指定的时间内检查连接过期.例如,要实现一个mqtt服务,为了保证QOS,在服务端发送消息后,需要等待客户端的ack,确保客户端接收到 ...
- KSImageNamed-Xcode
KSImageNamed-Xcode 非常的给力的XCODE图片浏览插件; What is this? Can't remember whether that image you just add ...
- 讨论asp.net通过机器cookie仿百度(google)实现搜索input搜索提示弹出框自己主动
为实现自己主动弹出通过用户输入关键词相关的搜索结果,在这里,我举两个解决方案,对于两个不同的方案. 常用的方法是建立一个用户数据库中查找关系表.然后输入用户搜索框keyword异步调用数据表中的相关数 ...