Java Time Format compareTime(String time1, String time2, String dateFormat)

Here you can find the source of compareTime(String time1, String time2, String dateFormat)

Description

compare Time

License

Apache License

Declaration

public static boolean compareTime(String time1, String time2, String dateFormat) 

Method Source Code

//package com.java2s;
/*//from   ww w  .j a  v a  2 s .co m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static boolean compareTime(String time1) {
        return compareTime(time1, getCurrDateStr(), "yyyy-MM-dd HH:mm:ss");
    }

    public static boolean compareTime(String time1, String time2) {
        return compareTime(time1, time2, "yyyy-MM-dd HH:mm:ss");
    }

    public static boolean compareTime(String time1, String time2, String dateFormat) {
        SimpleDateFormat t1 = new SimpleDateFormat(dateFormat);
        SimpleDateFormat t2 = new SimpleDateFormat(dateFormat);
        try {
            Date d1 = t1.parse(time1);
            Date d2 = t2.parse(time2);
            return d1.before(d2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getCurrDateStr() {
        return getCurrDateStr("yyyy-MM-dd HH:mm:ss");
    }

    public static String getCurrDateStr(String dateFormat) {
        return convertDateToStr(new Date(), dateFormat);
    }

    public static String convertDateToStr(Date date, String dateFormat) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(date);
    }
}

Related

  1. calFormatTime(final Calendar cal)
  2. createTimeFormatter(String languageCode)
  3. format(long time)
  4. format(long time)
  5. format(long time, String timerPrecisionId, boolean round)