Android Calendar

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);

你可能感兴趣的:(android)