public class TimeUtils {
public final static String FORMAT_YEAR = "yyyy";
public final static String FORMAT_MONTH_DAY = "MM月dd日";
public final static String FORMAT_DATE = "yyyy-MM-dd";
public final static String FORMAT_TIME = "HH:mm";
public final static String FORMAT_MONTH_DAY_TIME = "MM月dd日 hh:mm";
public final static String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm";
public final static String FORMAT_DATE1_TIME = "yyyy/MM/dd HH:mm";
public final static String FORMAT_DATE_TIME_SECOND = "yyyy/MM/dd HH:mm:ss";
private static final int YEAR = 365 * 24 * 60 * 60;// 年
private static final int MONTH = 30 * 24 * 60 * 60;// 月
private static final int DAY = 24 * 60 * 60;// 天
private static final int HOUR = 60 * 60;// 小时
private static final int MINUTE = 60;// 分钟
private static SimpleDateFormat sdf = new SimpleDateFormat();
//获取当前时间
public static String getNowDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
//获取当前时间
public static String getNowYmDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");// HH:mm:ss
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
//Day:日期字符串例如 2015-3-10 Num:需要减少的天数例如 7
public static String getDateStr(String day, int Num) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
Date nowDate = null;
try {
nowDate = df.parse(day);
} catch (ParseException e) {
e.printStackTrace();
}
//如果需要向后计算日期 -改为+
Date newDate2 = new Date(nowDate.getTime() - (long) Num * 24 * 60 * 60 * 1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
String dateOk = simpleDateFormat.format(newDate2);
return dateOk;
}
/**
* 日期转星期
*
* @param datetime
* @return
*/
public static String dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
//两个日期相差几天
public static long dateDiff(String startTime, String endTime, String format) {
// 按照传入的格式生成一个simpledateformate对象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
long nh = 1000 * 60 * 60;// 一小时的毫秒数
long nm = 1000 * 60;// 一分钟的毫秒数
long ns = 1000;// 一秒钟的毫秒数
long diff;
long day = 0;
try {
// 获得两个时间的毫秒时间差异
diff = sd.parse(endTime).getTime()
- sd.parse(startTime).getTime();
day = diff / nd;// 计算差多少天
long hour = diff % nd / nh;// 计算差多少小时
long min = diff % nd % nh / nm;// 计算差多少分钟
long sec = diff % nd % nh % nm / ns;// 计算差多少秒
// 输出结果
System.out.println("时间相差:" + day + "天" + hour + "小时" + min
+ "分钟" + sec + "秒。");
if (day >= 1) {
return day;
} else {
if (day == 0) {
return 1;
} else {
return 0;
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
public static String getWeek(String date) {
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = Integer.parseInt(date.substring(8, 10));
//获得一个日历
Calendar calendar = Calendar.getInstance();
//设置当前时间,月份是从0月开始计算
calendar.set(year, month - 1, day);
//星期表示1-7,是从星期日开始
int number = calendar.get(Calendar.DAY_OF_WEEK);
return getWeekDay(number);
}
private static String getWeekDay(int dayForWeek) {
if (dayForWeek == 1) {
return "周日";
} else if (dayForWeek == 2) {
return "周一";
} else if (dayForWeek == 3) {
return "周二";
} else if (dayForWeek == 4) {
return "周三";
} else if (dayForWeek == 5) {
return "周四";
} else if (dayForWeek == 6) {
return "周五";
} else if (dayForWeek == 7) {
return "周六";
} else {
return "";
}
}
public static String formatDate(String date) {
String dateFormat = null;
try {
dateFormat = date.substring(5, 7) + "月" + date.substring(8, 10) + "日";
} catch (Exception e) {
e.printStackTrace();
}
return dateFormat;
}
@SuppressLint("SimpleDateFormat")
public static String getTime(long date) {
SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
return format.format(date);
}
/**
* 根据时间戳获取描述性时间,如3分钟前,1天前
*
* @param timestamp 时间戳 单位为毫秒
* @return 时间字符串
*/
public static String getDescriptionTimeFromTimestamp(long timestamp) {
long currentTime = System.currentTimeMillis();
// 与现在时间相差秒数
long timeGap = (currentTime - timestamp) / 1000;
System.out.println("timeGap: " + timeGap);
String timeStr;
if (timeGap > YEAR) {
timeStr = timeGap / YEAR + "年前";
} else if (timeGap > MONTH) {
timeStr = timeGap / MONTH + "个月前";
} else if (timeGap > DAY) {// 1天以上
timeStr = timeGap / DAY + "天前";
} else if (timeGap > HOUR) {// 1小时-24小时
timeStr = timeGap / HOUR + "小时前";
} else if (timeGap > MINUTE) {// 1分钟-59分钟
timeStr = timeGap / MINUTE + "分钟前";
} else {// 1秒钟-59秒钟
timeStr = "刚刚";
}
return timeStr;
}
/**
* 获取当前日期的指定格式的字符串
*
* @param format 指定的日期时间格式,若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
*/
public static String getCurrentTime(String format) {
if (format == null || format.trim().equals("")) {
sdf.applyPattern(FORMAT_DATE_TIME);
} else {
sdf.applyPattern(format);
}
return sdf.format(new Date());
}
/**
* date类型转换为String类型
* formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
* data Date类型的时间
*/
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
/**
* long类型转换为String类型
* currentTime要转换的long类型的时间
* formatType要转换的string类型的时间格式
*/
public static String longToString(long currentTime, String formatType) {
String strTime;
// long类型转成Date类型
Date date = longToDate(currentTime, formatType);
// date类型转成String
strTime = dateToString(date, formatType);
return strTime;
}
/**
* string类型转换为date类型
* strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
* HH时mm分ss秒,
* strTime的时间格式必须要与formatType的时间格式相同
*/
public static Date stringToDate(String strTime, String formatType) {
SimpleDateFormat formatter = new SimpleDateFormat(formatType);
Date date = null;
try {
date = formatter.parse(strTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/**
* long转换为Date类型
* currentTime要转换的long类型的时间
* formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
*/
public static Date longToDate(long currentTime, String formatType) {
// 根据long类型的毫秒数生命一个date类型的时间
Date dateOld = new Date(currentTime);
// 把date类型的时间转换为string
String sDateTime = dateToString(dateOld, formatType);
// 把String类型转换为Date类型
Date date = stringToDate(sDateTime, formatType);
return date;
}
/**
* string类型转换为long类型
* strTime要转换的String类型的时间
* formatType时间格式
* strTime的时间格式和formatType的时间格式必须相同
*/
public static long stringToLong(String strTime, String formatType) {
// String类型转成date类型
Date date = stringToDate(strTime, formatType);
if (date == null) {
return 0;
} else {
// date类型转成long类型
return dateToLong(date);
}
}
/**
* date类型转换为long类型
* date要转换的date类型的时间
*/
public static long dateToLong(Date date) {
return date.getTime();
}
}
关于时间、日期的常用方法
最新推荐文章于 2021-02-05 10:59:07 发布