Java determines whether a time is __java in a time period

Source: Internet
Author: User
Tags time interval
Import Java.text.ParseException;


Import Java.text.SimpleDateFormat; public class Datetest {/** * @param args */public static void main (string[] args) {System.out.println (datetest.i
    Sintime ("20:00-01:00", "01:00"));
    System.out.println (Datetest.isintime ("20:00-01:00", "00:00"));
    System.out.println (Datetest.isintime ("20:00-01:00", "03:00"));
    System.out.println ();
    System.out.println (Datetest.isintime ("20:00-23:00", "03:00"));
    System.out.println (Datetest.isintime ("20:00-23:00", "22:00"));
    System.out.println (Datetest.isintime ("20:00-23:00", "18:00"));
    System.out.println (Datetest.isintime ("20:00-23:00", "20:00"));
System.out.println (Datetest.isintime ("20:00-23:00", "23:00")); /** * Determine whether a certain time is within an interval * * @param sourcetime * time interval, half closed, such as [10:00-20:00) * @param curtime * required Judgment time such as 10:00 * @return * @throws illegalargumentexception/public static Boolean Isintime (String sourcetime, String Curtime) {if (Sourcetime = = Null | | !sourcetime.contains ("-") | |
    !sourcetime.contains (":")) {throw new IllegalArgumentException ("Illegal Argument arg:" + sourcetime); } if (curtime = null | |!curtime.contains (":")) {throw new IllegalArgumentException ("Illegal Argument arg:"
    + curtime);
    } string[] args = Sourcetime.split ("-");
    SimpleDateFormat SDF = new SimpleDateFormat ("hh:mm");
        try {Long now = Sdf.parse (curtime). GetTime ();
        Long start = Sdf.parse (Args[0]). GetTime ();
        Long end = Sdf.parse (Args[1]). GetTime ();
        if (Args[1].equals ("00:00")) {args[1] = "24:00";
            } if (end < start) {if [now >= end && Now < start] {return false;
            else {return true;
            } else {if (now >= start && now < end) {return true;
            else {return false;
       } } catch (ParseException e) {e.printstacktrace ();
    throw new IllegalArgumentException ("Illegal Argument arg:" + sourcetime); }

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.