Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {

    public static long betweenMinute1(String t1, String t2, String... patterns) {
        long betweenMinute = betweenSecond1(t1, t2, patterns);
        if (betweenMinute != Long.MIN_VALUE) {
            betweenMinute /= 60;
        }
        return betweenMinute;
    }

    public static long betweenSecond1(String t1, String t2, String... patterns) {
        long betweenSecond = betweenMilli1(t1, t2, patterns);
        if (betweenSecond != Long.MIN_VALUE) {
            betweenSecond /= 1000;
        }
        return betweenSecond;
    }

    public static long betweenMilli1(String t1, String t2, String... patterns) {
        if (patterns != null && patterns.length > 0) {
            return getDate(t1, patterns[0]).getTime()
                    - getDate(t2, patterns.length > 1 ? patterns[1] : patterns[0]).getTime();
        }
        return Long.MIN_VALUE;
    }

    public static Date getDate(String t, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
        try {
            Date date = dateFormat.parse(t);
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.setTime(date);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }
}