1.字符串日期比较大小
public static boolean compareDate(String pre, String last) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date lastDate = sdf.parse(last);
Calendar lastCal = Calendar.getInstance();
lastCal.setTime(lastDate);
Date preDate = sdf.parse(pre);
Calendar preCal = Calendar.getInstance();
preCal.setTime(preDate);
return preDate.after(lastDate);
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
2.计算字符串日期相差多少天
public static int getDays(String pre, String last) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date lastDate = sdf.parse(last);
Calendar lastCal = Calendar.getInstance();
lastCal.setTime(lastDate);
Date preDate = sdf.parse(pre);
Calendar preCal = Calendar.getInstance();
preCal.setTime(preDate);
long diff = lastCal.getTimeInMillis() - preCal.getTimeInMillis();
float dayCount = (float) diff / (24 * 60 * 60 * 1000);
return (int) dayCount;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
3.获取上午、下午
/***
* 获取上午还是下午
*/
public static String getDateAmOrPm() {
String nihao = "";
Calendar cal = Calendar.getInstance();
//测试代码
//cal.set(Calendar.HOUR_OF_DAY, 15);
if (cal.get(Calendar.AM_PM) == Calendar.AM) {
nihao = "上午好";
} else {
nihao = "下午好";
}
return nihao;
}
4.获取问好
/***
* 根据Calendar的hour来判断
*/
public static String getDateSx() {
String nihao = "";
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
if (hour >= 6 && hour < 8) {
System.out.println("早上好");
nihao = "早上好";
} else if (hour >= 8 && hour < 11) {
System.out.print("上午好");
nihao = "上午好";
} else if (hour >= 11 && hour < 13) {
System.out.print("中午好");
nihao = "中午好";
} else if (hour >= 13 && hour < 18) {
System.out.print("下午好");
nihao = "下午好";
} else {
System.out.print("晚上好");
nihao = "晚上好";
}
return nihao;
}
5.获取周几
public static String[] weeks = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
public static String getWeekday() {
Calendar calendar = Calendar.getInstance();
String weekday = weeks[calendar.get(Calendar.DAY_OF_WEEK) - 1];
return weekday;
}
6.获取字符串年月日
@RequiresApi(api = Build.VERSION_CODES.M)
public static String convert(String dateString) {
if (TextUtils.isEmpty(dateString)) return "";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(dateString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
StringBuilder timestamp = new StringBuilder();
timestamp.append(calendar.get(java.util.Calendar.YEAR) + "-");
int month = calendar.get(java.util.Calendar.MONTH) + 1;
if (month < 10) {
timestamp.append("0" + month + "-");
} else {
timestamp.append(month + "-");
}
int day = calendar.get(java.util.Calendar.DAY_OF_MONTH);
if (day < 10) {
timestamp.append("0" + day);
} else {
timestamp.append(day + "");
}
return timestamp.toString();
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
7.获取昨天
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.add(java.util.Calendar.DAY_OF_MONTH, -1);
文章来源地址https://www.toymoban.com/news/detail-620078.html
文章来源:https://www.toymoban.com/news/detail-620078.html
到了这里,关于Android Calendar的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!