How to compare current time with time range?

前端 未结 8 1732
庸人自扰
庸人自扰 2020-12-30 12:02

I have two String variables - time1 and time2. Both contain value in the format HH:MM. How can I check:

  1. If the current time
相关标签:
8条回答
  • 2020-12-30 12:17
        Try this if you have specific time Zone.  
    
                  try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("hh a");
                Date timeseven = dateFormat.parse("7 AM");
                Date timeTen = dateFormat.parse("10 AM");
                Date timeOne = dateFormat.parse("1 PM");
                Date timefour = dateFormat.parse("4 PM");
                Date timefive = dateFormat.parse("10 PM");
                //Get current time
                // Date CurrentTime = dateFormat.parse(dateFormat.format(new Date()));
                //Sample time 
                Date CurrentTime = dateFormat.parse("9 PM");
    
                if (CurrentTime.after(timeseven) && CurrentTime.before(timeTen)) {
                    Toast.makeText(this, "FIRST", Toast.LENGTH_SHORT).show();
                } else if (CurrentTime.after(timeTen) && CurrentTime.before(timeOne)) {
                    Toast.makeText(this, "Secound", Toast.LENGTH_SHORT).show();
                } else if (CurrentTime.after(timeOne) && CurrentTime.before(timefour)) {
                    Toast.makeText(this, "THird", Toast.LENGTH_SHORT).show();
                } else if (CurrentTime.after(timefour) && CurrentTime.before(timefive)) {
                    Toast.makeText(this, "Fourth", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Not found in your time zone", Toast.LENGTH_SHORT).show();
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    0 讨论(0)
  • 2020-12-30 12:20

    For example if you want to compare time between 11pm to 6am for calculating extra night fare for any vechicle. then following code will help you.

    // Code

    package com.example.timedate;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.TimeZone;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.text.format.DateFormat;
    import android.text.format.Time;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        TextView tv;
        Button bt;
        int hour,min;
        String AM_PM;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView)findViewById(R.id.textView1);
            bt = (Button)findViewById(R.id.button1);
    
    
    
            final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());*/
    
            bt.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
    
    
                    Calendar c = Calendar.getInstance();
                    hour = c.get(Calendar.HOUR_OF_DAY);
                    min = c.get(Calendar.MINUTE);
                    int ds = c.get(Calendar.AM_PM);
                    if(ds==0)
                    AM_PM="am";
                    else
                    AM_PM="pm";
    
                    Toast.makeText(MainActivity.this, ""+hour+":"+min+AM_PM, Toast.LENGTH_SHORT).show();
                    if((hour==11&&AM_PM.matches("pm")) || (hour<7&&AM_PM.matches("am"))  || (hour==12&&AM_PM.matches("am")))
                    {
                        Toast.makeText(MainActivity.this, "Time is between the range", Toast.LENGTH_SHORT).show();
                    }
                    else
                        Toast.makeText(MainActivity.this, "Time is not between the range", Toast.LENGTH_SHORT).show();
    
                }
            });
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }`
    
    0 讨论(0)
  • 2020-12-30 12:22

    if you want time between after 9PM to before 9Am you can use following condition..

    if(cal.get(Calendar.HOUR_OF_DAY)> 20 || cal.get(Calendar.HOUR_OF_DAY)< 9)
    {
        // do your stuffs
    }
    
    0 讨论(0)
  • 2020-12-30 12:23
    • Convert the two strings to Date objects (which are also time objects) Create a new Date object.
    • This will contain the current time.
    • Use the Date.before() and Date.after() methods to determine if you are in the time interval.

    EDIT: You should be able to use this directly (and no deprecated methods)

    public static final String inputFormat = "HH:mm";
    
    private Date date;
    private Date dateCompareOne;
    private Date dateCompareTwo;
    
    private String compareStringOne = "9:45";
    private String compareStringTwo = "1:45";
    
    SimpleDateFormat inputParser = new SimpleDateFormat(inputFormat, Locale.US);
    
    private void compareDates(){
        Calendar now = Calendar.getInstance();
    
        int hour = now.get(Calendar.HOUR);
        int minute = now.get(Calendar.MINUTE);
    
        date = parseDate(hour + ":" + minute);
        dateCompareOne = parseDate(compareStringOne);
        dateCompareTwo = parseDate(compareStringTwo);
    
        if ( dateCompareOne.before( date ) && dateCompareTwo.after(date)) {
            //yada yada
        }
    }
    
    private Date parseDate(String date) {
    
        try {
            return inputParser.parse(date);
        } catch (java.text.ParseException e) {
            return new Date(0);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 12:24

    As of now, I am thinking about the following approach:

    int clTime = Integer.parseInt(time1.substring(0, 1))*60 + Integer.parseInt(time1.substring(3, 4)); 
    
    Time now = new Time();
    now.setToNow();
    int nowTime = now.hour*60 + now.minute;
    

    So, I'll need to compare just integer values clTime and nowTime.

    0 讨论(0)
  • 2020-12-30 12:26
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
    
    Date EndTime = dateFormat.parse("10:00");
    
    Date CurrentTime = dateFormat.parse(dateFormat.format(new Date()));
    
    if (CurrentTime.after(EndTime))
    {
        System.out.println("timeeee end ");
    }
    

    Don't forget to surrounded with a try catch block

    0 讨论(0)
提交回复
热议问题