java工具-Calender工具类

Calender主要用于时间解析转换。
Calender接口相关中文介绍见:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

相关代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* Created on 2018/07/23
*/
public class TimeUtil {
private static final String TAG = “TimeUtil”;
private static final boolean DEBUG = false;
private static final int TODAY = 0;
private static final int YESTERDY = -1;
private static final int THE_DAY_BEFORE_YESDAY = -2;

/**
 * 是否为前天
 * @param day (yyyy-MM-dd)
 * @return true, false
 * @throws ParseException
 */
public static boolean isTheDayBeforeYesterday(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                - pre.get(Calendar.DAY_OF_YEAR);

        if (diffDay == THE_DAY_BEFORE_YESDAY) {
            return true;
        }
    }
    return false;
}

/**
 * 是否为昨天
 * @param day (yyyy-MM-dd)
 * @return true, false
 * @throws ParseException
 */
public static boolean isYesterday(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                - pre.get(Calendar.DAY_OF_YEAR);

        if (diffDay == YESTERDY) {
            return true;
        }
    }
    return false;
}

/**
 * 是否为今天
 * @param day(yyyy-MM-dd)
 * @return true, false
 * @throws ParseException
 */
public static boolean isToday(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                - pre.get(Calendar.DAY_OF_YEAR);

        if (diffDay == TODAY) {
            return true;
        }
    }
    return false;
}

/**
 * 是否为今年
 * @param day(yyyy-MM-dd)
 * @return  true, false
 * @throws ParseException
 */
public static boolean isThisYear(String day) throws ParseException {
    Calendar pre = Calendar.getInstance();
    Date predate = new Date(System.currentTimeMillis());
    pre.setTime(predate);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(day);
    cal.setTime(date);

    if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
        return true;
    }
    return false;
}

/**
 * 是否是AM.
 * @param time(HH:mm:ss)
 * @return true, false
 * @throws ParseException
 */
public static boolean isAM(String time) throws ParseException {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = sdf.parse(time);
    cal.setTime(date);

    //0 : AM, 1: PM
    int apm = cal.get(Calendar.AM_PM);
    return (apm == 0);
}

/**
 * 获取12小时制的时间 hh:mm。
 * @param time 24小时制时间(HH:mm:ss)
 * @return 12小时制的时间(hh:mm)
 * @throws ParseException
 */
public static String get12HourSystemTime(String time) throws ParseException {
    System.out.println("get12HourSystemTime()");
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Date date = sdf.parse(time);
    cal.setTime(date);

    if (cal.get(Calendar.MINUTE) == 0) {
        return new StringBuilder().append(cal.get(Calendar.HOUR))
                .append(":").append(cal.get(Calendar.MINUTE)).append("0").toString();
    } else {
        return new StringBuilder().append(cal.get(Calendar.HOUR))
                .append(":").append(cal.get(Calendar.MINUTE)).toString();
    }
}

}

你可能感兴趣的:(java基础)