1.判断String时间是否是今天,格式为 yyyy-MM-dd
public static boolean isNow(String dateStr){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simpleDateFormat.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
// 默认的年月日的格式. yyyy-MM-dd
String PATTEN_DEFAULT_YMD = "yyyy-MM-dd";
// 当前时间
Date now = new Date();
SimpleDateFormat sf = new SimpleDateFormat(PATTEN_DEFAULT_YMD);
//获取今天的日期
String nowDay = sf.format(now);
//对比的时间
String day = sf.format(date);
return day.equals(nowDay);
}
2.判断当前的时分秒是否大于 String格式的时间,(比如说日出日落)
// 判断两个时间 HH:mm:ss
public boolean isBigger(String riseTime){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss",Locale.ENGLISH);
Date dateTimeNow = new Date(System.currentTimeMillis());
String timeNow = sdf.format(dateTimeNow);
System.out.println("if is timeNow=="+timeNow);
System.out.println("if is riseTime=="+riseTime);
try {
Long timeNowLong= sdf.parse(timeNow).getTime();
Long riseTimeLong= sdf.parse(riseTime).getTime();
System.out.println("if is timeNowLong=="+timeNowLong);
System.out.println("if is riseTimeLong=="+riseTimeLong);
if (timeNowLong >= riseTimeLong){
return true;
}else{
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
3.格式转换
public String TimeFormat(String todayTime) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd EEEE", Locale.ENGLISH);
Date date = sdf.parse(todayTime);
String timestr2 = sdf1.format(date);
return timestr2;
} catch (ParseException e) {
e.printStackTrace();
}
return todayTime;
}