博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android最新的工具DateHelper
阅读量:6038 次
发布时间:2019-06-20

本文共 9882 字,大约阅读时间需要 32 分钟。

最新的工具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:0703-12 15:02:07.102: I/System.out(11457): 2015-03-1203-12 15:02:07.102: I/System.out(11457): 2015-03-13 15:02:0703-12 15:02:07.102: I/System.out(11457): 2015-03-19 15:02:0703-12 15:02:07.102: I/System.out(11457): 2015-04-12 15:02:0703-12 15:02:07.102: I/System.out(11457): 2016-03-12 15:02:0703-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;    }

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

你可能感兴趣的文章
<html:form>标签
查看>>
除了《一无所有》,我一无所有
查看>>
每日英语:China Seeks to Calm Anxiety Over Rice
查看>>
C++中struct和class的区别 [转]
查看>>
C++ ofstream和ifstream详细用法
查看>>
Mysql 连接查询 Mysql支持的连接查询有哪些
查看>>
Hive Streaming 追加 ORC 文件
查看>>
打开Apache自带的Web监视器
查看>>
eclipse插件
查看>>
Android笔记:通过RadioGroup/RadioButton自定义tabhost的简单方法
查看>>
ELCSlider
查看>>
XCode工程中 Targets详解
查看>>
Ext.Msg.prompt的高级应用
查看>>
Postgres 中 to_char 格式化记录
查看>>
关于联合索引
查看>>
开源 java CMS - FreeCMS2.7 登录移动端管理中心
查看>>
Android FM模块学习之三 FM手动调频
查看>>
Python 设置系统默认编码以及其他编码问题大全
查看>>
解决端口占用问题
查看>>
kubernetes+docker监控之Docker监控——cadvisor
查看>>