Android Date Compare isDateTimeAfter(String startDate, String startTime, String endDate, String endTime)

Here you can find the source of isDateTimeAfter(String startDate, String startTime, String endDate, String endTime)

Description

is Date Time After

Declaration

public static boolean isDateTimeAfter(String startDate,
            String startTime, String endDate, String endTime) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;

import java.util.Date;
import java.util.StringTokenizer;

public class Main {
    public static boolean isDateTimeAfter(String startDate,
            String startTime, String endDate, String endTime) {
        Date startD = dateTimeToDateObj(startDate, startTime);
        Date endD = dateTimeToDateObj(endDate, endTime);

        if (startD.getTime() >= endD.getTime()) {
            return false;
        } else {//  ww  w  .j  av  a  2  s. com
            return true;
        }
    }

    public static Date dateTimeToDateObj(String date, String time) {
        java.util.Calendar cal = java.util.Calendar.getInstance();

        int[] dateTokens;
        dateTokens = tokenizeDate(date);
        cal.set(java.util.Calendar.YEAR, dateTokens[2]);
        cal.set(java.util.Calendar.MONTH, dateTokens[0] - 1);
        cal.set(java.util.Calendar.DATE, dateTokens[1]);

        int[] timeTokens;
        timeTokens = tokenizeTime(time);
        if (timeTokens[0] == 12) {
            timeTokens[0] -= 12;
        }
        cal.set(java.util.Calendar.HOUR, timeTokens[0]);
        cal.set(java.util.Calendar.MINUTE, timeTokens[1]);
        cal.set(java.util.Calendar.SECOND, 0);
        cal.set(java.util.Calendar.MILLISECOND, 0);
        cal.set(java.util.Calendar.AM_PM, timeTokens[2]);

        return new Date(cal.getTimeInMillis());
    }

    public static int[] tokenizeDate(String date) {
        StringTokenizer st = new StringTokenizer(date, "-/");

        ArrayList<Integer> al = new ArrayList<Integer>();

        while (st.hasMoreTokens()) {
            al.add(Integer.parseInt(st.nextToken()));
        }

        int[] tokenList = new int[3];

        for (int i = 0; i < al.size(); i++) {
            tokenList[i] = al.get(i);
        }

        return tokenList;
    }

    public static int[] tokenizeTime(String time) {
        String newTime = time.replaceAll(":", " ").replaceAll("PM", "1")
                .replaceAll("AM", "0"); // 11:27 PM will become "11 17 1"

        StringTokenizer st = new StringTokenizer(newTime, " ");

        ArrayList<Integer> al = new ArrayList<Integer>();

        while (st.hasMoreTokens()) {
            al.add(Integer.parseInt(st.nextToken()));
        }

        int[] tokenList = new int[3];

        for (int i = 0; i < al.size(); i++) {
            tokenList[i] = al.get(i);
        }

        return tokenList;
    }
}

Related

  1. isSameInstant(Date date1, Date date2)
  2. needsMidnightCountdown()
  3. dateDiffer(Date date1, Date date2)
  4. compare(Date date1, Date date2)
  5. compare(String date0, String date1, String format)
  6. isDateNew(String YearMonthDayHourMinuteSeconds)
  7. isSameDay(Date d1, Date d2)
  8. isSameDay(Date date1, Date date2)