时间日期工具类
import android.text.format.DateUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* @author : fada
* Date : 2018/8/16
* Description : 时间日期工具类
*/
public class TimeUtils {
/**
* 是否是今天
*
* @param when
* @return
*/
public static boolean isToday(long when) {
return DateUtils.isToday(when);
}
/**
* “HH:MM:SS”格式(24时制)
*
* @return
*/
public static String hourMinterSecond() {
return hourMinterSecond(currentTimeMillis());
}
/**
* “HH:MM:SS”格式(24时制)
*
* @param date
* @return
*/
public static String hourMinterSecond(long date) {
return String.format(Locale.CHINA, "%tT", date);
}
/**
* 获取当前时间
*
* @return
*/
public static long currentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 一年中的天数(即年的第几天)
*
* @return
*/
public static String dayOfYear() {
return dayOfYear(currentTimeMillis());
}
/**
* 一年中的天数(即年的第几天)
*
* @param date
* @return
*/
public static String dayOfYear(long date) {
return String.format(Locale.CHINA, "%tj", date);
}
/**
* 2位数字24时制的小时(不足2位前面补0)
*
* @return
*/
public static String hour() {
return hour(currentTimeMillis());
}
/**
* 2位数字24时制的小时(不足2位前面补0)
*
* @param date
* @return
*/
public static String hour(long date) {
return String.format(Locale.CHINA, "%tH", date);
}
/**
* 2位数字的分钟(不足2位前面补0)
*
* @return
*/
public static String minter() {
return minter(currentTimeMillis());
}
/**
* 2位数字的分钟(不足2位前面补0)
*
* @param date
* @return
*/
public static String minter(long date) {
return String.format(Locale.CHINA, "%tM", date);
}
/**
* 2位数字的秒(不足2位前面补0)
*
* @return
*/
public static String second() {
return second(currentTimeMillis());
}
/**
* 2位数字的秒(不足2位前面补0)
*
* @param date
* @return
*/
public static String second(long date) {
return String.format(Locale.CHINA, "%tS", date);
}
/**
* 年月日 星期
*
* @return
*/
public static String yearMonthDayWeekday() {
return yearMonthDayWeekday(currentTimeMillis());
}
/**
* 年月日 星期
*
* @param date
* @return
*/
public static String yearMonthDayWeekday(long date) {
return String.format(Locale.CHINA, "%s年%s月%s日 %s", year(date), month(date), day(date),
weekday(date));
}
/**
* 4位数字的年份(不足4位前面补0)
*
* @param date
* @return
*/
public static String year(long date) {
return String.format(Locale.CHINA, "%tY", date);
}
/**
* 两位数字的月份(不足两位前面补0)
*
* @param date
* @return
*/
public static String month(long date) {
return String.format(Locale.CHINA, "%tm", date);
}
/**
* 两位数字的日(不足两位前面补0
*
* @param date
* @return
*/
public static String day(long date) {
return String.format(Locale.CHINA, "%td", date);
}
/**
* 星期的全称
*
* @param date
* @return
*/
public static String weekday(long date) {
return String.format(Locale.CHINA, "%tA", date);
}
/**
* 4位数字的年份(不足4位前面补0)
*
* @return
*/
public static String year() {
return year(currentTimeMillis());
}
/**
* 星期
*
* @return
*/
public static String weekday() {
return weekday(currentTimeMillis());
}
/**
* 获取当前时间
*
* @return
*/
public static Date getCurrent() {
return Calendar.getInstance().getTime();
}
/**
* 获取今天的日期
*
* @return
*/
public static String today() {
return yearMonthDay();
}
/**
* “年-月-日”格式
*
* @return
*/
public static String yearMonthDay() {
return yearMonthDay(currentTimeMillis());
}
/**
* “年-月-日”格式
*
* @param date
* @return
*/
public static String yearMonthDay(long date) {
return String.format(Locale.CHINA, "%tF", date);
}
/**
* 年-月-日 时:分
*
* @return
*/
public static String yearMonthDayHourMinter() {
return yearMonthDayHourMinter(currentTimeMillis());
}
/**
* 年-月-日 时:分
*
* @param date
* @return
*/
public static String yearMonthDayHourMinter(long date) {
return String.format(Locale.CHINA, "%s %s", yearMonthDay(date), hourMinter(date));
}
/**
* “HH:MM”格式(24时制)
*
* @param date
* @return
*/
public static String hourMinter(long date) {
return String.format(Locale.CHINA, "%tR", date);
}
/**
* “HH:MM”格式(24时制)
*
* @return
*/
public static String hourMinter() {
return hourMinter(currentTimeMillis());
}
}