How to compare time string with current time in java?

I have a time line like "15:30". I want to compare this line with the current time. Please suggest something light. And how to get the current time in hour format ("HH: mm")

+4
source share
8 answers

TL; DR

LocalTime.now()
         .isAfter( LocalTime.parse( "15:30" ) )

More details

You should think differently: How to make this line turn into a time value. You would not try math by turning your numbers into strings. Date and time values.

, java.util.Date .Calendar, , , , , . java.time Java 8. java.time Joda-Time.

java.time Joda-Time - : LocalTime.

java.time

java.time, Java, LocalTime. . . isBefore, isAfter isEqual.

LocalTime now = LocalTime.now();
LocalTime limit = LocalTime.parse( "15:30" );
Boolean isLate = now.isAfter( limit );

/ , JVM.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
LocalTime now = LocalTime.now( z );  // Explicitly specify the desired/expected time zone.
LocalTime limit = LocalTime.parse( "15:30" );
Boolean isLate = now.isAfter( limit );

Joda

, Joda-Time, , , java.time.

, Joda-Time mode, java.time.


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

+8

:

String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
String timeToCompare = "15:30";
boolean x = currentTime.equals(timeToCompare);

, x true, x, false

+5

.

String currentTime=new SimpleDateFormat("HH:mm").format(new Date());
+3

public static String compareTime(String d) {
        if (null == d)
            return "";

        try {

            SimpleDateFormat sdf= new SimpleDateFormat("HH:mm");
            d = sdf.format(new Date());
        } catch (Exception e) {
            e.printStackTrace();

        }

        return d;

    }

0

, "15:30" . Java 7, Basil Bourque. , "15:30" true, .

public static boolean checkSlaMissedOrNot(String sla) throws ParseException {
        boolean slaMissedOrnot = false;
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sla.substring(0, 2)));
        cal.set(Calendar.MINUTE, Integer.parseInt(sla.substring(3)));
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (Calendar.getInstance().after(cal)) {
            System.out.println("it SLA missed");
            slaMissedOrnot = true;
        } else {
            System.out.println("it fine & not SLA missed");
        }
        return slaMissedOrnot;
    }

, after(Object when)

, , . .

0

:

MainActivity.java

Button search = (Button) findViewById(R.id.searchBus);
        SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm");

        String limit = "23:00";
        Date limitBus=null;
        try {
            limitBus = sdfDate.parse(limit);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String start = "07:15";
        Date startBus=null;
        try {
            startBus = sdfDate.parse(start);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String user_time = getCurrentTimeStamp();
        Date now = null;
        try {
            now = sdfDate.parse(user_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (now.after(limitBus)||now.before(startBus)){
            Toast.makeText(this, "Bus Operation Hours : 7:15AM - 11.00PM", Toast.LENGTH_LONG).show();
            search.setEnabled(false);
        }

        else
        {
            search.setEnabled(true);
        }



//get current time method
 public static String getCurrentTimeStamp() {
        SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm");//dd/MM/yyyy
        Date now = new Date();
        String strDate = sdfDate.format(now);
        return strDate;
    }
0

So my solution is a little different, it starts by creating a clock with the current time and testing the agaisnt hour defined by the user.

    public void TextClockWindow() {

    this.pack();

    javax.swing.Timer t = new javax.swing.Timer(1000, (ActionEvent e) -> {
        Calendar calendar = new GregorianCalendar();
        String am_pm;
        calendar.setTimeZone(TimeZone.getDefault());

        Calendar now = Calendar.getInstance();
        int h = now.get(Calendar.HOUR_OF_DAY);
        int m = now.get(Calendar.MINUTE);
        int s = now.get(Calendar.SECOND);

        if (calendar.get(Calendar.AM_PM) == 0) {
            am_pm = "AM";

        } else {
            am_pm = "PM";

        }   // Code to Determine whether the time is AM or PM

        Clock.setText("" + h + ":" + m + " " + am_pm);

        if(h < 5) { //int here which tests agaisnt the hour evwery second
        System.out.println("Success");
        } else {
        System.out.println("Failure"); 
        }
    });
    t.start();
}
0
source

This code checks the time entered by the user with the current system time and returns the time difference in the long value.

public long getTimeDifferenceInMillis (String dateTime) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        long currentTime = new Date().getTime();
        long endTime = 0;

        try {
            //Parsing the user Inputed time ("yyyy-MM-dd HH:mm:ss")
            endTime = dateFormat.parse(dateTime).getTime(); 
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }

        if (endTime > currentTime)
            return endTime - currentTime;
        else
            return 0;
    }

The user must transfer the time in the format "yyyy-MM-dd HH: mm: ss" .

0
source

All Articles