Compare user defined time and current time Java

前端 未结 3 1158
甜味超标
甜味超标 2021-01-26 23:25

I am trying to compare user defined time in format HH:MM with current time in an infinite loop. When they are equal, some action should occur.

I have used following code

3条回答
  •  再見小時候
    2021-01-26 23:50

    If you don't set YEAR, MONTH, DAY it starts from 1/1/1970.

    So basically you need to set YEAR, MONTH, DAY to the date you like.

    In your code you are creating a new Date setting only hour and minutes and leaving the other values to default.

    From javadoc:

    This parsing operation uses the calendar to produce a Date. All of the calendar's date-time fields are cleared before parsing, and the calendar's default values of the date-time fields are used for any missing date-time information. For example, the year value of the parsed Date is 1970 with GregorianCalendar if no year value is given from the parsing operation.

提交回复
热议问题