转  Calendar  调用  compareTo方法比较

解决方案 »

  1.   


    public static void main(String[] args) throws Exception {
    String str_0 = "20130505";
    String str_1 = "20130506";

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    Date d_0 = sdf.parse(str_0);
    Date d_1 = sdf.parse(str_1);

    Calendar ca_0 = Calendar.getInstance();
    Calendar ca_1 = Calendar.getInstance();

    ca_0.setTime(d_0);
    ca_1.setTime(d_1);

    System.out.println(ca_0.compareTo(ca_1));

    }
      

  2.   

    改进一下
    String time = "05:00:00";

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    cal.setTime(sdf.parse(time));

    Calendar now = Calendar.getInstance();
    cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
    cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
    cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

    // Within two minutes
    if (now.getTimeInMillis() - cal.getTimeInMillis() < 2 * 60 * 1000) {
    System.out.println("Time is up.");
    }
      

  3.   


    package com.jst.util;import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;public class DateUtil {
    public static String formart(Date date, String pattern) {
    if (date == null) {
    return null;
    }
    if (StringUtil.isEmpty(pattern)) {
    return formart(date);
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    return dateFormat.format(date);
    } public static String formart(Date date) {
    if (date == null) {
    return null;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return dateFormat.format(date);
    } public static Date parse(String str) throws ParseException {
    if (str == null || str.trim().equals("")) {
    return null;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return dateFormat.parse(str);
    } public static String getNowTime() {
    return formart(new Date());
    } public static long millisecondBetween(Date startDate, Date endDate) {
    Calendar startCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();
    startCalendar.setTime(startDate);
    endCalendar.setTime(endDate);
    setTimeToMidnight(startCalendar);
    setTimeToMidnight(endCalendar);
    // 获取毫秒
    long startMinutes = startCalendar.getTimeInMillis();
    // 获取毫秒
    long endMinutes = endCalendar.getTimeInMillis();
    long intervalMs = endMinutes - startMinutes;
    return intervalMs;
    } public static int minutesBetween(Date startDate, Date endDate) {
    Calendar startCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();
    startCalendar.setTime(startDate);
    endCalendar.setTime(endDate);
    setTimeToMidnight(startCalendar);
    setTimeToMidnight(endCalendar);
    // 获取毫秒
    long startMinutes = startCalendar.getTimeInMillis();
    // 获取毫秒
    long endMinutes = endCalendar.getTimeInMillis();
    long intervalMs = endMinutes - startMinutes;
    return millisecondsToMinutes(intervalMs);
    } public static int minutesBetweenByDay(Date startDate, Date endDate) {
    Calendar startCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();
    startCalendar.setTime(startDate);
    endCalendar.setTime(endDate);
    setTimeToMidnight(startCalendar);
    setTimeToMidnight(endCalendar);
    // 获取毫秒
    long startMinutes = startCalendar.getTimeInMillis();
    // 获取毫秒
    long endMinutes = endCalendar.getTimeInMillis();
    long intervalMs = endMinutes - startMinutes;
    return millisecondsToDay(intervalMs);
    } /**
     * 毫秒转换为分钟
     * 
     * @param intervalMs
     * @return
     */
    private static int millisecondsToMinutes(long intervalMs) {
    return (int) (intervalMs / (1000 * 60));
    } /**
     * 毫秒转换为天
     * 
     * @param intervalMs
     * @return
     */
    private static int millisecondsToDay(long intervalMs) {
    return (int) (intervalMs / (1000 * 60 * 60 * 24));
    } /**
     * 设置毫秒上的加减
     * 
     * @param calendar
     */
    private static void setTimeToMidnight(Calendar calendar) {
    // calendar.set(Calendar.SECOND, 0);
    } public static String getString(Date date, String pattern) {
    if (date == null) {
    return null;
    }
    if (pattern == null || pattern.trim().equals("")) {
    return getString(date);
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    return dateFormat.format(date);
    } public static String getString(Date date) {
    if (date == null) {
    return null;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return dateFormat.format(date);
    } public static Date parse(String str, String pattern) throws ParseException {
    if (str == null || str.trim().equals("")) {
    return null;
    }
    SimpleDateFormat dateFormat;
    if (pattern != null) {
    dateFormat = new SimpleDateFormat(pattern);
    } else {
    dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    }
    return dateFormat.parse(str);
    } public static void main(String[] args) throws InterruptedException {
    try {
    Date date1 = DateUtil.parse("2014-05-28 15:40", "yyyy-MM-dd HH:mm");
    Thread.sleep(1300);
    long aa = minutesBetween(date1, new Date());
    System.out.println(aa);
    } catch (ParseException e) {
    e.printStackTrace();
    } }
    }