Struggling with how to compare hours with different time zones in Java?

前端 未结 2 1868
后悔当初
后悔当初 2020-12-18 02:27

I have 2 date object in the database that represent the company\'s working hours.

I only need the hours but since I have to save date. it appears like this:

2条回答
  •  离开以前
    2020-12-18 02:48

    Try something like this:

    Calendar companyWorkStart = new GregorianCalendar(companyTimeZone);
    companyWorkStart.setTime(companyWorkStartHour);
    
    Calendar companyWorkEnd = new GregorianCalendar(companyTimeZone);
    companyWorkEnd.setTime(companyWorkEndHour);
    
    Calendar user = new GregorianCalendar(userTimeZone);
    user.setTime(userTime);
    
    if(user.compareTo(companyWorkStart)>=0 && user.compareTo(companyWorkEnd)<=0) {
      ...
    }
    

提交回复
热议问题