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的目录: ...
随机推荐
- ThinkPHP 自动创建数据、自动验证、自动完成详细例子介绍(十九)
原文:ThinkPHP 自动创建数据.自动验证.自动完成详细例子介绍(十九) 1:自动创建数据 //$name=$_POST['name']; //$password=$_POST['password ...
- JavaFX2: 鼠标拖动选择和Ctrl+Shift连续区间选择的ListView
JavaFX2的ListView中的多选没有提供鼠标拖动选择的功能,同时按下Ctrl和Shift后连续的区间选中也不支持,以下代码用于处理这两个问题,细节见代码注释: import com.sun.j ...
- php 简易验证码(GD库)
论坛中为了防止灌水,出现了非常多的验证码的插件.如今这里介绍一个非常easy的自己定义验证码函数,这个验证码实现的原理就是通过php扩展的gd库来实现的. 给出百度百科对验证码的定义"验证码 ...
- Typings实现智能
在Visual Studio Code中通过Typings实现智能提示功能 前言 我们知道在IDE中代码的智能提示几乎都是标配,虽然一些文本编辑器也有一些简单的提示,但这是通过代码片段提供的.功能 ...
- achieve aop through xml
The main way to achive AOP is deploying a xml file. Now a xml file is presented to be a explanation ...
- LIS(最长的序列)和LCS(最长公共子)总结
LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1 ...
- 在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused
在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused [原创]在RHEL上安装Thrift(支持C++)的若干问题 2010年12月1 ...
- Gnu Linux下文件的字符编码及转换工具
/********************************************************************* * Author : Samson * Date ...
- 《Pro Android Graphics》读书笔记之第三节
Android Frame Animation: XML, Concepts and Optimization Frame Animation Concepts: Cels, Framerate, a ...
- 俄罗斯方块游戏JavaScript代码
JavaScript代码俄罗斯方块游戏 早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要是以注释的形式). 我用 ...