This commit is contained in:
renjianbo0118
2021-03-10 23:48:19 +08:00
parent 90471f4849
commit b5fb4c1a2b
13 changed files with 1029 additions and 52 deletions

View File

@@ -0,0 +1,574 @@
/*
* Copyright (C) 2016 android@19code.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* Create by h4de5ing 2016/5/7 007
* 日期工具类
*
*/
public class DateUtilss {
private static final String TAG = "InviteMessageFragment";
private static final SimpleDateFormat DATE_FORMAT_DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat DATE_FORMAT_TIME = new SimpleDateFormat("HH:mm:ss");
/**
* 日期格式yyyy-MM-dd HH:mm:ss
*/
public static final String DATE_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* 日期格式yyyy-MM-dd
*/
public static final String DATE_SHORT = "yyyy-MM-dd";
/**
* 日期格式yyyy-MM
*/
public static final String MONTH_SHORT = "yyyy-MM";
/**
* 日期格式 yyyyMMdd
*/
public static final String DATE_NORMAL = "yyyyMMdd";
/**
* 将字符串转位日期类型
*
* @param sdate
* @return
*/
public static Date toDate(String sdate) {
try {
return dateFormater.get().parse(sdate);
} catch (ParseException e) {
return null;
}
}
private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
// private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
// @Override
// protected SimpleDateFormat initialValue() {
// return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// }
// };
private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();
/**
* 判断是否为今天(效率比较高)
*
* @param day 传入的 时间 "2016-06-28 10:10:30" "2016-06-28" 都可以
* @return true今天 false不是
* @throws ParseException
*/
public static boolean IsToday(String day) throws ParseException {
Calendar pre = Calendar.getInstance();
Date predate = new Date(System.currentTimeMillis());
pre.setTime(predate);
Calendar cal = Calendar.getInstance();
Date date = getDateFormat().parse(day);
cal.setTime(date);
if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
int diffDay = cal.get(Calendar.DAY_OF_YEAR)
- pre.get(Calendar.DAY_OF_YEAR);
if (diffDay == 0) {
return true;
}
}
return false;
}
/**
* 判断是否为昨天(效率比较高)
*
* @param day 传入的 时间 "2016-06-28 10:10:30" "2016-06-28" 都可以
* @return true今天 false不是
* @throws ParseException
*/
public static boolean IsYesterday(String day) throws ParseException {
Calendar pre = Calendar.getInstance();
Date predate = new Date(System.currentTimeMillis());
pre.setTime(predate);
Calendar cal = Calendar.getInstance();
Date date = getDateFormat().parse(day);
cal.setTime(date);
if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
int diffDay = cal.get(Calendar.DAY_OF_YEAR)
- pre.get(Calendar.DAY_OF_YEAR);
if (diffDay == -1) {
return true;
}
}
return false;
}
public static SimpleDateFormat getDateFormat() {
if (null == DateLocal.get()) {
DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
}
return DateLocal.get();
}
/**
* 获取当前时间字符串
*
* @return
*/
public static String getNowString() {
return dateFormat(new Date());
}
public static Date getNowDate() {
return new Date();
}
/**
* 获取精确到秒的时间戳
* @param date
* @return
*/
public static int getSecondTimestampTwo(Date date){
if (null == date) {
return 0;
}
String timestamp = String.valueOf(date.getTime()/1000);
return Integer.valueOf(timestamp);
}
/**
* @description 获取两个时间相差多少秒
* @param
* @return
*/
public static int calLastedTime(Date startDate) {
long a = new Date().getTime();
long b = startDate.getTime();
int c = (int)((b - a) / 1000);
return c;
}
/**
* Java Date与String的相互转换
*/
public static Date stringToDate(String time) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* JDate转为String
*/
public static String dateToString(Date date) {
SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
String tiem = sformat.format(date);
return tiem;
}
/**
* 从时间格式化字符串
* 默认格式为yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String dateFormat(Date date) {
return dateFormat(date, DATE_LONG);
}
/**
* 从时间格式化字符串
*
* @param date
* @param format
* @return
*/
public static String dateFormat(Date date, String format) {
if (date == null) {
date = new Date();
}
if (format == null || format.equals("")) {
format = DATE_LONG;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 从字符串格式化时间
*
* @param dateStr
* @param format
* @return
*/
public static Date dateFromString(String dateStr, String format) {
if (!dateStr.equals("") && !format.equals("")) {
DateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 增加时间数
*
* @param date
* @param field
* @param interval
* @return
*/
public static Date addOnField(Date date, int field, int interval) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(field, interval);
return ca.getTime();
}
/**
* 获取某个块
*
* @param date
* @param field
* @return
*/
public static int getFieldOfDate(Date date, int field) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(field);
}
public static String formatDataTime(long date) {
return DATE_FORMAT_DATETIME.format(new Date(date));
}
public static String formatDate(long date) {
return DATE_FORMAT_DATE.format(new Date(date));
}
public static String formatTime(long date) {
return DATE_FORMAT_TIME.format(new Date(date));
}
public static String formatDateCustom(String beginDate, String format) {
return new SimpleDateFormat(format).format(new Date(Long.parseLong(beginDate)));
}
public static String formatDateCustom(Date beginDate, String format) {
return new SimpleDateFormat(format).format(beginDate);
}
public static Date string2Date(String s, String style) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.applyPattern(style);
Date date = null;
if (s == null || s.length() < 6) {
return null;
}
try {
date = simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String getTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
return cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
}
/**
* 获得当前日期
* @return
*/
public static String getDate() {
return DATE_FORMAT_DATE.format(System.currentTimeMillis());
}
/**
*获取月末日期
* @return
*/
public static String getLastDayOfMonth() {
Calendar cale = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String lastday;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = format.format(cale.getTime());
return lastday;
}
public static String getDateTime(){
return DATE_FORMAT_DATETIME.format(System.currentTimeMillis());
}
public static String getDateTime(String format){
return new SimpleDateFormat(format).format(System.currentTimeMillis());
}
public static long subtractDate(Date dateStart, Date dateEnd) {
return dateEnd.getTime() - dateStart.getTime();
}
public static Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
public static int getWeekOfMonth() {
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.WEEK_OF_MONTH);
return week - 1;
}
public static int getDayOfWeek() {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day == 1) {
day = 7;
} else {
day = day - 1;
}
return day;
}
public static String getNextDay(){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,1);
String nextDay = sf.format(calendar.getTime());
return nextDay;
}
public static String gettimenow() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
String now = sdf.format(new Date());
return now;
}
public static String gettime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
Log.d(TAG, "当前的系统时间==" + format);
return format;
}
private static String gettimethtee() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
return format;
}
public static String gettimetwo() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");// HH:mm:ss
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
Log.d(TAG, "当前的系统时间(年月日格式)==" + format);
return format;
}
public static String gettimethree() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月");// HH:mm:ss
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
Log.d(TAG, "当前的系统时间(年月格式)==" + format);
return format;
}
/**
* 判断时间是不是今天
* @param date
* @return 是返回true不是返回false
*/
public static boolean isNow(Date date) {
//当前时间
Date now = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
//获取今天的日期
String nowDay = sf.format(now);
//对比的时间
String day = sf.format(date);
return day.equals(nowDay);
}
/**
* 获取明天的时间
*
* @return
*/
public static String getTomorrowTime() {
//获取当前时间
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
// System.out.println("当前日期:"+sf.format(c.getTime()));
c.add(Calendar.DAY_OF_MONTH, 1);
// System.out.println("增加一天后日期:"+sf.format(c.getTime()));
return sf.format(c.getTime());
}
/**
* 将秒数转换为基于00:00的时间
* 如541=9*60+1表示09:01
* @param minutes
* @return
*/
public static String getTimeByMinutes(int minutes){
//处理小时
int hour = minutes / 60;
String hourTime = "";
if(hour >= 0 && hour < 10){
hourTime = "0" + hour;// 1 --> 01
}else if(hour >= 10 && hour < 24){
hourTime = "" + hour;
}else if(hour >= 24){
hourTime = "0" + (hour-24);
}
//处理分钟
int min = minutes % 60;
String minTime = "";
if(min >= 0 && min < 10){
minTime = "0" + min;// 1 --> 01
}else{
minTime = "" + min;
}
return hourTime + ":" + minTime;
}
/**
* 秒钟转换成这种格式的时间00:00:10
* @param time
* @return
*/
public static String secToTime(int time) {
String timeStr = null;
int hour = time / 3600;
int minute = time / 60 % 60;
int second = time % 60;
timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
return timeStr;
}
public static String unitFormat(int i) {
String retStr = null;
if (i >= 0 && i < 10)
retStr = "0" + Integer.toString(i);
else
retStr = "" + i;
return retStr;
}
/**
* 00:00:10 这种格式的时间转化成秒钟
* @param time
* @return
*/
public static int timeToSec(String time){
String[] timeArray = time.split(":");
int hour = Integer.parseInt(timeArray[0])*3600;
int min = Integer.parseInt(timeArray[1])*60;
int sec = Integer.parseInt(timeArray[2]);
return hour+min+sec;
}
/**
* 毫秒换算
* 将毫秒换算成天、时、分、秒
*
*/
public static String formatDuring(long mss) {
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
// return days + "天" + hours + "时" + minutes + "分"
// + seconds + "秒 ";
return days + "" + hours + ":" + minutes + ":"
+ seconds + " ";
}
}