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的目录: ...
随机推荐
- NetBeans工具学习之道:NetBeans的(默认)快捷键
没什么好介绍的,是netbeans的快捷键,比較全面.看到好多坛子里还在问eclipse下的这个快捷键怎么netbeans下没有呢.曾经收集的,如今列在以下: 事实上,在当前安装的netbeans的 ...
- 欧拉计划·第四题
题目4:找出由两个三位数乘积构成的回文. 一个回文数指的是从左向右和从右向左读都一样的数字.最大的由两个两位数乘积构成的回文数是9009 = 91 * 99. 找出最大的有由个三位数乘积构成的回文数. ...
- android设置中的Preferencescreen使用方法介绍与分析
今天主要研究了一下设置中的Preferencescreen应用,它不仅可以作为设置界面显示,并且还可以启动activity,以下主要是对启动activity的介绍 1. Preferencescree ...
- ZooKeeper的安装、配置、启动和使用(一)——单机模式
ZooKeeper的安装.配置.启动和使用(一)——单机模式 ZooKeeper的安装非常简单,它的工作模式分为单机模式.集群模式和伪集群模式,本博客旨在总结ZooKeeper单机模式下如何安装.配置 ...
- UVA796- Critical Links(无向图中的桥梁)
题目链接 题意: 给出一个无向图,按顺序输出桥 思路:求出全部的桥,然后按顺序输出就可以 代码: #include <iostream> #include <cstdio> # ...
- CentOS 7 安装MySql Server 5.6
1. 安装MySql Server 在/etc/yum.repos.d/目录下添加以下文件mysql-community.repo文件,内容如下: [mysql56-community] name=M ...
- MyReport演示下载连接和相关文章索引
演示地址 Flex集成方式 (旧版2.6) HTML集成方式 MyReport产品站点 NEW 相关文章 ------------------------2.6下面版本号--------------- ...
- 4句代码读取Excel到DataSet(非Excel组件)
Toxy是继NPOI之后主推的还有一个项目,主要目的是为了解决文档的抽取问题.其支持的格式包括全部docx.xlsx.xls.csv.vcard等. 以下是一个简单但非常实用的样例 ParserCon ...
- Android的PackageManager的使用
Android系统提供了很多服务管理的类,包括ActivityManager.PowerManager(电源管理).AudioManager(音频管理)以及PackageManager管理类.Pack ...
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
1.基本概念 1.1.Spring Spring 是一个开源框架, Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作 Expert O ...