热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

androidutils工具类,[Android][工具类]TimeUtils

importandroid.text.TextUtils;importandroid.util.Log;importjava.text.ParseException;importj

import android.text.TextUtils;

import android.util.Log;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.regex.Pattern;

/**

* 日期时间格式封装 工具类

*

*/

public class TimeUtils {

public static final String TIME_FORMAT_STYLE_DIANDIAN = "yyyy.MM.dd";

public static final String TIME_FORMAT_STYLE_YMD = "yyyy/MM/dd HH:mm";

public static final String Time_FORMAT_YEAD = "yyyy年MM月dd日 HH:mm";

public static final String Time_POSITION = "yyyy/MM/dd";

public static final String TIME_FORMAT_STYLE_MD = "MM月dd日 HH:mm";

private final static Pattern emailer = Pattern

.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

// private final static SimpleDateFormat dateFormater = new

// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// private final static SimpleDateFormat dateFormater2 = new

// SimpleDateFormat("yyyy-MM-dd");

private final static ThreadLocal dateFormater = new ThreadLocal() {

@Override

protected SimpleDateFormat initialValue() {

return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

}

};

private final static ThreadLocal dateFormater2 = new ThreadLocal() {

@Override

protected SimpleDateFormat initialValue() {

return new SimpleDateFormat("yyyy-MM-dd");

}

};

private final static ThreadLocal dateFormater3 = new ThreadLocal() {

@Override

protected SimpleDateFormat initialValue() {

return new SimpleDateFormat("yyyy-MM");

}

};

private final static ThreadLocal dateFormater4 = new ThreadLocal() {

@Override

protected SimpleDateFormat initialValue() {

return new SimpleDateFormat("yyyy-MM-dd");

}

};

/**

* 将字符串转为日期类型

*

* @param sdate

* @return

*/

public static Date toDate(String sdate) {

Date d = new Date();

d.setTime(Long.parseLong(sdate) * 1000);

return d;

}

/**

* 友好方式显示日期

*

* @param sdate

* @return

*/

public static String friendly_time(String sdate) {

Date time = toDate(sdate);

Log.i("toDate", time.toString());

return friendly_time1(time);

}

/**

* 以友好的方式显示时间

*

* @param time

* @return

*/

public static String friendly_time1(Date time) {

if (time == null) {

return "Unknown";

}

Date serviceTime = new Date();

if (serviceTime == null) {

return "Unknown1";

}

String ftime = "";

// 判断是否是同一天

String curDate = dateFormater2.get().format(serviceTime.getTime());

String paramDate = dateFormater2.get().format(time);

if (curDate.equals(paramDate)) {

int hour = (int) ((serviceTime.getTime() - time.getTime()) / 3600000);

if (hour == 0)

ftime = Math.max(

(serviceTime.getTime() - time.getTime()) / 60000, 1)

+ "分钟前";

else

ftime = hour + "小时前";

return ftime;

}

long lt = time.getTime() / 86400000;

long ct = serviceTime.getTime() / 86400000;

int days = (int) (ct - lt);

if (days == 0) {

int hour = (int) ((serviceTime.getTime() - time.getTime()) / 3600000);

if (hour == 0)

ftime = Math.max(

(serviceTime.getTime() - time.getTime()) / 60000, 1)

+ "分钟前";

else

ftime = hour + "小时前";

} else if (days == 1) {

ftime = "昨天";

} else if (days == 2) {

ftime = "前天";

} else if (days > 2 && days <&#61; 10) {

ftime &#61; (days - 1) &#43; "天前";

} else if (days > 10) {

ftime &#61; dateFormater2.get().format(time);

}

return ftime;

}

/**

* 判断给定字符串时间是否为今日

*

* &#64;param sdate

* &#64;return boolean

*/

public static boolean isToday(String sdate) {

boolean b &#61; false;

Date time &#61; toDate(sdate);

Date today &#61; new Date();

if (time !&#61; null) {

String nowDate &#61; dateFormater2.get().format(today);

String timeDate &#61; dateFormater2.get().format(time);

if (nowDate.equals(timeDate)) {

b &#61; true;

}

}

return b;

}

/**

* 判断给定字符串时间是否为今日

*

* &#64;param sdate

* &#64;return boolean

*/

public static boolean isThisMonth(String sdate) {

boolean b &#61; false;

Date time &#61; toDate(sdate);

Date today &#61; new Date();

if (time !&#61; null) {

String nowDate &#61; dateFormater3.get().format(today);

String timeDate &#61; dateFormater3.get().format(time);

if (nowDate.equals(timeDate)) {

b &#61; true;

}

}

return b;

}

/**

* 判断是否为今年&#xff0c;必须固定格式如下 &#xff1a;2014-01-01

*

* &#64;param time 如&#xff1a;2014-01-01

* &#64;return

*/

public static boolean isThisYear(String time) {

if (time.length() !&#61; 10) {

return false;

} else {

Date today &#61; new Date();

String nowDate &#61; dateFormater2.get().format(today);

if (nowDate.substring(0, 4).equals(time.substring(0, 4))) {

return true;

} else {

return false;

}

}

}

/**

* 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串&#xff0c;返回true

*

* &#64;param input

* &#64;return boolean

*/

public static boolean isEmpty(String input) {

if (input &#61;&#61; null || "".equals(input))

return true;

for (int i &#61; 0; i

char c &#61; input.charAt(i);

if (c !&#61; &#39; &#39; && c !&#61; &#39;\t&#39; && c !&#61; &#39;\r&#39; && c !&#61; &#39;\n&#39;) {

return false;

}

}

return true;

}

/**

* 格式化显示时间

* &#64;param dateStr yyyy-MM-dd HH:mm:ss

* &#64;return

*/

// public static String formatDate(String dateStr) {

//

// SimpleDateFormat sdf &#61; new SimpleDateFormat("yyyy-MM-dd HH:mm");

// Date date;

// try {

// date &#61; sdf.parse(dateStr);

// } catch (ParseException e) {

// date &#61; new Date();

// e.printStackTrace();

// }

// String needStr &#61; sdf.format(date);

// return needStr;

// }

/**

* &#64;param dateStr

* &#64;param format

* &#64;return

*/

public static Date parseDate(String dateStr, String format) {

SimpleDateFormat sdf &#61; new SimpleDateFormat(format);

Date date;

try {

date &#61; sdf.parse(dateStr);

} catch (ParseException e) {

date &#61; new Date();

e.printStackTrace();

}

return date;

}

/**

* 格式化时间

*

* &#64;param time

* &#64;param format

* &#64;return

*/

public static String formatTime(long time, String format) {

if (time &#61;&#61; 0 || TextUtils.isEmpty(format)) {

return "";

}

SimpleDateFormat sdf &#61; new SimpleDateFormat(format.toString());

Date date &#61; new Date(time);

String dateStr &#61; sdf.format(date);

return dateStr;

}

/**

* 格式化时间

*

* &#64;return

*/

public static String formatTime(String strTime) {

long time &#61; Long.parseLong(strTime);

if (time &#61;&#61; 0 || TextUtils.isEmpty(TIME_FORMAT_STYLE_YMD)) {

return "";

}

SimpleDateFormat sdf &#61; new SimpleDateFormat(TIME_FORMAT_STYLE_YMD.toString());

Date date &#61; new Date(time * 1000);

String dateStr &#61; sdf.format(date);

return dateStr;

}

/**

* 格式化时间

*

* &#64;return

*/

public static String formatTime(String strTime, String format) {

long time &#61; Long.parseLong(strTime);

if (time &#61;&#61; 0 || TextUtils.isEmpty(format)) {

return "";

}

SimpleDateFormat sdf &#61; new SimpleDateFormat(format.toString());

Date date &#61; new Date(time * 1000);

String dateStr &#61; sdf.format(date);

return dateStr;

}

/*时间戳转换成字符窜*/

static SimpleDateFormat sf;

public static String getDateToString(long time) {

Date d &#61; new Date(time);

sf &#61; new SimpleDateFormat("yyyy年MM月dd日");

return sf.format(d);

}

public static String formatTimeMonth(String strTime) {

long time &#61; Long.parseLong(strTime);

if (time &#61;&#61; 0 || TextUtils.isEmpty(TIME_FORMAT_STYLE_MD)) {

return "";

}

SimpleDateFormat sdf &#61; new SimpleDateFormat(TIME_FORMAT_STYLE_MD.toString());

Date date &#61; new Date(time * 1000);

String dateStr &#61; sdf.format(date);

return dateStr;

}

/**

* 根据年 月 获取对应的月份 天数

*/

public static int getDaysByYearMonth(int year, int month) {

Calendar cal &#61; Calendar.getInstance();

cal.set(Calendar.YEAR, year);

cal.set(Calendar.MONTH, month - 1);

cal.set(Calendar.DATE, 1);

cal.roll(Calendar.DATE, -1);

int maxDate &#61; cal.get(Calendar.DATE);

return maxDate;

}

/**

* 将字符串转为日期类型

*

* &#64;param sdate

* &#64;return

*/

public static Date toDate2(String sdate) {

try {

if (sdate.contains("/")) {

return dateFormater2.get().parse(sdate);

} else {

return dateFormater4.get().parse(sdate);

}

} catch (ParseException e) {

return null;

}

}

/**

* 获取当前年月日

*

* &#64;return

*/

public static String getCurrentTime() {

Date d &#61; new Date();

SimpleDateFormat sdf &#61; new SimpleDateFormat("yyyy-MM-dd");

String dateNowStr &#61; sdf.format(d);

return dateNowStr;

}

/**

* 获取当前年月日

*

* &#64;return

*/

public static long getCurrentLongTime() {

return System.currentTimeMillis() / 1000;

}

}



推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了GregorianCalendar类的基本信息,包括它是Calendar的子类,提供了世界上大多数国家使用的标准日历系统。默认情况下,它对应格里高利日历创立时的日期,但可以通过调用setGregorianChange()方法来更改起始日期。同时,文中还提到了GregorianCalendar类为每个日历字段使用的默认值。 ... [详细]
  • React基础篇一 - JSX语法扩展与使用
    本文介绍了React基础篇一中的JSX语法扩展与使用。JSX是一种JavaScript的语法扩展,用于描述React中的用户界面。文章详细介绍了在JSX中使用表达式的方法,并给出了一个示例代码。最后,提到了JSX在编译后会被转化为普通的JavaScript对象。 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • PHP中的单例模式与静态变量的区别及使用方法
    本文介绍了PHP中的单例模式与静态变量的区别及使用方法。在PHP中,静态变量的存活周期仅仅是每次PHP的会话周期,与Java、C++不同。静态变量在PHP中的作用域仅限于当前文件内,在函数或类中可以传递变量。本文还通过示例代码解释了静态变量在函数和类中的使用方法,并说明了静态变量的生命周期与结构体的生命周期相关联。同时,本文还介绍了静态变量在类中的使用方法,并通过示例代码展示了如何在类中使用静态变量。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 小程序wxs中的时间格式化以及格式化时间和date时间互转
    本文介绍了在小程序wxs中进行时间格式化操作的问题,并提供了解决方法。同时还介绍了格式化时间和date时间的互相转换的方法。 ... [详细]
author-avatar
手机用户2502903077
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有