SimpleDateFormat 简单使用
原创
©著作权归作者所有:来自51CTO博客作者小粥ha的原创作品,请联系作者获取转载授权,否则将追究法律责任
yyyy:年
MM:月
dd:日
hh:1~12小时制(1-12)
HH:24小时制(0-23)
mm:分
ss:秒
S:毫秒
E:星期几
D:一年中的第几天
F:一月中的第几个星期(会把这个月总共过的天数除以7)
w:一年中的第几个星期
W:一月中的第几星期(会根据实际情况来算)
a:上下午标识
k:和HH差不多,表示一天24小时制(1-24)。
K:和hh差不多,表示一天12小时制(0-11)。
z:表示时区
Eg: yyyy-MM-dd HH:mm:ss
public void getFormatTime() {
Date date = new Date();
String strDateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
System.out.println(sdf.format(date)); // 2021-03-27 21:13:23
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm aa");
simpleDateFormat.format(new Date(mRunTime));//11:00 AM
}
1.获取手机时间(24小时、12小时制)自动转换
//获取HH:mm 格式时间
public String getFormatHourTime() {
boolean is24HourFormat = DateFormat.is24HourFormat(getActivity());
Calendar mCalendar = Calendar.getInstance();
mCalendar.setTimeInMillis(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(is24HourFormat ? "HH:mm" : "hh:mm aa");
return simpleDateFormat.format(System.currentTimeMillis());
}
2.获取当前时间(HH:mm),不带秒
//获取HH:mm:ss 格式时间
public String getFormatCurrentTime() {
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("H:mm");
return simpleDateFormat.format(new Date()) + ":00";
}
3.判断是不是同一天
//判断是不是同一天
public boolean isEqualHour(long mNewRunTime, long mOldRunTime) {
Calendar old = Calendar.getInstance();
old.setTimeInMillis(mNewRunTime);
int i = old.get(Calendar.DAY_OF_MONTH);
Calendar newca = Calendar.getInstance();
newca.setTimeInMillis(mOldRunTime);
int i1 = newca.get(Calendar.DAY_OF_MONTH);
return i == i1;
}
4.判断当前时间是否在startTime、EndTime之内
private static final String TIME_FORMAT_24 = "H:mm:ss";
//是否在两个时间之内(13:10:00)
public boolean isStartTimeEndTimeBetween(String mCurrentTime, String mStartTime, String mEndTime) {
if (TextUtils.isEmpty(mCurrentTime) || TextUtils.isEmpty(mStartTime) || TextUtils.isEmpty(mEndTime)) {
return false;
}
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf1 = new SimpleDateFormat(TIME_FORMAT_24);
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf2 = new SimpleDateFormat(TIME_FORMAT_24);
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf3 = new SimpleDateFormat(TIME_FORMAT_24);
Date currentDate;
Date startDate;
Date endDate;
try {
currentDate = sdf1.parse(mCurrentTime);
startDate = sdf2.parse(mStartTime);
endDate = sdf3.parse(mEndTime);
if (currentDate == null || startDate == null || endDate == null) {
return false;
}
long currentDateTime = currentDate.getTime();
long startDateTime = startDate.getTime();
long endDateTime = endDate.getTime();
return currentDateTime >= startDateTime && currentDateTime <= endDateTime;
} catch (ParseException e) {
Log.i(TAG, "e: " + e.getMessage());
}
return false;
}
5.获取今天是星期几
Calendar mCalendar = Calendar.getInstance();
mCalendar.setTimeInMillis(System.currentTimeMillis());
int mWeek = mCalendar.get(Calendar.DAY_OF_WEEK);
log.i("TAG","今天是周: "+daysOfWeek.contains(mWeek - 1));
6.判断开始时间是否大于结束时间
private static final String TIME_FORMAT_24 = "H:mm:ss";
//开始是否大于结束时间(Eg:12:30:00)
public boolean isStartThanEndTime(String mStartTime, String mEndTime) {
if (TextUtils.isEmpty(mStartTime) || TextUtils.isEmpty(mEndTime)) {
return false;
}
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf1 = new SimpleDateFormat(TIME_FORMAT_24);
@SuppressLint("SimpleDateFormat") SimpleDateFormat sdf2 = new SimpleDateFormat(TIME_FORMAT_24);
Date startDate;
Date endDate;
try {
startDate = sdf1.parse(mStartTime);
endDate = sdf2.parse(mEndTime);
if (startDate == null || endDate == null) {
return false;
}
long startDateTime = startDate.getTime();
long endDateTime = endDate.getTime();
return startDateTime > endDateTime;
} catch (ParseException e) {
Log.i(TAG, "e: " + e.getMessage());
}
return false;
}
7. 24小时制转换12小时制(1:00 PM --> 13:00:00 )
public String getConvert24Time(String mTime) {
String mDateFormat = null;
try {
@SuppressLint("SimpleDateFormat") SimpleDateFormat mFormat = new SimpleDateFormat("HH:mm:ss");
Date mDate = new SimpleDateFormat("hh:mm aa", Locale.US).parse(mTime);
if (mDate == null) {
return "";
}
mDateFormat = mFormat.format(mDate);
} catch (ParseException e) {
WpkLogUtil.i(TAG, "e: " + e.getMessage());
}
Log.i(TAG, "mDate: " + mDateFormat);
return mDateFormat;
}
8.12小时制转换24小时制(1:00 PM --> 13:00:00 )
public String getConvert24Time(String mTime) {
String mDateFormat = null;
try {
@SuppressLint("SimpleDateFormat") SimpleDateFormat mFormat = new SimpleDateFormat("HH:mm:ss");
Date mDate = new SimpleDateFormat("hh:mm aa", Locale.US).parse(mTime);
if (mDate == null) {
return "";
}
mDateFormat = mFormat.format(mDate);
} catch (ParseException e) {
WpkLogUtil.i(TAG, "e: " + e.getMessage());
}
Log.i(TAG, "mDate: " + mDateFormat);
return mDateFormat;
}